Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:48:03+00:00 2026-06-02T22:48:03+00:00

I am trying to server device specific layout and also content based on the

  • 0

I am trying to server device specific layout and also content based on the browser viewport. I have a heavey site and I am not using Media Queries for some specific reasons primarily page load speeds. I need to accomplish this task in this manner only.

OK the situation is like this…..

I have 2 files index.php and viewport.php both residing in a folder http://localhost/test/

To get the browser viewport and convert the values to PHP variable, I am using the following script:

<?php
if (isset($_GET['width'])) {
  $layoutType = $_GET['width'];

      if ( $layoutType >= 240 && $layoutType <= 600 ) {
        $layoutType = 'mobile';
      } else if ($layoutType >= 601 && $layoutType <= 900 ) {
          $layoutType = 'tablet';
      } else {
         $layoutType = 'desktop';
      }
} else {
  echo "<script language='javascript'>\n";
  echo "  location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
            . "&width=\" + document.documentElement.clientWidth;\n";
  echo "</script>\n";
  exit();
}
?>

My questions:

Q1. How can I store $layoutType as a Session Variable so that I can use it multiple times? What would be the correct syntax for it?

Q2. How can I run the file viewport.php from index.php and get only the $layoutType and no other data. This is because by using the <?php require_once ?> method I am getting my URL like this:http://localhost/test/index.php?&width=1600. I want the URL to display like http://localhost/test/ only. What will be the correct syntax I need to use in my index.php file?

Q3. Skipping the Ajax part is there a way to get rid of index.php?&width=1600 from the URL? I tried via .htaccess but it gives me the error saying “The page is not redirecting properly”

Please note: I do not intend to use and JavaScript Libraries like jQuery and MooTools as this is the only JavaScript function in my entire website and it wont make sense to load an entire library for it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:48:03+00:00Added an answer on June 2, 2026 at 10:48 pm

    Firstly, PHP happens server side, so once the page loads the only way to use PHP again (without loading a page) is to make an ajax call, presumably to another php page that performs a certain function and returns a value.

    If you want to store a value as a session variable so that it can be used continuously, you can do the following:

    1. when they first land on your site have php check for the existence of a session and if it does not exist, call a Javascript function to get the layoutWidth and make an ajax call to a php page that will store it as a session variable and then reload the page and include the correct layout file.

    I would probably not use this method and actually look at ways to do this with JavaScript/JQuery instead. But this is how you have asked to do it.

    For your example, I have not used JQuery at all, but I would as the include is only about 19kb or so and it makes life SO much easier.

    Example:

    index.php

    <?php
       session_start();
    ?>
    <!DOCTYPE html>
    <html>
       <head>
          <title></title>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <?php 
             if (empty($_SESSION['layoutWidth'])) {
                echo '<script type="text/javascript"> var session=false; var layoutWidth;</script>';
             } else {
                echo '<script type="text/javascript"> var session=true; var layoutWidth=' . $_SESSION['layoutWidth'] . ';</script>';
             }
          ?>
          <script type="text/javascript" src="js/viewport.js"></script>
       </head>
       <body>
             <?php
    
                if (!empty($_SESSION['layoutWidth'])) {
                   $layoutWidth = $_SESSION['layoutWidth'];
                   if ( $layoutWidth >= 240 && $layoutWidth <= 900 ) {
                      include('layout1.php');
                   } else if ($layoutWidth > 900 && $layoutWidth <= 1200 ) {
                      include('layout2.php');
                   } else {
                      include('layout3.php');
                   }
                }
             ?>
       </body>
    </html>
    

    js/viewport.js

    // JavaScript Document
    
    window.onload = function() {
        // if there is no session (session = false)
        if (!session) {
            // call function to get the screen size
            getScreenWidth();
    
            // make ajax call to php page to set the session variable
            setSession();
        }
    }
    
    function getScreenWidth() {
        layoutWidth = screen.width;
    }
    
    function setSession() {
        var xmlhttp;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            // Reload the page
            window.location.reload();
            }
          }
        xmlhttp.open("POST","set_session.php?layoutWidth=" + layoutWidth,true);
        xmlhttp.send();
    }
    

    your php file to set the session the parameter is passed from your ajax call:

    set_session.php

    <?php
        session_start();
        // you can check if it is already set if you like otherwise:
        $_SESSION['layoutWidth'] = $_REQUEST['layoutWidth'];
    ?>
    

    layout1.php

    <?php
       echo '<div>This is layout1.php</div>';
       echo '<div>Your screen width is: ' . $_SESSION['layoutWidth'] . '</div>';
    ?>
    

    layout2.php

    <?php
      echo '<div>This is layout2.php</div>';
      echo '<div>Your screen width is: ' . $_SESSION['layoutWidth'] . '</div>';
    ?>
    

    layout3.php

    <?php
       echo '<div>This is layout3.php</div>';
       echo '<div>Your screen width is: ' . $_SESSION['layoutWidth'] . '</div>';
    ?>
    

    This method means you dont have to pass parameters around in your URL. it will all be hidden.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send an image using a Java server to my Android device
Trying to get my server to serve my rails apps. I have a test
Iam trying to create an iterative server based on datagram sockets (UDP). It calls
I'm trying to build a client server application using POSIX shared memory and POSIX
I'm trying to implement sql server 2005 cache dependency in ASP.Net 3.5. I have
I'm trying to install Team Foundation Server using 2 machines: Machine1 : OS: Windows
I am trying to connect an iOS device with a server trough TCP. I
I am trying to see if using a custom index for a specific type
I am trying to upload file to a php server from my android device.
I'm trying to connect to a server using https and when I do it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.