PHP会话变量不保持不变

时间:2014-01-28 21:29:26

标签: php session

我在使用$_Session[]时遇到问题,我正在尝试设置一个常量变量,该变量应该在我的所有页面中都有效。

当有人成功登录时,它会在登录时变为活动状态。{$logged_in设置为1

以下是 action.php:

的代码
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
    if($logged_in==1) {
        $_SESSION['logged_in'] = 1;
        $_SESSION['logged_user'] = $username;
    }
?>
<head>

action.php是完成所有表单处理的地方,因此在action.php中设置了$logged_in

然后我尝试在 index.php中调用$_SESSION['logged_user']

(link to code) http://pastebin.com/8BA3csUZ

但正如你可以通过查看代码看到的那样:我试图将其称为<?php echo $_SESSION['logged_in']; ?>,但没有任何反应,为什么会这样?

修改: 登录代码:

if (isset($_GET['do']) && $_GET['do'] === "login") {
$username = $_POST['username'];
$password_input = $_POST['password'];
$password = md5($password_input);

$result = mysqli_query($con,"SELECT * FROM users WHERE username='" . $username . "'");
while($row = mysqli_fetch_array($result)) {
if($password == $row['password']) {
echo "successful login";
$logged_in = 1;
}
else { echo "unsuccessful login";}
}
}

...是的我知道md5并没有真正做太多;)

1 个答案:

答案 0 :(得分:1)

if (isset($_GET['do']) && $_GET['do'] === "login") {
$username = $_POST['username'];
$password_input = $_POST['password'];
$password = md5($password_input);

$result = mysqli_query($con,"SELECT * FROM users WHERE username='" . $username . "'");
while($row = mysqli_fetch_array($result)) {
if($password == $row['password']) {
session_start();
$_SESSION['logged_in'] = 1;
echo "successful login";
}
else { echo "unsuccessful login";}
}
}