PHP - Screen Resolution - City of Bath College Moodle

advertisement
Easy Screen Resolution
This tutorial explains how the user’s screen resolution can be obtained and written to a log file on the
server (information on screen size is useful for optimising web pages to fit the user’s screen).
Screen resolution can be found using the following JavaScript variables:
screen.width
screen.height
To set these values as cookies, assign these values to 2 cookie names (e.g. screenwidth and screenheight)
<script>
document.cookie = "screenwidth = " + screen.width;
document.cookie = "screenheight = "+ screen.height;
</script>
screenwidth
screenheight
cookie name
screen.width screen.height
cookie value
PHP
PHP can access the values of the cookies created in JavaScript using the following expressions:
$_COOKIE['screenwidth']
$_COOKIE['screenheight']
Assign PHP variables (so the log file can be written to the server):
$width = $_COOKIE['screenwidth']
$height = $_COOKIE['screenheight']
Use PHP functions fopen and fwrite to write the values to a log file:
$logfile = "logfile.txt";
$handle = fopen($logfile, 'w');
$width = $_COOKIE["screenwidth"];
$height = $_COOKIE["screenheight"];
fwrite($handle, "The screen dimensions are: ".$width." * ".$height."px");
In order for this to work, you need to press the F5 function key to refresh the page, as the browser needs
to send the cookie data back to the server. To avoid having to refresh the page manually every time, add
a JavaScript refresh function to reload the page, automatically, as soon as the cookies have been set:
document.location.reload()
An IF statement to test if the $_COOKIE values have been set is required to output the cookie values to a
log file and prevent the page repeatedly setting cookies and reloading.
if(isset($_COOKIE['screenwidth']) AND isset($_COOKIE['screenheight']))
Full source code
Here is a listing of the full “hybrid” code (includes PHP and JavaScript).
1. An IF statement checks to see if the cookies containing the screen resolution exist. If the cookies do
exist, the width and height are written to a log file (and echoed to the screen confirming it has
worked).
2. Otherwise an else statement to set the cookies and reload the page is activated.
3. At the end of the else statement, a JavaScript page reload function ensures the cookie variables are
available so the IF statement in step 1 above works.
4. Note how to step in and out of the PHP source using { ?> and <?php }
<?php
if(isset($_COOKIE["screenwidth"]) AND isset($_COOKIE["screenheight"]))
{
$width = $_COOKIE["screenwidth"];
$height = $_COOKIE["screenheight"];
echo "User resolution: ".$width."x".$height;
$logfile = "logfile.txt";
$handle = fopen($logfile, "w"); //w = write only and create a new file if it
doesnt exist
fwrite($handle, "The screen dimensions are: ".$width." * ".$height."px");
echo "<p><a href='logfile.txt'>Click here to open the log file</a>";
}
else
{
?>
<script>
document.cookie = "screenwidth = " + screen.width;
document.cookie = "screenheight = "+ screen.height;
document.location.reload(); //Reload the page to access cookies
</script>
<?php
}
?>
Several predefined variables in PHP, such as $_REQUEST and $_SESSION, are superglobals, which they
can be accessed from anywhere.
$_SESSION Array Variables
A session stores variables to be used across multiple pages. Session variables can be retrieved them from
the session opened at beginning of each page.
Session variables hold variables or information such as screen name, selected items for a shopping basket,
s. and are available to all pages in one application.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The
UID is either stored in a cookie or is propagated in the URL. A PHP session can temporarily store user
information on the server.
Starting a PHP Session
Register the user's session with the server, then start saving user information, and the UID for that user's
session. The session_start() function must appear BEFORE the <html> tag:
<?php session_start(); ?>
Storing a Session Variable
Use the PHP $_SESSION variable to store data then retrieve the session data, e.g.:
<?php
session_start();
$_SESSION['views']=1;
//store the data
?>
<html>
<body>
<?php
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
//retrieve session data
Example – Page Counter
The isset() function checks if the "views" variable has already been set. If "views" has been set, increment
counter by 1. If "views" doesn't exist, create a "views" variable, and set it to 1:
<?php
session_start();
if(isset($_SESSION['views'])) {
$_SESSION['views']=$_SESSION['views']+1;
}
Else {
$_SESSION['views']=1;
}
echo "Views=". $_SESSION['views'];
?>
Destroying a Session
To delete any session data, use unset() to free a specified session variable or session_destroy() to
destroy or reset the session loosing all stored data:
Unset():
<?php
session_start();
if(isset($_SESSION['views']))
unset($_SESSION['views']);
?>
session_destroy()
<?php
session_destroy();
?>
PHP $_REQUEST
The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.
PHP $_REQUEST can be used to collect data after submitting an HTML form. When a user clicks a "Submit"
button, form data is sent to the file specified in the action attribute of the <form> tag. In this example, we
point to this file itself for processing form data. If you wish to use another PHP file to process form data,
replace that with the filename of your choice. Then, we can use the super global variable $_REQUEST to
collect the value of the input field:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
$name = $_REQUEST['name'];
echo $name;
?>
Reading screen width and height using $_REQUEST
The $_REQUEST and $_SESSION variables can be used to find/store the screen resolution, stored as
session variables ($_SESSION['screen_width'] and ($_SESSION['screen_height']





Use JavaScript to detect screen width and height (last else statement in the code below). Output
this to the address bar
$_REQUEST can be used to get these values from the address bar (2nd else statement below)
The PHP script header('Location: ' . $_SERVER['PHP_SELF']); refreshes the page by
loading itself again. This allows the server to obtain the values for screen width and height which
are only local
The first if statement echoes the user’s screen resolution to screen becaue the $_SESSION variable
is now set.
The values can be written to a file (fwrite) or stored as a cookie.
<?php
session_start();
if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height']))
{
echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' .
$_SESSION['screen_height'];
}
else if(isset($_REQUEST['width']) AND isset($_REQUEST['height']))
{
$_SESSION['screen_width'] = $_REQUEST['width'];
$_SESSION['screen_height'] = $_REQUEST['height'];
header('Location: ' . $_SERVER['PHP_SELF']);
}
else
{
echo '<script type="text/javascript">window.location = "' .
$_SERVER['PHP_SELF'] .
'?width="+screen.width+"&height="+screen.height;</script>';
}
?>
Download