Description:
PHP session is used to store and pass information from one page to another temporarily (until user close the website)
Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc).
By default, session variables last until the user closes the browser.
So; Session variables hold information about one single user, and are available to all pages in one application.
Now, let's create a new page called "session.php".
In this page, we start a new PHP session and set some session variables:
Code:
<?php
session_start(); // here session start//
?>
<?php
$_SESSION["user"] = "hai"; // session variable//
echo "Session information are set successfully.<br/>";
?>
<a href="session2.php">Visit next page</a> // its links to the session2.php page//
Description:
Next, we create another page called "session2.php".
From this page, we will access the session information we set on the first page ("session2.php")
Session2.php file code:.
<?php
session_start();
?>
<?php
echo "User is: ".$_SESSION["user"];
?>
Output:
session.php:
Output:
session2.php
PHP session is used to store and pass information from one page to another temporarily (until user close the website)
Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc).
By default, session variables last until the user closes the browser.
So; Session variables hold information about one single user, and are available to all pages in one application.
Now, let's create a new page called "session.php".
In this page, we start a new PHP session and set some session variables:
Code:
<?php
session_start(); // here session start//
?>
<?php
$_SESSION["user"] = "hai"; // session variable//
echo "Session information are set successfully.<br/>";
?>
<a href="session2.php">Visit next page</a> // its links to the session2.php page//
Description:
Next, we create another page called "session2.php".
From this page, we will access the session information we set on the first page ("session2.php")
Session2.php file code:.
<?php
session_start();
?>
<?php
echo "User is: ".$_SESSION["user"];
?>
Output:
session.php:
Output:
session2.php
No comments:
Post a Comment