PHP

Sessions


 

In PHP, sessions provide a way to store and pass information from one page to another for a specific user temporarily.


A session in PHP establishes a temporary file on the server, specified by the session.save_path in the php.ini file. This file stores registered session variables and their values. Throughout a user's visit, this data remains accessible to all pages on the site. Proper configuration of the session.save_path is crucial to ensure the server can efficiently read and write session information. This mechanism facilitates the sharing of user-specific data across different pages, providing a temporary and centralized storage solution for maintaining stateful interactions in web applications.

 

A session concludes either when the user closes their browser or navigates away from the site. Alternatively, the server automatically terminates the session after a predetermined duration, often set to around 30 minutes.
 

Php Session Start:

In PHP, the session_start() function is used to initiate a session or resume the current session. It needs to be called at the beginning of every script where you want to work with session variables or utilize sessions.

 

Syntax:

<?php
		Bool session_start(void);
	<?

 

Example:

<?php
		session_start();
	<?

 

PHP $_SESSION:

In PHP, $_SESSION is a superglobal array that allows you to store and retrieve session variables. These variables can be used to maintain state information across multiple pages during a user's visit to a website or web application.


Example:

<?php
    	// starting a session
    	session_start();
	?>
	<html>
	<body>
    	<?php
    	$_SESSION["user"] = "Raj";
    	echo "Session information are Saved.<br/>";
    	?>
	</body>
	</html>

 

PHP Destroying Session:

Php session_destroy() function is used to destroy all variables of session.

 

Syntax:

<?php
		session_destroy(void);
	<?

 

Example:

<?php
		session_start();
		session_destroy();
	?>