Reputation: 1
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>
Upvotes: 0
Views: 335
Reputation: 497
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;
?>
Upvotes: 1