Home > Backend Development > PHP Problem > How to implement mobile phone adaptation jump page in php

How to implement mobile phone adaptation jump page in php

藏色散人
Release: 2023-03-04 11:48:01
Original
4136 people have browsed it

php method to implement mobile phone jump page: first open the "header.php" file; then add the code at the top as "$agent = $_SERVER['HTTP_USER_AGENT'];"; finally save the changes.

How to implement mobile phone adaptation jump page in php

Recommended: "PHP Video Tutorial"

PHP website mobile phone adaptation jump to the corresponding Page

If you have visitors coming from a mobile interface, you will most likely need to direct them to a different page, one specifically optimized for mobile browsers.

1. Add judgment in the header of the page

Through PHP and browser header information, we can easily redirect mobile phone users to a specially designed WAP interface. I happened to use the WP-T-WAP plug-in to implement the WAP interface. The WAP browsing address is http://mkaifa.com/wap. (You can try to browse this address, it can be opened on a computer. Sorry, I am using MobilePress now and there is no demonstration. MobilePress can automatically determine the visitor's browser.)

I hope the mobile phone When visitors open the http://mkaifa.com/ address, they are automatically directed to http://mkaifa.com/wap. How to do it?

The answer is very simple.

Open the header.php file. Insert the following code in the first line:

< ?php
$agent = $_SERVER[&#39;HTTP_USER_AGENT&#39;];
if(strpos($agent,"comFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS"))
header("Location:http://mkaifa.com/wap");
?>
Copy after login

The meaning of this code is that when the UA information returned by the browser is Opera Mini, UCWEB, etc., the page will be redirected to http://mkaifa.com/wap .

1.1 Supplement: Add a jump prompt for mobile browsers

When you use the above method to set up a jump, you may encounter this situation: mobile visitors directly access something like http: The subpage of //mkaifa.com/archive/xxx, not the homepage. At this time, changing the judgment statement will still execute the command and guide the mobile visitor to the homepage of the mobile interface, so that the visitor cannot obtain the desired information.

How to do it?

You can add another judgment in addition to this statement. This command will only be executed when the current page is the home page.

In the WordPress environment, this judgment is written as

<?php if ( is_home() ) { ... }?>
Copy after login

Then the whole code is:

<?php
$agent = $_SERVER[&#39;HTTP_USER_AGENT&#39;];
if ( is_home() && ((strpos($agent,"comFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) )
header("Location:http://mkaifa.com/wap");
}
?>
Copy after login

In this way we encounter another problem: mobile visitors are browsing sub-pages At the time, I didn’t know that you provided a WAP interface, and you just wanted to promote your WAP interface.

We can modify this judgment statement so that it no longer jumps directly, but displays a prompt message in a specific browser to tell the mobile phone user to switch to the WAP interface for browsing.

The code is written as follows:

<?php
$agent = $_SERVER[&#39;HTTP_USER_AGENT&#39;];
if(strpos($agent,"comFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) { ?>
<div id="mobile"><p>系统检测到您正在使用手机浏览器,您可以访问<a href="http://mkaifa.com/wap">移动界面</a>以获取更好的浏览体验。</p></div>
<?php } ?>
Copy after login

Of course, you can combine these two methods to allow mobile visitors to automatically jump when they visit the homepage, and display prompt information when they visit other interfaces.

2. Need enough mobile browser UA information

When the browser accesses the page, it will submit a flag character to the server. This character is called UA (User Agent). They look something like this:

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) )
Copy after login

Yes, the UA is very long and even includes system information. But when we make a judgment, we only need to check the most critical characters. For example, if I need to determine whether the current user is using IE 8 (I am indeed using IE8), then the above judgment statement should be written as strpos($agent,”MSIE 8.0″).

I have prepared the 8 most common browsers/platforms: comFront, iPhone, JAVA (MIDP-2.0), Opera Mini, UCWEB, Android, Windows CE/Mobile, and SymbianOS. As far as the current situation is concerned, these 8 platforms already include almost all mobile visitors in the country. Of course, the complete list of mobile browsing clients is more than that. I will list some:

2.0 MMP, 240×320, 400X240, AvantGo, BlackBerry, Blazer, Cellphone, Danger, DoCoMo, Elaine/3.0 EudoraWeb,   Googlebot-Mobile, hiptop, IEMobile, KYOCERA/WX310K, LG/U990, MIDP-2., MMEF20, MOT-V, comFront, Newt, Nintendo Wii, Nitro, // Nintendo DS Nokia, Opera Mini, Palm, PlayStation Portable, portalmmm, Proxicom, Proxicom, SHARP-TQ-GX10, SHG-i900, Small, SonyEricsson, Symbian OS, SymbianOS, TS21i-10, UP.Browser, UP.Link, webOS, // Palm Pre, etc. Windows CE, WinWAP, YahooSeeker/M1A1-R2D2
Copy after login

You can follow this list Add or replace the PHP judgment statement above. But I still recommend the 8 platforms I have selected, because users who currently use mobile phones to browse the Internet-especially personal blogs, are basically high-end players.

In addition, you can write a simple page to determine the UA of your mobile phone

<?phpecho $_SERVER[&#39;HTTP_USER_AGENT&#39;]; ?>
Copy after login

Save it as ua-test.php, upload it to the server, and use your mobile phone to access this page.

The above is the detailed content of How to implement mobile phone adaptation jump page in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template