Login Page Passing $_SESSION

妖精的绣舞 提交于 2019-12-11 19:05:10

问题


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:


I think you forget to add

session_start();

at the beginnig of the Login Status Page:



来源:https://stackoverflow.com/questions/35069604/login-page-passing-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!