Sessions are another method of making data available from a page to another of a Web site.
When a visitor accesses a web site, the server assigns it an unique ID (called session id or SID). This is either stored in a cookie (with a name of PHPSESSID) on the user computer or is propagated in the URL (if the browser don't accept cookies).
Based on this SID, it is created an unique file with the session data on the server, in a folder location as set by the session.save_path in "php.ini" file.
When a session is initiate, the server stores session values in the superglobal $_SESSION variable, which can be accessed by other pages as long as the session remains active. Normally, a session remains active until the browser is closed.
<?php session_start(); // start the session // store session data $_SESSION['site'] = 'coursesweb.net'; ?> <html> ...$_SESSION['site'] can now be used in any page that calls session_start().
<?php // a.php file session_start(); // start the session // store session data $_SESSION['site'] = 'coursesweb.net'; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>a.php</title> </head> <body> This is the "a.php" page<br /> <?php echo 'Here is defined the session $_SESSION["site"], its value is: '. $_SESSION['site']; ?> <br /><br /> Link <a href="b.php">to b.php</a> </body> </html>In the same folder, create the "b.php" file with the fallowing code:
<?php // b.php file session_start(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>b.php</title> </head> <body> This is b.php page <br /> <?php echo 'The value of the $_SESSION["site"] is: '. $_SESSION['site']; // changes the value of $_SESSION['site'] and output it $_SESSION['site'] = 'marplo.net'; echo '<br /> The new $_SESSION["site"] value is: '.$_SESSION['site']; ?> </body> </html>When you open the "a.php" page, will display the fallowing result:
<?php if(session_id()=="") { session_start(); } ?>
unset($_SESSION['name']);To delete every session variable, reset the entire $_SESSION array:
$_SESSION = array();To remove all of the data associated with the current session from the server, use session_destroy():
session_destroy();- Example:
<?php session_start(); unset($_SESSION['name']); // delete the $_SESSION['name'] session_destroy(); // remove all current session data ?>
<?php session_start(); echo '<br /> The ID of the current session is: '. session_id(); ?>This code will output something similar to:
<?php session_id('123abc'); session_start(); echo '<br /> The ID of the current session is: '. session_id(); ?>This code will output:
<?php session_start(); $data = 'php-mysql'; // encrypt $data and store it in a session $hash_data = sha1($data); $_SESSION['pass'] = $hash_data; $pass = 'php-mysql'; // this could be a password sent by a user // check if we have a valid $pass if ($_SESSION['pass']==sha1($pass)) { echo 'Correct pass, its encrypted value is: '. $_SESSION['pass']; } else { echo 'Incorrect pass'; } ?>This example use the sha1() function to add some encrypted data ($hash_data) in a session, than check if the value of another variable ($pass - which could be a password sent by a user) is the same with the one added in session.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net