登录页面传递 $_SESSION [英] Login Page Passing $_SESSION

查看:50
本文介绍了登录页面传递 $_SESSION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么没有传递 $_SESSION 变量用户".我确实登录成功,但是当重定向到我的主页时,会话没有保留.我使用登录状态 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"><头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>新注册页面</title><link rel="stylesheet" href="style.css" type="text/css"/><身体><中心><div id="登录表单"><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">登录</button></td></tr><tr><td><a href="register_new.php">在这里注册</a></td></tr></表单>

</中心></html>

登录状态页面:

<!DOCTYPE html><html lang="zh-cn"><头><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-ui"><link href="favicon.png" type="image/x-icon" rel="快捷方式图标"><link href="assets/css/master.css" rel="stylesheet"><script src="assets/plugins/jquery/jquery-1.11.3.min.js"></script><?php if(isset($_SESSION['user'])) {$res=mysql_query("SELECT * FROM studentdata WHERE student_id=".$_SESSION['user']);$userRow=mysql_fetch_row($res);?>
  • <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">再见</span></a></li><?php } else { ?><li><a href="includes/register_new.php" title="">注册<span class="nav-subtitle">学生用</span></a></li><li><a href="includes/login_new.php" title="">登录<span class="nav-subtitle">学生用</span></a></li><?php } ?></html>
  • 很确定我只是遗漏了一些简单的东西.

    解决方案

    我想你忘记添加

    session_start();

    登录状态页面的开头:

    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.

    解决方案

    I think you forget to add

    session_start();
    

    at the beginnig of the Login Status Page:

    这篇关于登录页面传递 $_SESSION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆