SHARE
Facebook X Pinterest WhatsApp

PHP Tutorial: Sessions

Written By
thumbnail
Vince Barnes
Vince Barnes
Feb 22, 2005

There is a relationship between Sessions and Cookies — they serve somewhat
the same purpose, and are, to a certain extent, usable interchangeably. 
Sessions, which were integrated into PHP in version 4 of the language, are a
means to store and track data for a user while they travel through a series of
pages, or page iterations, on your site.

The most significant differences between the two are that cookies are stored
on the client, while the session data is stored on the server.  As a
result, sessions are more secure than cookies (no information is being sent back
and forth between the client and the server) and sessions work even when the
user has disabled cookies in their browser.  Cookies, on the other hand,
can be used to track information even from one session to another by setting
it’s time( ) parameter (see

https://www.htmlgoodies.com/php/p14cookies.html
)

How Sessions Work

Sessions in PHP are started by using the session_start( ) function. 
Like the setcookie( ) function, the session_start( ) function must come before
any HTML, including blank lines, on the page.  It will look like this:

<?php
session_start( );
?>
<html>
<head> ……. etc

The session_start( )
function generates a random Session Id and stores it in a cookie on the user’s
computer (this is the only session information that is actually stored on the
client side.)  The default name for the cookie is PHPSESSID, although this
can be changed in the PHP configuration files on the server (most hosting
companies will leave it alone, however.)  To reference the session Id in
you PHP code, you would therefore reference the variable $PHPSESSID (it’s a
cookie name; remember that from Cookies?)

Your sharp mind may be
wondering what happens when you come to the second pass through your page and
reach the session_start( ) function again.  PHP knows that there is already
a session on progress and so ignores subsequent instances of the session_start(
)   — phew!!

Using Session Data

Having established a
session, you can now create, store and retrieve information pertaining to that
session.  You might want, for example, to keep track of items in your
visitor’s shopping cart.  Information for sessions is stored in a special
directory on the server; the path of that directory is specified in the server’s
PHP configuration files.

Information to be stored
for a session is kept in session variables.  Session variables are created
by registering them for the session, using the session_register( ) function. 
To use that information (on any page iteration in the session) you simply
reference the variable just like you would any other variable.  Here’s an
example:

<?php
session_start( );
?>
<html>
<head>

<title>Using a session
variable</title>

</head>

<body>

<?php

print "Welcome to
session number: ";

print $PHPSESSID;

?>

<br />

<?php

session_register("username");

$username = "Goody";

print "Your name is: ";

print $username;

?>

</body>

</html>

In this example we have
created a session and displayed the session number.  We then registered a
session variable called username (notice the quotes around the variable’s name
in the call to the session_register( ) function.)

Next we assigned a value
to that variable with the " = " assignment operator (remember operators from

https://www.htmlgoodies.com/php/p05expressions.html
?) and then displayed the
value of that session variable.

We now have all the
basic tools to establish a session, and to create and use variables that last
through the entire duration of the session.

 

Continue to the next part of this Tutorial


Return to the Tutorial Series Index

Recommended for you...

PHP vs Javascript: Compare Web Development Languages
How Laravel Speeds Up App Development
Rob Gravelle
Aug 29, 2018
7 Ways to Start Learning JavaScript as a WordPress Developer
Exploring PhpSpreadsheet’s Formatting Capabilities
Rob Gravelle
Feb 26, 2018
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.