Session help?

I’m trying to store the user’s email, using a session to use across multiple pages.

Scenario: A user registers with their credentials, and is stored in the customer table. The user email is the PK of the table and therefore is unique. On success, the user is redirected to a booking page where the user can go ahead and make a booking. The user email is needed to reference the customer when making the booking, for the booking table (being the FK). “is needed to carry forward to the booking page.” - from tasks.

[php]

<?php session_start(); ?> BookMe

// HTML form code


.

<?php // user input validations - working fine // connect to server // pre check select statement to see if user email exists // if fails, error message is displayed // else (success), else{ // insert statement into customer table //$query = mysqli_query($DBConnect, "INSERT INTO customer(cust_name, cust_pass, cust_email, cust_phone) VALUES ('$userName', '$userPassword1', '$userEmail', '$userPhone')"); // if $userEmail matches then register a session and redirect to booking.php //$userCheck = mysqli_query($DBConnect, "SELECT * FROM customer WHERE cust_email = '" . $userEmail . "'"); $_SESSION['userEmail'] = $userEmail; echo "Registration Success, '".$_SESSION['userEmail']."'."; echo "Please wait while we redirect you to the Booking Page"; // if successful redirect to booking.php page header('Location: booking.php'); } // close database mysqli_close($DBConnect); [/php] Where I currently have the session variable storing the user email, it's working. How do I get this to work across multiple pages, i.e the booking page. Is this being stored in the right place?

Are you starting a new session on the other pages?

[php]<php
session_start();
echo $_SESSION[‘userEmail’];
?>[/php]
That should be all you need.

Yes, I’m starting sessions on the other pages. When do I need to destroy the session? For instance if a new user registers after them, is the new users email stored in the session variable, or do I have to destroy the old variable?

When the user logs out.

A PHP session is bound to a single instance, that’s what the PHPSESSID cookie is for. If two users visit your site they will get two different session instances and two different PHPSESSID hashes.

Sponsor our Newsletter | Privacy Policy | Terms of Service