dongshang1979 2011-03-19 16:34
浏览 38
已采纳

PHP会话变量未设置

I'm having trouble with PHP session variables: I try setting the variables, and they can't be read even in the same file, much less another file.

Sorry if the question has been asked before, but I can't find anything that doesn't say "make sure you have session_start() at the beginning of the file."

Lines 1, 2, & 3 of file:

<?php
    ini_set('session.save_path','/home/[username]/session_data'); // Just in case, points to a correct directory
    session_start();

Later, after username validation, and login, the variables are set, and code tries to echo them out:

$_SESSION['login'] == "true";
$_SESSION['username'] == $username;
$_SESSION['password'] == sha1($password);
//header("Location: [admin page hidden]"); // Code would normally redirect to admin page (commented out for debugging), and exit
//exit;
echo "Username: ".$username."<br />"; // The following 4 lines try to print out the data
echo "Password: ".$password."<br />";
echo "Secure Password: ".sha1($password)."<br />";
echo "Session Username: ".$_SESSION['username']."<br />";

The output is:

Username: root
Password: [correct password]
Secure Password: [correct sha1 version of password]
Session Username: 

The secure password (sha1, what is checked) matches what is in the database file, I checked. Nothing comes out for the session variables, even though it was created 7 lines previously. The directory that I pointed the save path to just has a blank file for the session.

Does anyone have any clue why this doesn't work, or advice on how to proceed? Any helpful response is appreciated.

  • 写回答

3条回答 默认 最新

  • douyun3887 2011-03-19 16:36
    关注

    The following lines are the cause of the problem :

    $_SESSION['login'] == "true";
    $_SESSION['username'] == $username;
    $_SESSION['password'] == sha1($password);
    

    Those lines do pretty much nothing : they compare what's before the == with what's after it ; and do nothing with the result of that comparison.


    To assign values to variables, you should use ONE =, and not two :

    $_SESSION['login'] = "true";
    $_SESSION['username'] = $username;
    $_SESSION['password'] = sha1($password);
    

    Those three lines assign what's on the right of the = to what's on its left.


    In PHP :

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写