How can we create a session in php?
A PHP session stores information about a user on the server, so you can use it on many pages. Start it with session_start() and save the data in the $_SESSION array.
<?php
// page1.php
session_start();
$_SESSION["username"] = "Robin";
echo "Session created.";
?>
<?php
// page2.php
session_start();
echo "Hello " . $_SESSION["username"];
?>
Output:
When page2.php is opened after page1.php
Hello Robin
Call session_start() at the top of every page that uses the session, before any output. Read PHP Session for login examples and security.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |