I have to two site one is my main site and otherone is for mobile site.This is the script i am using for redirecting the site on mobile when it is being used in mobile device. Now i want to ignore the mobile site redirection for iPad. I have used this script but its not ignoring iPad it still redirecting on mobile site on Ipad and i dont want this. Plz help.
<?php
function check_user_agent ( $type = NULL ) {
$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( $type == 'bot' ) {
// matches popular bots
if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {
return true;
// watchmouse|pingdom\.com are "uptime services"
}
} else if ( $type == 'browser' ) {
// matches core browser types
if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {
return true;
}
} else if ( $type == 'mobile' ) {
// matches popular mobile devices that have small screens and/or touch inputs
// mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America
// detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here
if( preg_match ( "/iPad/", $user_agent )) {
return false;
} else if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
// these are the most common
return true;
} else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
// these are less common, and might not be worth checking
return true;
}
}
return false;
}
$ismobile = check_user_agent('mobile');
if($ismobile) {
header('Location:mobiles_site_url');
}
?>
You used strtolower() on the user agent string and the first line to check for the 'iPad' has an uppercase letter in it.
Try:
if( preg_match ( "/ipad/", $user_agent )) { // all lower case
....
}
Related
I have a small website (using HTML, PHP and MySQL), and would like to display a specific banner image according to the country of the visitor. Each country has a different banner image.
I have searched Google for solutions and found quite some API's (such as HostIP) that allow to return the country based upon the IP address. That's nice, but I could not find how to implement it for my purpose to make the image switch according to the country...
I have no developer knowledge. Can anyone help me out?
Get Geo-IP Information
Requests a geo-IP-server (netip.de) to check, returns where an IP is located (host, state, country, town).
<?php
$ip='94.219.40.96';
print_r(geoCheckIP($ip));
//Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )
//Get an array with geoip-infodata
function geoCheckIP($ip)
{
//check, if the provided ip is valid
if(!filter_var($ip, FILTER_VALIDATE_IP))
{
throw new InvalidArgumentException("IP is not valid");
}
//contact ip-server
$response=#file_get_contents('http://www.netip.de/search?query='.$ip);
if (empty($response))
{
throw new InvalidArgumentException("Error contacting Geo-IP-Server");
}
//Array containing all regex-patterns necessary to extract ip-geoinfo from page
$patterns=array();
$patterns["domain"] = '#Domain: (.*?) #i';
$patterns["country"] = '#Country: (.*?) #i';
$patterns["state"] = '#State/Region: (.*?)<br#i';
$patterns["town"] = '#City: (.*?)<br#i';
//Array where results will be stored
$ipInfo=array();
//check response from ipserver for above patterns
foreach ($patterns as $key => $pattern)
{
//store the result in array
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}
return $ipInfo;
}
?>
to complete the anwer of Avinash, is this the right solution to switch image based upon country?
function switchImage($var) {
switch ($var)
{
case "United states":
$source = '/images/US.png';
$class = 'myClass';
$alt = 'myAlt';
break;
case "United Kingdom":
$source = '/images/UK.png';
$class = 'myClass';
$alt = 'myAlt';
break;
.
.
.
default:
return "Default"; //default case
}
}
I have a xampp server installed in a windows cpu which I use as a public server for my clients. Now I have made a request box for my clients to drop some request on it, and I am saving their IP in db while sending data through php. I use this php script to get the clients IP address
function get_ip() {
//Just get the headers if we can or else use the SERVER global
if ( function_exists( 'apache_request_headers' ) ) {
$headers = apache_request_headers();
} else {
$headers = $_SERVER;
}
//Get the forwarded IP if it exists
if ( array_key_exists( 'X-Forwarded-For', $headers ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
$the_ip = $headers['X-Forwarded-For'];
} elseif ( array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 )
) {
$the_ip = $headers['HTTP_X_FORWARDED_FOR'];
} else {
$the_ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
}
return $the_ip;
}
echo get_ip();
But XAMPP is showing the machine's default gateway instead of showing the visitor's IP.
How can I fix that?
I'm trying to get a little script working. It shouldn't be hard, but I'm kind of a n00b when it comes to PHP (my last experiments were many years ago).
So, basically, I want a little script that IF the client uses a mobile device, it gives a link to Facebook as an app link. IF the client has a regular laptop, the script should output a regular Facebook.
Okay, so I'm just going to show my screwed up code, to make it (hopefully) more clear what I'm trying (some might notice that I grabbed part of the code from another thread):
<?php
function check_user_agent ( $type = NULL ) {
$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( $type == 'mobile' ) {
if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
echo "fb://groups/334257489999204";
} else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
echo "fb://groups/123456789";
}
}
else {
echo "https://www.facebook.com/groups/123456789";
};
echo "https://www.facebook.com/groups/123456789";
};
?>" />
This code is in a html anchor href tag, inside a .php file (mainly HTML though).
Thanks!
This should set you on the right path
$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( empty($user_agent) ) {
$is_mobile = false;
} elseif ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap|phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
$is_mobile = true;
} else {
$is_mobile = false;
}
echo (is_mobile) ? 'I am a mobile !' : 'I am not a mobile :(';
I would like to create an icon for my website when user visits my website from mobile. I assume i will have to check the OS which request the url and on knowing from where the request came ie Apple, Android or Blackberry phones and executing script which will create icon if accepted by user..
Can this be done ? If yes, then how ?
Check the code below this will work
<?php
$ismobile = 0;
$container = $_SERVER['HTTP_USER_AGENT'];
// Mobile Company And Os
$useragents = array ('Blazer','Palm','Handspring','Nokia','Kyocera','Samsung','Motorola','Smartphone','Windows CE','Blackberry','WAP','SonyEricsson','PlayStation Portable','LG','MMP','OPWV','Symbian','EPOC','android','Android');
foreach ( $useragents as $useragents )
{
if(strstr($container,$useragents))
{
$ismobile = 1;
$browser = $useragents;
}
}
if ( $ismobile == 1 )
{
echo "<p>Browsing Using ".$browser." device</p>";
}
?>
And The Out Put Will Be Like Below
Browsing Using Android device
Yesterday, i post an 'ad' on an ebay site (marktplaats.nl) and the ad was immediately visible in google when i search for this ad on Google.
How do they do this?
Because it is a very large site, i don't think they repost the sitemap. I have made such function to update the sitemap but don't know this is a good method. What i have is this (part of my library):
// returns false when input invalid, returns a number (array index) when url is invalid or // request fails, returns true when complete:
function suSubmitSitemap( $sXmlUrl, $asSearchEnginePingUrl = 'http://www.google.com/ping?sitemap=%s', $bContinueOnError = false )
{
if( !suIsValidString( $sXmlUrl ) || ( !suIsValidString( $asSearchEnginePingUrl ) && !suIsValidArray( $asSearchEnginePingUrl )))
{ return false; }
$a = (is_array($asSearchEnginePingUrl))?$asSearchEnginePingUrl:explode(',', $asSearchEnginePingUrl );
$sXmlUrl = urlencode( $sXmlUrl );
$ret = false;
foreach( $a as $i=>$sUrl )
{
$sUri = str_replace( '%s', $sXmlUrl, $sUrl );
$bValid = (!is_bool( strpos( $sUrl, '%s' )) && suGetUrlContent( $sUri ));
if( !$bValid )
{
if( !$bContinueOnError )
{ return $i; }
if( !is_array( $ret ))
{ $ret = array(); }
$ret[$i] = $sUri;
}
}
return ret;
}
Is this a safe way to do this (Google will not ban you when frequently called), when this is not the way to do it, does anyone know how implement such index update functionality in PHP?
Google will constantly crawl frequently updated/popular sites and update their search results. This has the benefit of making Google more relevant.
Resubmitting your site map will not help you get crawled as fast as Ebay. Get more visitors and post content more frequently to get Google to visit your site more often.
Also, check out:
http://www.google.com/support/webmasters/bin/answer.py?answer=48620
http://www.searchenginejournal.com/10-ways-to-increase-your-site-crawl-rate/7159/