PHP – Sessions,An opportunity way to make information accessible across the numerous pages of an entire website is to use a PHP Session.
A consultation creates a record in a brief directory on the server wherein registered consultation variables and their values are stored. This information might be available to all pages on the website at some point of that visit.
The vicinity of the temporary file is decided by means of a setting inside the Hypertext Preprocessor.Ini record known as consultation.Save_path. Before using any consultation variable make certain you have setup this direction.
When a session is began following matters manifest −
- PHP first creates a unique identifier for that particular session that is a random string of 32 hexadecimal numbers inclusive of 3c7foj34c3jj973hjkop2fc937e3443.
- A cookie referred to as PHPSESSID is automatically despatched to the user’s computer to save specific session identification string.
- A document is automatically created at the server in the specified brief listing and bears the name of the unique identifier prefixed through sess_ ie sess_3c7foj34c3jj973hjkop2fc937e3443.
When a PHP script desires to retrieve the fee from a consultation variable, PHP routinely gets the particular session identifier string from the PHPSESSID cookie after which looks in its temporary listing for the file bearing that call and a validation may be completed via comparing both values.
A consultation ends when the person loses the browser or after leaving the website, the server will terminate the session after a predetermined period of time, generally 30 minutes duration.
PHP – Sessions,Starting a PHP Session
A PHP consultation is easily started by means of making a name to the session_start() function.This function first assessments if a consultation is already began and if none is commenced then it starts offevolved one. It is suggested to position the decision to session_start() at the beginning of the web page.
Session variables are stored in associative array referred to as $_SESSION[]. These variables may be accessed throughout lifetime of a session.
The following example starts a session then check in a variable known as counter this is incremented each time the page is visited all through the session.
Make use of isset() function to test if session variable is already set or now not.
Put this code in a take a look at.Personal home page document and load this document frequently to see the result
<?php
session_start();
if( isset( $_SESSION['counter'] ) ) {
$_SESSION['counter'] += 1;
}else {
$_SESSION['counter'] = 1;
}
$msg = "You have visited this page ". $_SESSION['counter'];
$msg .= "in this session.";
?>
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
</html>
It will produce the following result −
You have visited this page 1in this session.
Destroying a PHP Session
A PHP consultation can be destroyed through session_destroy() characteristic. This function does not need any argument and a unmarried call can spoil all of the session variables. If you want to break a single session variable then you can use unset() feature to unset a session variable.
Here is the example to unset a single variable −
<?php
unset($_SESSION['counter']);
?>
Here is the call which will destroy all the session variables −
<?php
session_destroy();
?>
PHP – Sessions,Turning on Auto Session
You do not need to name start_session() feature to begin a session while a consumer visits your website online if you may set session.Auto_start variable to one in Hypertext Preprocessor.Ini file.
Sessions without cookies
There can be a case while a user does now not allow to keep cookies on their device. So there is any other method to ship consultation ID to the browser.
Alternatively, you can use the consistent SID that is defined if the consultation commenced. If the consumer did now not send the best session cookie, it has the form session_name=session_id. Otherwise, it expands to an empty string. Thus, you can embed it unconditionally into URLs.
The following instance demonstrates how to sign in a variable, and how to hyperlink correctly to every other web page the usage of SID.
<?php
session_start();
if (isset($_SESSION['counter'])) {
$_SESSION['counter'] = 1;
}else {
$_SESSION['counter']++;
}
$msg = "You have visited this page ". $_SESSION['counter'];
$msg .= "in this session.";
echo ( $msg );
?>
<p>
To continue click following link <br />
<a href = "nextpage.php?<?php echo htmlspecialchars(SID); ?>">
</p>
It will produce the following result −
You have visited this page 1in this session.
To continue click following link