How Sessions Work?
When a user logs in an application, his details are usually stored in a session variable. This information is available to all pages in one application. Sessions in PHP work using a unique id for each visitor.
Starting a php session: This tag must also appear before the HTML tag.
Session_start();
Storing a session variable: Here in the sample variable value is set to 1
Session_start();
$_session[‘sample]=1;
How Sessions Work?
Session are handled in PHP by using the function session_start(). This function should be invoked before any HTML tags in the script.
Example.<?php
session_start( );
?>
<html>
<head>....... etc
A random session id is generated by the function session_start(). This session id is persisted in a cookie on the client / user computer. To refer the cookie, the reference variable $PHPSESSID is used. This is the default session id. The session id can be changed in the PHP configuration files on the server.
Even the user returns this page after visiting some pages, PHP keeps track about this session and its progress. Subsequent to this process, the session can be used as follows –
print $PHPSESSID;
session_register("username");
print $username;
The user name is registered and by using session variable name username and used it for display.