A Guide to Saving Variables to Session and Accessing Them Across Multiple Pages

Photo by Matt Duncan on Unsplash

A Guide to Saving Variables to Session and Accessing Them Across Multiple Pages

In web development, it's common to need to store and access variables across different pages. One way to achieve this is by using PHP sessions. In this blog post, we will explore how to save variables to a session and access them from different pages, specifically focusing on two PHP pages: page1.php and page2.php. Additionally, we will implement a scenario where user input from page1.php is saved to a database and show a success page (success.php) after successful submission.

Part 1: Saving Variables to Session

To save a variable to a PHP session, you need to follow these steps:

  1. Start a session: At the very beginning of both page1.php and page2.php, add the following code to start a session:
<?php
session_start();
?>
  1. Set a session variable: On page1.php, create a form that allows users to input data. When the form is submitted, save the input to a session variable. Here's an example:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $_SESSION['user_data'] = $_POST['user_data'];
    // Redirect to page2.php
    header("Location: page2.php");
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Page 1</title>
</head>
<body>
    <form method="post" action="">
        <label for="user_data">User Data:</label>
        <input type="text" name="user_data" id="user_data">
        <input type="submit" value="Submit">
    </form>
</body>
</html>
  1. Accessing the session variable: On page2.php, you can retrieve the session variable set in page1.php and use it in various ways. For example, you can display it or insert it into a database. Here's an example:
<?php
session_start();

if (isset($_SESSION['user_data'])) {
    $user_data = $_SESSION['user_data'];
    // Insert $user_data into a database here
    // Redirect to success.php after successful insertion
    header("Location: success.php");
    exit();
} else {
    // Handle the case where the session variable is not set
    echo "Session variable 'user_data' not found.";
}
?>

Part 2: Inserting Data into a Database

In the example above, we have mentioned inserting data into a database. To do this, you can use PHP and a database management system like MySQL. Make sure you have a MySQL database set up and configured in your project.

  1. Establish a database connection: At the beginning of page2.php, you should establish a database connection using PHP's mysqli extension or any other database library you prefer.

  2. Insert data into the database: Once you have the database connection, you can use SQL queries to insert the data from the session variable ($user_data) into your database. Make sure to handle errors and security measures like prepared statements to prevent SQL injection.

Part 3: Success Page

After successfully inserting the data into the database, you can redirect the user to a success.php page to confirm the submission.

Here's a simple example of what success.php might look like:

<!DOCTYPE html>
<html>
<head>
    <title>Success Page</title>
</head>
<body>
    <h1>Data Successfully Submitted</h1>
    <!-- You can provide a link to return to the first page or any other relevant actions. -->
    <a href="page1.php">Return to Page 1</a>
</body>
</html>

With this setup, you can save user data to a session variable on page1.php, insert it into a database on page2.php, and display a success message on success.php after a successful submission. This approach allows you to maintain data across multiple pages in a secure and efficient way.