Home
XHTML
CSS
PHP
MySQL
SEO
JavaScript
Computer Basics
Number Systems
LINUX


PHP Sessions and Cookies

PHP | Values | Operators | Functions | Arrays | Forms | Sessions-Cookies | Classes

A session is a small file that a server stores on the user's browser to maintain state until the browser is closed. A session can maintain a username and password, shopping cart items, and other types of information. Session statements are coded on the first line of a PHP file before the XHTML declaration. The  session_start()  function declares the session...

<?php

session_start();

?>


The  session_start()  function automatically creates a unique id number when a user visits the website and can be tracked from page to page. When the browser refreshes or goes to another page, data is sent back to the server and accessed in a global array called $_SESSION...

<?php

session_start();

$_SESSION['views']=1;

echo "Pageviews = " . $_SESSION['views'];

?>

The output is...

Pageviews = 1




The  isset()  function evaluates if a session has been declared. Each time the browser refreshes, the  if()  function evaluates true and the counter increments by one...

<?php

session_start();

if(isset($_SESSION['views']))

$_SESSION['views']=$_SESSION['views'] + 1;
else
$_SESSION['views']=1;

echo "views = ". $_SESSION['views'];

?>

If the browser is refreshed again, the output is...

Pageviews = 2




The  unset()  function removes the contents of a session...

<?php

session_start();

if(isset($_SESSION['views']))
unset($_SESSION['views']);

?>


The  session_destroy()  function deletes a session completely...

<?php

session_destroy();

?>





A cookie is like a session, but is stored on the user's computer instead of the browser and can maintain state over a specified length of time even after the browser is closed. Cookie statements are coded on the first line of a PHP file before the XHTML declaration and passed in the URL. A cookie stores a name and value that becomes available as a variable after the browser is refreshed or goes to another page. The  setcookie()  function declares the cookie name and value, as well as an expiration time. This cookie uses the  time()  function to expire one hour from activation...

<?php

setcookie('name', 'value', time()+3600);

?>


Cookie data is accessed in a global array called $_COOKIE...

<?php

$_COOKIE['name'];

echo "This is a " . $_COOKIE[name] . ".";

?>

The output is...

This is a value.




The  isset()  function evaluates if a cookie has been set...

<?php

if (isset($_COOKIE['name']))
echo 'Welcome ' . $_COOKIE['name'];
else
echo 'Welcome stranger!';

?>

If the cookie is not set, the output is...

Welcome stranger!



A cookie can store multiple names and values...

<?php

setcookie('name', 'value1');
setcookie('school', 'value2');
setcookie('subject', 'value3');

echo $_COOKIE['name'];
echo $_COOKIE['school'];
echo $_COOKIE['subject'];

?>


A cookie can be deleted by declaring a negative expiration time...

<?php

setcookie('name', '', time()-1);

?>


An alternative method to deleting a cookie is to declare another cookie statement with the same name...

<?php

setcookie('name');

?>


The  mktime()  function returns a date and time in standard format in the order of hour, minute, second, month, day, and year. If a value is not declared, it will not be included. This cookie expires at 3 a.m. on July 1, 2008...

<?php

setcookie('name', 'value', mktime(3,0,0,7,1,2008));

?>


Sessions are recommended over cookies. Sessions work even if the user has cookies disabled on their computer. A MySQL database is recommended for storing information over a long period of time.




SEO Vancouver, Washington
SEO Portland, Oregon
Website Design Vancouver, Washington