5

I have a responsive website and I need some PHP conditions depending on the windows width (or media queries).

Example:

if ($window_width > 1400px) {
    echo 'Your window is wider than 1400px';
}

elseif ($window_width > 1000px) AND ($window_width < 1399px) {
    echo 'Your window is between 1000px and 1399px';
}

else {
    echo 'Your window is narrower than 1000px.';
}

Thanks for your help!

user1706680
  • 1,103
  • 3
  • 15
  • 34

5 Answers5

6

check this

Goolgle Mobile Detect

Try to use http://www.php.net/get_browser and check for isMobileDevice field. It might help only, of course, if the path to browscap.ini is set up in php.ini. If not, you can use php classes like https://github.com/garetjax/phpbrowscap

Shakti Patel
  • 3,762
  • 4
  • 22
  • 29
2

Nope you can not do it with php. php is strictly server side

user javascript instead. Below is my code to get device resolution using javascript

<script>
     screenWidth = window.screen.width,
     screenHeight = window.screen.height;
    console.log(screenWidth);
    console.log(screenHeight);
</script> 
Janak Prajapati
  • 896
  • 1
  • 9
  • 36
1

In views you can show/hide divs, something like this:

<style>
    #web {display: block;}
    #mobile {display: none;}

    @media screen and (max-width: 320px) {
        #web {display: none;}
        #mobile {display: block;}
    }
</style>

<div id="mobile">
    <?php echo "is mobile"; //include("page_mobile.phtml"); ?>
</div>

<div id="web">
    <?php echo "is web"; //include("page_web.phtml"); ?>
</div>
catalinp
  • 131
  • 2
  • 5
  • This is the best answer because it doesn't matter that the PHP is processed first. The css will take over before it's rendered anyway so it avoids the ugly show/hide after page load if you rely on JavaScript. – Vincent Jan 07 '23 at 03:27
0

I'm assuming this is for device detection. I'm not sure if you can detect window width using PHP alone. If you can then this information would appear in the HTTP headers. I would recommend using an open source PHP class built for this: http://mobiledetect.net/

Jonathon
  • 15,873
  • 11
  • 73
  • 92
  • That doesn’t help out because I also need the script detect when only the windows is resized on a desktop. – user1706680 Sep 19 '13 at 08:54
  • Then you are looking at the wrong technology. PHP is executed on the server side. You could use CSS media queries to achieve this. http://css-tricks.com/css-media-queries/ – Jonathon Sep 19 '13 at 08:55
  • I know media queries for CSS, but I need different PHP scripts to be executed depending on the window width and that’s not possible with pure CSS media queries. – user1706680 Sep 19 '13 at 08:58
  • 1
    PHP is not the right technology for this. If you want something to happen on the client side, i.e. if a user resizes their browser window, you need to use client side technology. CSS media queries, or javascript. – Jonathon Sep 23 '13 at 08:51
0

Here is the trick: Evaluate the window width with js, then load your PHP in a frame and put the width in a parameter. Your PHP script then can read that parameter and perform different conditions depending on that value.

Here is the js part:

<script type="text/javascript">
    var width = window.innerWidth;
    document.write('<iframe src="content.php?w='+width+'"></iframe>');
</script>

Within your content.php file just read the width parameter and do something with it:

<?php
    //Get width of browser window
    $width = $_GET['w'];
    echo ('width: '.$width);
?>
robix
  • 16