DEV Community

Cover image for Session in PHP with an example
ncutixavier
ncutixavier

Posted on

Session in PHP with an example

Sessions in PHP are a mechanism to store user-specific information across multiple web pages. This is useful when you need to track a user's state within your application, such as keeping items in a shopping cart or remembering their login status.

Here's how sessions work in PHP:

  1. Starting a Session: You initiate a session using the session_start() function. This function either retrieves an existing session associated with the user (identified by a session ID) or creates a new session if one doesn't exist.

  2. Storing Session Data: You can store data related to the user in the session using the associative array $_SESSION. This array acts like a container where you can assign key-value pairs. For example, $_SESSION["username"] = "johnDoe"; stores the username "johnDoe" under the key "username".

  3. Accessing Session Data: On subsequent pages within the same session, you can access the stored data using the $_SESSION array. The values can be used for various purposes, like personalizing content or remembering user preferences.

  4. Destroying Sessions: Sessions can expire automatically when the user closes the browser or after a specific period of inactivity. You can also explicitly destroy a session using the session_destroy() function.

Here's an example to illustrate these concepts:

login.php:

<?php
session_start();

if (isset($_POST['username']) && isset($_POST['password'])) {
  // Simulate successful login (replace with actual authentication logic)
  $_SESSION['username'] = $_POST['username'];
  header('Location: profile.php');
  exit;
}
?>

<!DOCTYPE html>
<html>
<body>
<form method="post">
  Username: <input type="text" name="username" required><br>
  Password: <input type="password" name="password" required><br>
  <button type="submit">Login</button>
</form>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

profile.php:

<?php
session_start();

if (!isset($_SESSION['username'])) {
  header('Location: login.php');
  exit;
}

$username = $_SESSION['username'];
?>

<!DOCTYPE html>
<html>
<body>
  Welcome, <?php echo $username; ?>! This is your profile page.
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this example:

  • login.php checks for submitted login credentials. If valid (replace with actual authentication), it stores the username in the session ($_SESSION['username']) and redirects to profile.php.
  • profile.php starts the session and checks if a username is present in the session. If not, it redirects back to the login page. If a username exists, it welcomes the user by name using the retrieved session data.

This is a basic example of using sessions in PHP. You can extend this concept to store various user-specific information and manage user state across your web application. Remember to always implement proper security measures when handling user data in sessions.

Top comments (0)