Creating a Mobile Friendly Website in PHP

Creating a Mobile Friendly Website in PHP

In the past few years, the number of mobile users (and mobile devices, of course) has been constantly increasing. This trend continues and according to a recent research, in 2015 there will be more mobile devices than people on Earth. Nowadays, people surf a lot using mobile technologies, which implies that the website owners need to make their websites mobile-friendly in order attract more visitors and web developers need to learn how to do that in order to make more money. Keep on reading to find out more about ways to create a mobile-friendly website, their advantages and how to apply them.

Solution 1 – Separate Websites for Desktop and Mobile

In this approach, we will actually create two websites – one for desktop users, and the other for mobile users. A complete separation of desktop and mobile websites allows you to have more control over the features i.e. it is easier to add a new feature for mobile users only or make some desktop features unavailable in the mobile website. Furthermore, recent research has shown that more than a half of the mobile users in the world have reasonably slow internet connections, which means that the mobile version must be much lighter that the desktop one in order to perform well – this is also easy to achieve using this approach. The disadvantage of this solution is that the changes to the website will have to be made twice, making the maintenance harder.

How to Create Separate Websites for Desktop and Mobile in PHP

Let’s assume that your website is already live. First, create a new subdomain for mobile users: e.g. or Copy the main website’s files to that subdomain. Don’t duplicate the database.

At this moment, the mobile website is set up, but is still the same as the desktop one. It needs to be modified. The modification consists of two steps: 1) Remove unnecessary files and features and 2) Create a mobile-friendly design. During the first step, we make the mobile website lighter by removing unnecessary features, Javascript files, CSS, and making images smaller i.e. we are doing everything we can in order to make the website as fast as possible. The second step will produce new design. In most cases, a completely new design is made and is usually 320px wide.

Now, the mobile website is ready and customized, but the mobile users aren’t automatically redirected to the mobile version of the website. To solve this problem, we use a PHP MobileDetect class, which can detect whether a user is using a phone, a tablet or a desktop device to access a website. You can download this PHP class from here: Include the class in all PHP files and add the following code:

Where the current_url function is:

This code detects if a user is visiting your website via a mobile phone and redirects him to the mobile version of the same URL he wants to visit. Also, this PHP code allows you to have a “Go to full site” button for mobile users – just put the following link:

Solution 2 – Responsive Design

The second solution for creating a mobile-friendly website is responsive web design. As the name says, in this approach only the design is changed, i.e. CSS files. This is very good in terms of maintenance and SEO. Google recommends responsive design as the best solution when creating a mobile website. On the other hand, not being able to remove unnecessary files and scripts can have a great impact on your mobile website’s performance.

How to Create a Responsive Web Design

In responsive web design, we have different styles for different screen sizes. The screen sizes are targeted using media CSS queries, which are available in CSS3. The standard device widths for phones and tablets are the following:

You can, of course, remove some of the queries, depending on your needs. After deciding which sizes you will customize, you need to decide how the mobile device design should look, i.e. which elements will be removed and which ones will be resized. Then create CSS styles and apply them for each mobile view. Add the following meta tag in the head of your HTML code:

Keep in mind that the following meta tag may sometimes cause problems with iPad, so if the design doesn’t look good on an iPad, just remove this tag.


Creating a separate mobile website and responsive design are two approaches in creating a mobile-friendly website. There is no right or wrong approach. Use the one that best suits your needs.