php - 登录页面传递 $_SESSION

标签 php mysql html session session-variables

我不确定为什么 $_SESSION 变量“user”没有被传递。我确实成功登录,但是当重定向到我的主页时, session 不会保留。我使用登录状态 php 文件将某些导航栏项目切换为签名/注册或名字和注销。

登录页面:

    <?php
session_start();
include_once 'dbconnect_new.php';

if(isset($_SESSION['user'])!="")
{
    header("Location: ../index.php");
}

if(isset($_POST['btn-login']))
{
    $s_email = mysql_real_escape_string($_POST['email']);
    $s_password = mysql_real_escape_string($_POST['password']);
    $s_email = trim($s_email);
    $s_password = md5(trim($s_password));

    $res=mysql_query("SELECT student_id, student_firstname FROM studentdata WHERE student_email='$s_email' AND student_password='$s_password'");
        if (!$res) {
        // Debug query result by below code
        //echo 'Could not run query: ' . mysql_error();
        //exit;
        echo '<script language="javascript">';
        echo 'alert("Username / Password Seems Wrong !")';
        echo '</script>';
    }else{
      $row = mysql_fetch_row($res);
      $stu_id = $row[0];         
      $stu_fname =  $row[1];
      $_SESSION['user'] = $stu_id;
      header("Location: ../index.php");
    } 

}
?>
    <!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">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>New Reg Page</title>
        <link rel="stylesheet" href="style.css" type="text/css" />
    </head>

    <body>
        <center>
            <div id="login-form">
                <form method="post">
                    <table align="center" width="30%" border="0">
                        <tr>
                            <td>
                                <input type="text" name="email" placeholder="Your Email" required />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input type="password" name="password" placeholder="Your Password" required />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <button type="submit" name="btn-login">Sign In</button>
                            </td>
                        </tr>
                        <tr>
                            <td><a href="register_new.php">Sign Up Here</a></td>
                        </tr>
                    </table>
                </form>
            </div>
        </center>
    </body>

    </html>

登录状态页面:

<?php
 session_start();
 ?>
    <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimal-ui">
    <link href="favicon.png" type="image/x-icon" rel="shortcut icon">
    <link href="assets/css/master.css" rel="stylesheet">
    <script src="assets/plugins/jquery/jquery-1.11.3.min.js"></script>
</head>

<?php if(isset($_SESSION['user'])) { 
    $res=mysql_query("SELECT * FROM studentdata WHERE student_id=".$_SESSION['user']); 
    $userRow=mysql_fetch_row($res);?>
    <li class="dropdown">
        <a href="">
            <?php echo $userRow['student_firstname'];?><span class="nav-subtitle">Account</span></a</li>
                <li><a href="includes/logout.php" title="">Logout<span class="nav-subtitle">Goodbye</span></a></li>
                <?php } else { ?>
                    <li><a href="includes/register_new.php" title="">Register<span class="nav-subtitle">for Students</span></a></li>
                    <li><a href="includes/login_new.php" title="">Login<span class="nav-subtitle">for Students</span></a></li>
                    <?php  }  ?>

</html>

很确定我只是错过了一些简单的东西。

最佳答案

我认为您忘记添加

session_start();

登录状态页面的开头:

关于php - 登录页面传递 $_SESSION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069604/

相关文章:

javascript - jQuery 附加了错误的 html 元素

javascript - 应用不同的复选框过滤器来显示/隐藏 div

javascript - 如何使用 html、Javascript、CSS 或其他工具按颜色代码或 bookweb 中的类别对教科书进行排序?

javascript - 来自 html/javascript/php 中的 json 响应的折线图

php - 计算php中字符串中的精确子字符串

MySQL 自然排序不起作用

java - 使用 JDBC 将值插入到 postgresql

php - 按类别过滤请求 WordPress Rest Api

php - 替换重复的 html php 仅图像和文本更改

mysql - MySQL 语句中的聚合结果