How does Sessions Work in php?
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( );
?>The session_start( ) generates a random Session Id and stores it in a cookie on the user’s browser(this will be only stored on the client browser.) The default name for the cookie is PHPSESSID, although this can be changed in the PHP configuration files on the server. To reference the session Id in you PHP code, we reference the variable $PHPSESSID (it’s a cookie name; remember that from Cookies?)
We can think when we come to the second pass through our page and reach the session_start( ) the second time . PHP knows that there is already a session is on progress and so that it ignores many instances of the session_start( ) function.
How to Use Session Data?
As we have created a session, we can now create, store and retrieve information relating to that session. Session variables are created by registering them for the session, using the session_register( ) function. Here’s an example using session register:
<?php
session_start( );
?>
<html>
<head>
<title>we are using session register</title>
</head>
<body>
<?php
print “here is the session id: “;
print $PHPSESSID;
?>
<br />
<?php
session_register(”name”);
$name = “Suresh”;
print “this is your name in session: “;
print $user;
?>
</body>
</html>
The name variable will have the value “suresh”.
Posted by
Suresh B