I would like to re-direct visitors to my webpage to a mobile-specific version, based on the width of their device. I tried to do user-agent detection, but the more I learned, the more I realized how bad an idea it was. I put this code in, and the problem is that I don't know how to test if it works before putting the page live. I tried using DevTools emulator but it just shows the regular page and doesn't do the re-direct. I tried downloading the PHP file to my phone to test it, but can't open the PHP file there. Should I download an app to test PHP on the phone? Or is there some simple way to test this that has evaded my elaborate google searches? Does my code look ok?
<link rel="alternate" media="only screen and (max-width: 770px)" href="http://m.mysite.ca/" >
<?php if (substr_count($_SERVER[‘HTTP_ACCEPT_ENCODING’], ‘gzip’)) ob_start(“ob_gzhandler”); else ob_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.responsive {
width: 99%;
height: auto;
}
if (window.screen.width < 770) {
window.location = 'm.mysite.ca';
}
</style>
<link href="css/bootstrap-4.4.1.css" rel="stylesheet" type="text/css">
</head>
My Answer:
The code you provided is missing, so I cannot comment on whether it will work or not. However, I can provide you with a general approach to testing mobile redirects in PHP.
One way to test the mobile redirect code is to use a browser extension that allows you to change the user agent of your browser. This will simulate different devices and screen sizes, allowing you to test the redirect logic based on the width of the device.
Another option is to use online tools that allow you to test your website on different devices and screen sizes, such as BrowserStack or Responsinator. These tools will give you a preview of how your website will look on different devices and allow you to test the redirect logic.
Additionally, you can also set up a local development environment on your computer using software like XAMPP or MAMP, and test the redirect logic on different devices by accessing your local server from your mobile device.
Overall, testing mobile redirects in PHP can be done using browser extensions, online tools, or setting up a local development environment. Make sure to thoroughly test the code before putting it live to ensure it works as expected.
Rate this post
5 of 5 based on 8464 votesComments