PHP Session not received by new page...

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
HuuD
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Sep 23, 2017 7:16 pm

Mon Sep 25, 2017 4:23 am

Hi All,

I have this issue where the PHP session stored in one page is not received in the new page, I hvae set the session_start(); at the very top of the page and funnily there is no error but the data does not show on the new page, please find the code below as Im not able to understand whats going wrong here, Thanks.

The error I receive :

Notice: Undefined index: sesName in D:\Softwares\Xampp\htdocs\Assignment1\booking.php on line 3

But when I check the the echo of the session in the page that priginated, there is data, also note that all the 3 codes below are from different pages.

Session data sending page :

Code: Select all

 while ($row = mysqli_fetch_assoc($result))
	{
		$dbemail = $row["email"];
		$dbpass = $row["password"];
	}
                            
		if(($lemail == $dbemail) && ($lpass == password_verify($lpass, $dbpass)))
                	{
					$_SESSION['sesName'] = $dbemail;
					echo $_SESSION['sesName'];
					header("Location:booking.php");
Session data receiving page :

Code: Select all

<?php
session_start();
echo $_SESSION['sesName']
?>

<!DOCTYPE html>
<html lang="en">
<body id="admin">
<head>
	<link href="css/bootstrap.min.css" rel="stylesheet">
	<link href="css/datepicker3.css" rel="stylesheet">
	<link href="css/styles.css" rel="stylesheet">
</head>

<body>
	<div class="row">
        <div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-2 col-md-4 col-md-offset-4">
			<div class="login-panel panel panel-default">
                <div class="panel-heading"><p class="text-center">CABSONLINE</p></div>
				<div class="panel-heading">
                    <?php
                        if (empty($_SESSION['sesName']))
                            {
                                    echo '<span class="label label-danger center-block">YOU ARE NOT LOGGED IN, REDIRECTING TO LOGIN PAGE..</span></h3>';
                                    echo "<meta http-equiv='refresh' content='4;url=login.php'>";
                        }
                        else
                            {
                            echo '<h5>' . $_SESSION['sesName'] . ' IS LOGGED IN, FILL IN THE DETAILS AND BOOK A CAB</h5>';
                            }
                    ?>
Page destroying the session :

Code: Select all

		if (isset($_POST['logout']))
        {
            session_unset();
            session_destroy();
            header("location:index.html");

User avatar
hyper
php-forum Active User
php-forum Active User
Posts: 498
Joined: Mon Feb 22, 2016 5:52 pm

Mon Sep 25, 2017 8:51 am

You need to start_session() on all pages.

You cannot do this:

Code: Select all

echo $_SESSION['sesName'];
header("Location:booking.php");

HuuD
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Sep 23, 2017 7:16 pm

Tue Sep 26, 2017 4:50 pm

hyper wrote:
Mon Sep 25, 2017 8:51 am
You need to start_session() on all pages.

You cannot do this:

Code: Select all

echo $_SESSION['sesName'];
header("Location:booking.php");
Thanks, issue resolved..

Appreciate it..

Post Reply