Google News
logo
PHP Program to Start a session
In the following example of PHP program to start a session :
Program :
<?php
// Start a new session or resume an existing one
session_start();

// Set session variables
$_SESSION["username"] = "john_doe";
$_SESSION["email"] = "john_doe@example.com";

// Display session variables
echo "Username: " . $_SESSION["username"] . "<br>";
echo "Email: " . $_SESSION["email"] . "<br>";

// End session
session_destroy();
?>
Output :
Username: john_doe
Email: john_doe@example.com
In this example, we use the `session_start()` function to start a new session or resume an existing one.

We then set session variables using the `$_SESSION` superglobal array. In this case, we set the "username" and "email" variables.

We use `echo` statements to display the values of these session variables.

Finally, we use the `session_destroy()` function to end the session.