【问题标题】:$_SESSION not storing values$_SESSION 不存储值
【发布时间】:2015-01-05 09:06:55
【问题描述】:

我在存储 $_SESSION 变量时遇到问题,我对 PHP 有点陌生,就我目前的知识而言,我喜欢它。

首先是我的信誉:

Win 7 专业版 64 PHP 5 远程服务器(不确定它的配置)

所以我要做的是设置一个登录页面,其中包含用于测试目的的硬编码用户名和密码。

我想在后续页面上显示用户名以确认它。

登录.php

<?php session_start();?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head >
<title>Login</title>

<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en-gb" />

<script type="text/javascript">
    // Perform Client side validation of Username and Password
    function redirect() {
        var username = document.getElementById("username");
        var password = (document.getElementById("password"));

        if (!username.value == " ") {
             if (!password.value == " ") {
                if (username.value == "aknight") {
                    if (password.value == "00226291") {
                        window.location = "Home.php";
                        exit();
                    } else
                        alert("Username or Password Invalid");
                }
            } else
                alert("Password is required");
        } else
            alert("Username is required");
    }
</script>
</head>

<body>
<div id="header">
<h1>Product Order System</h1>
<br/>
<h2>The most expensive crap youll ever find...</h2>
</div>

<div id="content">
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

$_SESSION['username'] = $_POST['username'];

}else{
?>

<form action="" method="POST">
<fieldset>
<legend>Login Information</legend>
<p>
<label for="Username">Username:</label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="Password">Password:</label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="button" name="login" value="Login" onclick="redirect();"/>
<input type="button" name="cancel" value="Cancel" />
</fieldset>
</form>
<?php }

    include ('includes/footer.html');
?>

home.php

<?php
session_start();
$page_title = 'Home';
include ('includes/header.php');
if(isset($_SESSION['username'])) { $name = $_SESSION['username'];}else{echo "<p>error</p>";}
echo "<h3>Welcome ".$name."!</h3>" ;
?>

我已尝试打印出 $_SESSION 值,但它们为空。

产生这个结果

<?php
   echo "<pre>".print_r($_SESSION)."</pre>";
?>

Array {
    [username] => 
}

请不要介意 JS,它只是为了继续前进而进行的学前客户端验证。

任何帮助将不胜感激。

【问题讨论】:

    标签: php session


    【解决方案1】:

    为您的登录表单命名,然后在您的 JavaScript 验证函数中,而不是 window.location,执行 document.forms['yourformname'].submit()。

    【讨论】:

    • 我尽量避免在 JS 中使用 Form,但在这里它是有道理的,是一个很大的疏忽。
    【解决方案2】:

    我认为您的 Javascript 验证实际上是导致问题的原因。 您正在使用 POST 请求在 Login.php 中设置会话变量,但实际上没有 POST 被发送到 Login.php,因为一旦表单被认为有效,您的 Javascript 就会重定向到 Home.php。您需要在流程中的某个时间点提交表单,因此您实际上会获得一些 POST 值,您将使用这些值来填充您的 SESSION 变量。 如果你想保持你的 Javascript 验证,也许看看 submit() :http://www.w3schools.com/jsref/met_form_submit.asp 在提交表单之前,您必须在表单上设置正确的操作属性。

    【讨论】:

    • 我真的没有看到我没有向 login.php 发布任何内容,并且使用 JS 的 submit() 允许发布,调用 header() 也很好,谢谢。跨度>
    【解决方案3】:

    所以这是一个似乎有效的修复方法

    <?php session_start();?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head >
    <title>Login</title>
    
    <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="content-language" content="en-gb" />
    
    <script type="text/javascript">
        // Perform Client side validation of Username and Password
        function redirect() {
            var username = document.getElementById("username");
            var password = (document.getElementById("password"));
    
            if (!username.value == " ") {
                if (!password.value == " ") {
                    if (username.value == "aknight") {
                        if (password.value == "00226291") {
                            document.forms[0].submit();
                            exit();
                        } else
                            alert("Username or Password Invalid");
    
                    }
                } else
                    alert("Password is required");
    
            } else
                alert("Username is required");
    
        }
    </script>
    </head>
    
    <body>
    <div id="header">
    <h1>Product Order System</h1>
    <br/>
    <h2>The most expensive crap youll ever find...</h2>
    </div>
    
    <div id="content">
    <?php
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    
        $_SESSION['username'] = $_POST['username'];
        header('Location : ../Home.php');
    
    }else{
    ?>
    
    <form action="Home.php" name="login" method="POST">
    <fieldset>
    <legend>Login Information</legend>
    <p>
    <label for="Username">Username:</label>
    <input type="text" name="username" id="username" />
    </p>
    <p>
    <label for="Password">Password:</label>
    <input type="password" name="password" id="password" />
    </p>
    <p>
    <input type="button" name="login" value="Login" onclick=" redirect();"/>
    <input type="button" name="cancel" value="Cancel" />
    </fieldset>
    </form>
    <?php }
    
        include ('includes/footer.html');
    ?>
    

    如果nex系列页面有类似问题会再次发布

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 2022-06-27
      • 2014-08-20
      • 2018-03-31
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多