60 likes | 79 Views
PHP Session provides the facility for storing the information which can be used across the webpages.
E N D
PHP Session: PHP Session provides the facility for storing the information which can be used across the webpages. Session In PHP is that if you log in some site and spend some time on it and after some time you logged out from site then time duration from login and logout is nothing but it is a session. In PHP you can start a session by using the session_start() function and can use the session for storing the information in the session variables using $_SESSION[“variableName”]; How to start PHP sessions: session_start() is used to start the PHP session. Here is the example of session in php is as follows: <?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["user"] = "king"; $_SESSION["pass"] = "123"; echo "Session are set."; ?> </body> </html>
The output is as follows: How to get PHP session values: You can get all the session values on any php page. The following example shows you the demo: <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Echo session variables that were set on previous page echo "user name is : " . $_SESSION["user"] . ".<br>"; echo "password is : " . $_SESSION["pass"] . ".";
?> </body> </html> The output is as follows: Another way to display session value: Print_r($_SESSION); is used to print all the session variable values. The example of this is as follows: <?php session_start(); ?> <!DOCTYPE html> <html> <body>
<?php print_r($_SESSION); ?> </body> </html> The output is as follows: Modify the session variable values: You can change the value for session variables. The example to change the session variables are as follows: <?php session_start(); ?> <!DOCTYPE html> <html>
<body> <?php // to change a session variable $_SESSION["user"] = "varshney"; print_r($_SESSION); ?> </body> </html> Here we have changed the user session value. Now output is as follows: How to destroy the session: Session_unset() function used for removing session variables and session_destroy() is used to remove the session. The example shows you how to remove the session:
<?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // it remove all session variables session_unset(); // it destroy the session session_destroy(); ?> </body> </html> For more information visit: https://www.bebee.com/producer/@eit-world/php- session-how-to-start-sessions-in-php