dongpou7275 2015-09-21 01:05
浏览 41

页面重新加载后找不到会话[重复]

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>
</div>
  • 写回答

1条回答 默认 最新

  • douzhan1031 2015-09-21 01:12
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭