doutuo3935 2013-12-25 13:47
浏览 42

为什么我不能在PHP中的多个文件中访问会话变量?

I have a login checker that redirects the user to a homepage if the check succeeds. I save the username in session variable at 'username' index.

<?php

ob_start();
include("db.php");
session_start();
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];


$sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'";
$result=pg_query($sql);

$count=pg_num_rows($result);

if($count==1){
$_SESSION['username'] = $myusername;
header("location:homepage.php");
}
else {
    echo "Wrong Username or Password";
}
ob_end_flush();
?>

I try to access the index but get an error that it doesn't exists

 <p>WELCOME!</p>
<?php
session_start();
echo $_SESSION['username'];

I call session_start in each file so I can't see the problem.

  • 写回答

3条回答 默认 最新

  • dquh37673 2013-12-25 14:10
    关注
       Check  your $count variable's  value, 
       I think your  session is not creating due to your conditions.
    
       If your $count value will 1 then it will print session's value.
    
       Your code is fine, no need to change the code, only check your conditions.
    
       I am sure your issue will be solve.  
    
       Change
       if($count==1){
              $_SESSION['username'] = $myusername;
              header("location:homepage.php");
          } 
       To 
    
        if($count){
              $_SESSION['username'] = $myusername;
              header("location:homepage.php");
          } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题