PHP - How Sessions Work?

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.
PHP - PHP Superglobals
PHP Superglobals - These are predefined variables that are by default available globally..
PHP - What is the difference between mysql_connect() and mysql_pconnect()?
What is the difference between mysql_connect() and mysql_pconnect()? - When mysql_pconnect() is used, the function initially tries to find an open persistent connection....
PHP - Explain with code how to get a user's IP address.
Explain with code how to get a user's IP address - The users IP address can be returned by specifying the host name as aparameter in gethostbyname(“hostname”) method....
Post your comment