Undefined Variable
Undefined Variable

Reputation: 4267

Redirecting to responsive site in PHP based on screen resolution detected in Javascript

Currently I have a PHP library that looks at header information and decides what kind of browser/device the user is using to access my site. I hooked up my router to this library so that when a user is at my site, based on what the PHP library say I redirect him to a mobile or desktop version. All this happens in pure PHP.

Now the complication is that many tablet devices have such wide screens that showing them a responsive site for their screen dimension does not really make much sense. I would like it that my router can get information about the screen dimension/viewport of the user and then make the choice instead of just checking if it isTablet().

I know that I need to use Javascript for this. I wanted to check if anyone has experience doing something like this. To keep it clean, I would like my router to be the decision maker, and not have both Javascript and PHP making independent decisions on where to redirect. Is there any library/strategy to handle this use case that anyone has prior experience of?

Upvotes: 0

Views: 743

Answers (1)

Victory
Victory

Reputation: 5890

You can use the windows width to check for a redirect in your <head>, that redirect should set a $_SESSION variable to not show the mobile site in php.

The following method requires that the jQuery library be loaded in <head></head> and placing:

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

JS:

if ( $(window).width() > 1000 ) {
  document.location = '/no-mobile-redirect.php?set=true';
}

no-mobile-redirect.php would have

<?php
session_start();
$_SESSION['noMobileRedirect'] = isset($_GET['set']);
header("Location: /home");

Upvotes: 1

Related Questions