How to Get User Agent in PHP

How to get user agent in PHP

Use the native PHP $_SERVER['HTTP_USER_AGENT'] variable instead.

Get the user agent string of the user from a PHP file used to retrieve a CSS

When you're accessing a web page, your browser is adding the User-Agent to the request HTTP headers. As the CSS file is only another resource requested via HTTP, you should be able to access to header within the PHP file via:

$_SERVER['HTTP_USER_AGENT']

You can read about it in the PHP docs.

EDIT: That should even work for your dynamic CSS file, as the browser is adding the header to all of the HTTP requests.

How to properly check if the user agent is empty in php

Your test for an empty user agent looks fine to me.

In the second code, there's no need to check if $user_agent is in the array. If it's not the empty string that you initialize it with, it must be one of the values from the array. So just write

if ($user_browser == '') {
echo "your browser is not allowed";
}

reliable user browser detection with php

Using an existing method (ie get_browser) is probably better than writing something yourself, since it has (better) support and will be updated with newer versions. There might be also usable libraries out there for getting the browser id in a reliable way.

Decoding the $_SERVER['HTTP_USER_AGENT'] is difficult, since a lot of browsers have quite similar data and tend to (mis)use it for their own benefits. But if you really want to decode them, you could use the information on this page for all available agent ids.
This page also shows that your example output indeed belongs to IE 7. More information about the fields in the agent id itself can be found on this page, but as I said already browsers tend to use it for their own benefits and it could be in a (slightly) other format.

Header redirect if user agent equals variables using PHP

I think you used $user instead of $user_agent.

And according to php manual php manual on stipos you should use triple = like ===.

Here is an example of how it should be.

<?php
//-- Get user agent
//-- Thanks @creditosrapidos10min for hint about strtolower()
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);

//-- BadBot variable
$Baiduspider = stripos($useragent, "Baiduspider");
$DotBot = stripos($useragent, "DotBot");

//-- BadBot constant
$BADBOT = ($Baiduspider||$DotBot);

if ($useragent === $BADBOT){

header("Location: ohno/403.php");
exit;

} else { ?>

Display home page

<?php }
?>

How to Block a browser after detecting it with php on my website?

I solved it using this

    <?php

class Browser
{
public static function detect()
{
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
if ((substr($_SERVER['HTTP_USER_AGENT'], 0, 6) == "Opera/") || (strpos($userAgent, 'opera')) != false) {
$name = 'opera';
} elseif ((strpos($userAgent, 'chrome')) != false) {
$name = 'chrome';
} elseif ((strpos($userAgent, 'safari')) != false && (strpos($userAgent, 'chrome')) == false && (strpos($userAgent, 'chrome')) == false) {
$name = 'safari';
} elseif (preg_match('/msie/', $userAgent)) {
$name = 'msie';
} elseif ((strpos($userAgent, 'firefox')) != false) {
$name = 'firefox';
} else {
$name = 'unrecognized';
}
if (preg_match('/.+(?:me|ox|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name'] == 'safari') {
$version = $matches[1];
}
if (preg_match('/.+(?:me|ox|it|on|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name'] != 'safari') {
$version = $matches[1];
} else {
$version = 'unknown';
}

return array(
'name' => $name,
'version' => $version,
);
}
}

$browser = Browser::detect();
echo 'You browser is ' . $browser['name'] . ' version ' . $browser['version'];
echo "<br />";
?>

I found the solution in this thread.

Furthermore, you can add a condition and use die().

API : How to put useragent into url GET?

Use the urlencode() function to encode special characters.

And &page= should be &ua=.

$query = 'https://website.com/api.php?ip='.$ip.'&ua='.urlencode($useragent);


Related Topics



Leave a reply



Submit