Home  >  Article  >  Backend Development  >  How to make your website mobile-friendly using PHP

How to make your website mobile-friendly using PHP

藏色散人
藏色散人Original
2019-02-14 13:32:102890browse

It is important to make your website accessible to all users. While many people access your website from computers, there are also people who access your website from mobile phones and tablets. When you program your website, it's important to remember these types of media so that your website works on these devices.

How to make your website mobile-friendly using PHP

#PHP is all processed on the server, so when the code reaches the user, it's just HTML. Basically, the user requests a page from your website from your server, and then your server runs all the PHP and sends the PHP results back to the user. The device never actually sees or has to work with the actual PHP code. This gives websites completed in PHP an advantage over other languages ​​such as Flash that are processed on the user side.

Redirecting users to the mobile version of your website has become popular. This can be done using an htaccess file or using PHP. One way is to use strpos() to find the name of some device.

Here's an example:

<?php

$android = strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],"Android");

$bberry = strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],"BlackBerry");

$iphone = strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],"iPhone");

$ipod = strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],"iPod");

$webos = strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],"webOS");

if ($android || $bberry || $iphone || $ipod || $webos== true)

{

    header(&#39;Location: http://www.yoursite.com/mobile&#39;);

}

If you choose to redirect users to a mobile site, be sure to provide users with an easy way to access the entire site.

Another thing to remember is that if someone comes to your site from a search engine, they usually won't browse your homepage, so they don't want to be redirected there. Instead, redirect them from the SERP (search engine results page) to the mobile version of the article.

Interestingly, this CSS switcher script is probably written in PHP. This allows users to place different CSS templates via a drop-down menu. This will allow you to offer the same content in different mobile-friendly versions, perhaps one for phones and another for tablets. This way users can choose to change one of the templates but also choose to keep the full version of the site if they wish.

One last thing to consider: While PHP is great for websites accessed by mobile users, people often combine PHP with other languages ​​to allow their sites to do whatever they want. Be careful when adding new features, lest they make your site unusable by members of the mobile community.

Related recommendations: "PHP Tutorial"http://www.php.cn/course/list/29.html

The above is the detailed content of How to make your website mobile-friendly using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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