doujiang5211 2017-04-14 18:57
浏览 35
已采纳

会话变量不会转移到其他页面

php session variables dont carry over from login page to home page. I can see the variables being saved in the session files on the web server with the correct values, but the second page doesn't see them. looks like it is openeing a new session every time.

The home page just redirects back to login page even after successful login. I do see that there have been quite a few questions in the forum on this issue. But most of them have the same solution. use session_start().

I've used session_start() on both pages.

My actual plan was to use MySQL db for saving session data. Login page would write session data to DB but the home page wouldnt read it. I'm trying to use local files now to eliminate any issue with the DB or my code for session management using DB. I still have the same issue with local files as well.

login.php

<?php
session_start();
if(isset($_POST['submit'])) {

//sanitize the data
$user = htmlspecialchars (stripslashes (trim ($_POST['user'])));
$password = htmlspecialchars (stripslashes (trim ($_POST['password'])));
$domain = htmlspecialchars (stripslashes (trim ($_POST['domain'])));
//validate user account from LDAP.
//once authorized, assign session variables and redirect to home page.
$_SESSION['accesslevel'] = "III";
$_SESSION['loggedin'] = "true";
$_SESSION['user'] = $user;
$_SESSION['name'] = $name;
header('location:home.php');
?>
`<!DOCTYPE html>
<html lang="en"> 
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin Login</title>
<link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="logincontainer">
<div class="login">
    <h1>Login</h1>
        <form action="<?php htmlentities(urldecode($_SERVER['PHP_SELF']));?>" 
method="post">
        <p><b></b><input type="text" name="user" value="" placeholder="ADM_5-
2-1" required autocomplete="off"></p>
        <p><b> </b><input type="password" name="password" value="" 
placeholder="Password" required autocomplete="off"></p>
        <p><select name="domain">
            <option value="DOMAIN">DOMAIN</option>
            <option value="domain">NANET</option>
            <option value="domain">EUNET</option>
            <option value="domain">APNET</option>
            <option value="domain">JPNET</option>
        </select>
        </p>
        <p class="submit"><input type="submit" name="submit" value="Login">
</p>
    </form>
</div>
</div><br>
<div class="error">
    <?php //print_r($_POST);?>
    <?php //var_dump($_POST);?>
    <?php //echo $msg;?>
</div>
</body>
</html>

home.php

<?php
session_start();
if ((!isset($_SESSION['loggedin'])) or ($_SESSION['loggedin'] != true)) {
header ("location: login.php");
}

<<<<html (php uses session variable (name) for user profile menu>>>>>

?>
  • 写回答

1条回答 默认 最新

  • duanmou9228 2017-04-14 18:59
    关注

    In your login.php, Change the $_SESSION['loggedin'] = "true"; to $_SESSION['loggedin'] = true;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?