With the popularity of today’s mobile devices, surfing the Internet has become more convenient than in the past. For mobile terminals such as Android smartphones and iPhone/iPad, many websites have successively launched web pages for access by mobile devices such as computers and such mobile phones. The example code described in this article can automatically jump to pages suitable for browsing based on these mobile device terminals. That is, it determines the PC side or the smart side of the mobile site and jumps.
The complete example code is as follows:
<?php $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $iphone = (strpos($agent, 'iphone')) ? true : false; $ipad = (strpos($agent, 'ipad')) ? true : false; $android = (strpos($agent, 'android')) ? true : false; if($iphone || $ipad) { echo "<script>window.location.href='pc.html'</script>";//这里也可以是网址 } if($android){ echo "<script>window.location.href='andorid.html'</script>";//这里也可以是网址 } ?>
HereUse the built-in function HTTP_USER_AGENT in PHP to obtain the client device type, then use the string processing function to extract it, then determine what device it is, and automatically locate the appropriate web page or page to send based on the device. to the client.
Basic HTML language can do it
The code can be added anywhere, This is an HTML tag. It can be recognized by any browser
Php itself does not have a complete page jump function. Maybe the Header function is one, but it can only be used for the first line of the page. If placed at the end of the Php page, unless the previous Php does not output any characters, an error will be reported. The following are three methods for automatic page jump in Php:
1: Use the Header function.
Two: Use HTML inherent tags. (Not only applicable to Php, but also applicable to ASP, .Net, and Jsp).
Three: Output javascript and use Js code to achieve the purpose of automatic jump to the Php page. (It also applies to languages other than Php, but the corresponding language codes are different). 1. Use HTTP header information (Header function)
that is, use PHP’s HEADER function. The function of the HEADER function in PHP is to issue control instructions to the browser that should be passed through the WEB server specified by the HTTP protocol, such as declaring the type of return information ("Context-type: xxxx/xxxx"), the attributes of the page ("No cache", "Expire"), etc.
The method to use HTTP header information to make Php automatically jump to another page is as follows:
$url = index.php
Header("HTTP/1.1 303 See Other") ;
Header("Location: $url");
exit;
?>
Note that there is a space after "Localtion:". 2. Use HTML tags (REFRESH attribute in META)
Use HTML tags, that is, use META’s REFRESH tag. For example:
< ;HTML>