0

I found some jquery and ajax code that gets the browser window width and height. I do not know jquery or ajax, but I was very pleased to get it working, sort of. The script below works perfectly with a link to the next script that needs to run. Both $_SESSION variables are set properly and available. When I link to score.php through a header redirect, the $_SESSION variables apparently are not set because they are null in score.php. Timing issue?

I really do not want to have a link that the user has to click to continue. (These scripts are part of a system that requires a user to be "logged in".) Is there an easy solution to redirect to score.php so that the browserWidth and browserHeight values are available? I am ok with either accessing them as $_SESSION variables or as parameters (score.php?w=$browserWidth&h= . . .).

<?php   
session_start();  
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
</head>  
<body>  
<?php  
/******************************************* 
/ code from http://holden.pro/test/test2.php   
/******************************************/   
if(!isset($_POST['width']) || !isset($_POST['height'])) {  
  echo '  
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  
  <script type="text/javascript">
  $(document).ready(function () {
    var height = $(window).height();
    var width = $(window).width();
    $.ajax({
      type: \'POST\',
      url: \'get_window_size.php\',
      data: {
        "height": height,
        "width": width
      },
      success: function (data) {
        $("body").html(data);
      },
    });
  });
  </script>
  ';
}

$_SESSION['browserWidth'] =  $_POST['width'];  
$_SESSION['browserHeight'] =  $_POST['height'];  

echo '<a  href="score_image.php">Score Image</a>';    // THIS WORKS!

/*  THIS DOES NOT WROK
$location = 'Location: score_image.php';  
header("$location");  
header("content-type: text/html; charset=utf-8");  
*/
?>  
</body>  
</html>

1 Answer 1

1

PHP Header - Manual <-- Read for better understanding. Try the code below, it will append the width and height to the URL.

<?php
$width = $_POST['width'];
$height = $_POST['height'];


$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'score_image.php?h='.$height.'&w='.$width;
header('Location: http://'.$host.$uri.'/'.$extra);
exit;
?> 
Sign up to request clarification or add additional context in comments.

1 Comment

Your header is certainly better built than mine, however, height and width continue to be null in score.php. I do not think the problem is with the header; rather it seems to be a timing problem. If the script is paused with a link, the width and height can be successfully passed on. If the width and height assignment are immediately followed by the header, it is as if there were never set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.