dqd2800 2016-01-28 18:49
浏览 55

登录页面传递$ _SESSION

I am not sure why the $_SESSION variable 'user' is not being passed. I do get a successful login, however when redirected to my home page the session is not kept. I use a login status php file to switch some of the nav bar items to sign/register or first name & Logout.

Login Page:

    <?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>

Login Status Page:

<?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>

Pretty sure I am just missing something simple.

  • 写回答

1条回答 默认 最新

  • dqys98341 2016-01-28 19:01
    关注

    I think you forget to add

    session_start();
    

    at the beginnig of the Login Status Page:

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决