1

I have 2 page index.php (For Desktop) and indexm.php (For Mobile). I want to redirect it with detection of screen resolution size. I'm trying this code

<script type="text/javascript">
    if (screen.width <= 720) {
        window.location = "indexm.php";
    } else {
        window.location = "index.php";
    }
</script>

and i am trying same in PHP,

<?php 
  if (screen.width <= 720) {
      include "indexm.php";
  } else {
      include "index.php";
  }
?>

But both the code is not working, Please help me with PHP or JavaScript code to make it work.

Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
Maurya Ashish
  • 39
  • 1
  • 1
  • 6
  • How are you testing it? during initial load or while resizing? – brk Nov 13 '18 at 10:19
  • what is screen here ... ? – Negi Rox Nov 13 '18 at 10:20
  • @brk Initial load – Maurya Ashish Nov 13 '18 at 10:23
  • @NegiRox Screen size can be any and it differ but normally we know desktop screen is big and mobile is always less then 800 so in this logic, i am placing to change the index page for mobile user and desktop user. – Maurya Ashish Nov 13 '18 at 10:25
  • according to it should work properly using javascript. – Negi Rox Nov 13 '18 at 10:29
  • @NegiRox I see it working in JS after small changes but still PHP one is not working. Do you have any solution for PHP? – Maurya Ashish Nov 13 '18 at 10:38
  • PHP runs on the server, it knows nothing about the screen resolution of the client, see: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming - and you can't just stick `` around some JavaScript code; it's a completely different language. You'd probably be better off just creating a responsive design with CSS anyway. – CD001 Nov 13 '18 at 10:43

1 Answers1

0

For JavaScript:

change window.location to window.location.href = ('indexm.php');

For php I think using header is the best:

header('Location: indexm.php');
Matnex Mix
  • 49
  • 8