can not retrive session after reopening browser

I write 2 php files that show up user's info in the database when they type a correct user and display it again if they reopen browser. However when i reopen browser i receive this error msg :

Notice: Undefined index: full_name in C:\xampp\htdocs\test\session-database-2.php on line 3
wrong user please type again
Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\test\session-database-1.php on line 1

1st is session-database-1.php

<?php session_start() ?>
<?php
echo "login Form";
echo "<form action='session-database-2.php' method='POST'>
        name <input type='text' name='full_name' />
            <input type='submit' value='submit'/>
        </form>";
?>

2nd is

<?php session_start() ?>
<?php
$_SESSION['full_name']=$_POST['full_name'];
$host='localhost';
$username='root';
$password='root';
$dbname='pet';
$connect=mysqli_connect($host,$username,$password,$dbname) or die("can't connect to server");
$query="SELECT * FROM register WHERE full_name='{$_SESSION['full_name']}'";
$result=mysqli_query($connect,$query) or die("can't execute query");
if(mysqli_affected_rows($connect))
{
    while($row=mysqli_fetch_assoc($result))
    {
        extract($row);
        echo $full_name."<br/>";
        echo $email."<br/>";
        echo $phone."<br/>";

    }
}
else
{

    echo "wrong user please type again";
    include "session-database-1.php";
    exit();
}

?>

Upvotes: 0

Views: 202

Answers (2)

PKeidel
PKeidel

Reputation: 2589

There are a few more errors I think.

  1. You can delete the 1. line in session-database-1.php because $_SESSION isn't used there
  2. Undefined index: full_name is because you haven't a $_POST when you directly call this url
  3. If you call your second file directly and the SELECT doesn't return any results then you include your first file. This way session_start is called twice.

You could solve it in one file:

if(isset($_POST['full_name']))
  // Then show your login form
else
  // make your database select and show the results

Upvotes: 1

Drazion
Drazion

Reputation: 362

If you want to have the data persist, even when a browser is closed, you're not going to be able to use a session. As Pat mentioned in their comment, a session is destroyed once a browser is closed down. This SO thread might help you understand the difference between the two

Cookie VS Session

Upvotes: 0

Related Questions