0

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();
}

?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

2 Answers2

1

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
PKeidel
  • 2,559
  • 1
  • 21
  • 29
0

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

Community
  • 1
  • 1
Drazion
  • 342
  • 1
  • 4
  • 12
  • With `ini_set('session.cookie_lifetime', 60 * 60 * 24 * 7);` you can set your session lifetime to 7 days. If you close your browser and reopen it again, your session will still be there. – PKeidel Dec 06 '13 at 15:41
  • I don't think this is relevant to his error. He's assigning to the session, not reading from it. – Barmar Dec 06 '13 at 15:41
  • ah ya you're right ... was a bit too hasty I suppose. But for the session_lifetime I had assumed he had it left at default, which kills the session on exit. – Drazion Dec 06 '13 at 15:43
  • It was just for the records becouse Drazion wrote `a session is destroyed once a browser is closed`. – PKeidel Dec 06 '13 at 15:43
  • Yes @Drazion, thats right. The default value is `0` which means `until the browser is closed` – PKeidel Dec 06 '13 at 15:44