I want to determine which user uses which browser when they visit to my website.
For this process i'am using $_SERVER['HTTP_USER_AGENT'] but when i try to print it while i'am using chrome i get this result(on localhost):
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
What can i do to fix that ? Any help would be appriciated
$_SERVER['HTTP_USER_AGENT']
This gives the information of the browser as you said.
Always keep in mind that HTTP_USER_AGENT can be easily spoofed by the user.
get_browser() gives the browser's capability.
you can also use the php class provided by Wolfcast
use like:
$browser = new BrowserDetection();
echo 'You are using ', $browser->getBrowser(), ' version ', $browser->getVersion();
if you really want to differentiate the browsers from the client-side you could use the javascript to find the browser and apply the changes through that based on different browser.
this is a code that i obtained from internet that exactly differentiate the browsers.
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
var isChrome = !!window.chrome && !isOpera; // Chrome 1+
if(isFirefox){
//do the change
} else if (isChrome){
//do the change
}
Related
I am trying to figure out how can I call php functions running on remote of shoretel IP phone .
I can access it through browser by simple IP and it is flash player script but I want to do few things programmatically and I don't find any help to do the same.
The flash player app is calling following api
https:///gateway.php?PHPSESSID=6432fc5f0999d0f4536fced79b95
and in payload of this it is calling php function like LSRoom_Remoting.getHeaderInfo
I am trying but getting error like "Malformed AMF message"
Here is the code
static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
var content = new StringContent("LSRoom_Remoting.getBoardID", Encoding.UTF8, "application/x-amf");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Accept","*/*");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.9");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
//client.DefaultRequestHeaders.Add("Content-Length", "47");
//client.DefaultRequestHeaders.Add("Content-Type", "application/x-amf");
client.DefaultRequestHeaders.Add("Cookie", "PHPSESSID=3cbc7ff268e80e78e1458a4adfdd1f56");
client.DefaultRequestHeaders.Add("DNT", "1");
client.DefaultRequestHeaders.Add("Host", "10.6.32.21");
client.DefaultRequestHeaders.Add("Referer", "https://10.6.32.21/interface/ls_interface.swf");
client.DefaultRequestHeaders.Add("Origin", "https://10.6.32.21");
client.DefaultRequestHeaders.Add("X-Requested-With", "ShockwaveFlash/32.0.0.114");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
var result = client.PostAsync(string.Format("https://10.6.32.21/gateway.php?PHPSESSID={0}", "3cbc7ff268e80e78e1458a4adfdd1f56"), content).Result;
//var result = client.DownloadString("https://10.6.32.21/interface/interface.php?uniqueKey=152322314997139&brand=shoretel&key=");
var obj = result.Content.ReadAsStringAsync();
Console.WriteLine(result.Content.ToString());
Console.ReadKey();
}
}
What i am doing wrong?
it seems likely, that the PHPSESSID is long expired, while not using one from a recent request; because the default timeout is 15 minutes. in case it had not yet been expired, you still have to build a proper AMF message, which consist of a header and a body. there is a library for that: amfphp, which at least should provide some clue how it should look alike, despite it is written in PHP.
I need to get requesting browsers name in my web app.( for analytics )
In core php when I use $visitor_user_agent=$_SERVER['HTTP_USER_AGENT']it returns Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36 string when using with chrome.And then preg_match('/Chrome/i', $visitor_user_agent) can be used to know if its chrome or not.I am not sure if that was efficient way to find browser name or not.
I also found get_browser link but it is not giving browser name.
Is there a way in cakephp3 or core php to get browser name ?
This would return the user agent used for the request:
$this->request->header('User-Agent');
http://book.cakephp.org/3.0/en/controllers/request-response.html
Look into documentation of Request object.
You can get HTTP_USER_AGENT using env() method:
$this->request->env('HTTP_USER_AGENT');
You can also prepare custom detector:
$this->request->addDetector(
'chrome',
['env' => 'HTTP_USER_AGENT', 'pattern' => '/Chrome/i']
);
And then in controller just use is() method as follows:
if($this->request->is('chrome')) {
// do stuff for chrome
}
I'm trying to show the browser that I used to open my site using Codeigniter User Agent Libraries. When I open with IE,Chrome,Firefox,and Safari, User agent return the right value. But when I open with Opera, it return "Chrome" value.
here is part of my code:
Controller
*** another code ***
$this->load->library('user_agent');
$data['browser'] = $this->agent->browser();
$this->load->view('agent',$data);
*** another code ***
View
Your browser is <span><?php echo $browser; ?></span>.
the result if I open with opera is this:
Your browser is Chrome.
My question is, why it return Chrome? How can I fix this problem?
Thanks.
It looks like the user agent string in opera (since version 15) is this:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.100
You might need to extend the user_agent library to do a check to see if the user agent string contains OPR.
https://dev.opera.com/blog/opera-user-agent-strings-opera-15-and-beyond/
As the title says, is there a reliable way to detect whether a user is visiting my page using Chrome on Mac using PHP?
I have a bug that only displays when using Chrome on Mac, and until I get it sorted out I would like to have a popup to recommend the users to use a different browser.
Use $_SERVER['HTTP_USER_AGENT'] to find the users' user agent and a function to check if this is within the string.
//The following will find out whether the user is using a Mac, obviously you can change this by echoing the user agent to find out what you need to search for.
$browser = strpos($_SERVER['HTTP_USER_AGENT'], "Chrome");
$os = strpos($_SERVER['HTTP_USER_AGENT'], "Macintosh; Intel Mac OS X");
if($browser !== false && $os !== false) {
//do stuff if on a mac with Chrome
}
This will work with any type of Mac OS X software and any version of Chrome. If you want to narrow your results down further, you can just use:
if($_SERVER['HTTP_USER_AGENT'] == "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11" ) {
//do stuff here
}
It would be better if you posted the code you're having issues with rather than creating a hacky fix.
However, you can use $_SERVER['HTTP_USER_AGENT'] to check the users browser/OS.
To aid, you would expect something like Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11 from a Mac user on Chrome.
Note
The user agent isn't always set so use isset first to avoid notices appearing.
I know you're looking for the PHP solution to find the browser, but since the problems you are most likely to encounter with a specific browser are client-side, there should be no need to detect it from the server-side.
So here's a solution with javascript, and if you get the match you expect, you can hide the body, or display a popup, or send it to your server, set a cookie, or whatever you see appropriate to "fix" your problem (although you should be looking into really fixing the problem...)
Look here for the source:
function that_is_called_on_load() {
if (BrowserDetect.browser == "Chrome" && BrowserDetect.OS == "Mac") {
alert("You are using mac, sorry.");
window.location = "mac_chrome.php";
}
}
This is a very "dirty" solution, and it might be slightly more accurate/reliable than the server-side one.
I wanted to know in PHP, how to detect on which browser my web application is running.
e.g.
If current browser is chrome then alert("Chrome browser processing") otherwise alert("rest browser processing");
echo $_SERVER['HTTP_USER_AGENT'];
Below output I am getting If i execute above code :
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.21 Safari/532.0
Please suggest your pointers to detect exact browser name.
Thanks,
-Pravin
This has some useful code.
Basically, in your case, you can just look for the string "Chrome". In general, it might take a bit more logic, since, for example, the string "Safari" is found in the user-agents provided by other browsers than Safari (including Chrome). PHP provides the 'browser' element for this.
Personally, I'd use Javascript for this one.
if(navigator.userAgent.match(/Chrome/i)) {
alert("You're using Chrome!");
}
else {
alert("You're using something other than Chrome!");
}
... but if you really wanted to, you could accomplish the same thing in PHP:
if (preg_match("/Chrome/i", $_SERVER['HTTP_USER_AGENT']) == 0) {
// zero matches
echo "<script>alert('You're not using Chrome!')</script>";
} else {
echo "<script>alert('You're using Chrome!')</script>";
}