- 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.
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