-1

When I reload a page I seem to get disconnected from the session that I have created. I have added session_start() at the top of all my pages. I have even tried adding session_save_path("C:\xampp\tmp"); which I saw suggested on here but when it the user_id Session still disappears when I reload the page. This code above is saved in a file and included into all my pages where I need to get the $user_id variable. It works fine when i log in but then when I try go to another page I get this

Notice: Undefined index: user_id in C:\xampp\htdocs\php\goal\external\user_id.inc.php on line 7 Failed to find sessions.

<?php
    session_save_path("C:\xampp\tmp"); 
    session_start();
    include 'connect.inc.php';


    if($user_id == $_SESSION["user_id"]){

     echo 'session found';

     $user_id=$_SESSION["user_id"];
     print_r($_SESSION); 
                                    }else{
                                            echo 'Failed to find sessions.';
                                        };
?>

Below is the code that creates the Session.

 <?php
require_once'core.inc.php';

if(isset($_POST['username']) && isset($_POST['password'])){
    $username=$_POST['username'];
    $password=$_POST['password'];

    if(!empty($username) && !empty($password)){
        $query_verify = "SELECT `password` FROM `users` WHERE `username` =
         '".mysql_real_escape_string($username)."'";
        $hash = mysql_query($query_verify);
        $password_verified=password_verify($password, $hash);

            if($password_verified=true){
                $query = "SELECT `id` FROM `users` WHERE `username` =    
               '".mysql_real_escape_string($username)."'";
                if($query_run = mysql_query($query)){
                    $query_num_rows =   
 mysql_num_rows($query_run);
                    if($query_num_rows == 0){
                        echo 'You have entered a wrong username or 
 password';
                    }else if($query_num_rows == 1){
                        $user_id = mysql_result($query_run, 0 , 'id');
                        $_SESSION['user_id'] = $user_id;
                        header('Location: home.php');
                        exit();
                    }
                }
            }
    }else{
        echo("Please enter a username and password.");
    }
}   


?>
<form action ="<?php echo $current_file;?>" method="post">
    Name:     <input type='text' name='username'/><br>
    Password: <input type='password' name='password'/>
    <input type='submit' value='Log in'/>
</form>
Niall
  • 804
  • 10
  • 27

1 Answers1

0

I think problem in if($user_id = $_SESSION["user_id"]) statement. It should be if($user_id == $_SESSION["user_id"])

satroy
  • 116
  • 9