PHP realizes the method of automatically jumping the corresponding page according to the device type

  • 2021-07-09 07:35:59
  • OfStack

With the popularity of mobile devices today, surfing the Internet is more convenient than in the past. For mobile terminals such as Android smart phones and iPhone/iPad, many websites have successively launched web pages for mobile devices such as computers and such mobile phones. The example code described in this paper can automatically jump to the page suitable for these mobile devices according to their terminals. That is, judge the PC terminal or the intelligent terminal of the mobile phone station and jump.

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>";// This can also be the website address 
}
if($android){
 echo "<script>window.location.href='andorid.html'</script>";// This can also be the website address 
}
?>

Here, the built-in function HTTP_USER_AGENT in PHP is used to obtain the client device type, and then the string processing function is used to extract it, and then what device is judged, and the suitable webpage or page is automatically located according to the device and sent to the client.


Related articles: