正确的用户名和密码对PHP没有任何影响

时间:2014-08-30 11:06:10

标签: php

我在php中有一些问题。这是我的代码:

if (isset($_POST['submit'])) { // Form has been submitte
echo "Submitted";
$username = trim($_POST['username']);
$password = trim($_POST['password']);

// Check database to see if username/password exist.
$found_user = User::authenticate($username, $password);


if ($found_user) {

$session->login($found_user);

redirect_to("index.php");
} 

} else { // Form has not been submitted.
$username = "";
$password = "";
}

?>
<html>
<head>
<title>Photo Gallery</title>
<link href="../stylesheets/main.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
  <h1>Photo Gallery</h1>
</div>
<div id="main">
    <h2>Staff Login</h2>


    <form action="login.php" method="post">
      <table style="float: left;">
        <tr>
          <td>Username:</td>
          <td>
            <input type="text" name="username" maxlength="30" value="
<?php echo htmlentities($username); ?>" />
          </td>
        </tr>
        <tr>
          <td>Password:</td>
          <td>
            <input type="password" name="password" maxlength="30" value="
<?php echo htmlentities($password); ?>" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input type="submit" name="submit" value="Login" />
          </td>
        </tr>
      </table>
    </form>

</div>
<div id="footer">Copyright <?php echo date("Y", time()); ?>, Gio baramidze</div>
</body>
</html>

我的问题是,当我尝试使用不正确的用户登录时,一切正常并且消息&#34;已提交&#34;屏幕上出现了。但是当我写出正确的用户名和密码并点击&#34;登录&#34;它甚至没有向我显示消息(&#34;已提交&#34;)。这意味着&#34; if(isset($ _ POST [&#39; submit&#39;]))#34;这种情况并非如此。尽管事实上我点击了提交。感谢。

2 个答案:

答案 0 :(得分:3)

我的猜测是上面的代码是index.php。当找到用户时,它会重定向到同一页面(快速),因此重新加载后不会设置$ _POST。

if ($found_user) {

$session->login($found_user);

redirect_to("index.php"); //If user found redirect without $_POST information.
} 

您应该使用$ _SESSION数组。这是一个服务器端阵列,您可以在用户登录后使用用户ID填充它。在您需要检查登录的每个页面上,您应该包含一个检查此ID的脚本。您还可以使用它来存储用户名或您需要经常访问的其他内容:

$_SESSION['USER_ID'] = //Some ID
$_SESSION['USERNAME'] = $username

要使$ _SESSION工作,你需要这样的东西:

<?php
session_start();
//other code...
if ($found_user) {

    $session->login($found_user);
    //give the user an ID
    $_SESSION['USER_ID'] = //some id, usually fetched from a database
    //Let's give a name too
    $_SESSION['USERNAME'] = $username;   
    //Now redirect
    redirect_to("index.php"); //If user found redirect without $_POST information.
    } 
    //Other code...
?>

现在从index.php开始我们再次使用session_start();,我们可以使用php来检索他的ID和名称。把它放在最上面,你会看到$ _SESSION被转移到服务器上的其他页面。

<?php
session_start();
echo $_SESSION['USER_ID'];
echo $_SESSION['USERNAME'];
//or something nicer
if (isset($_SESSION['USER_ID']))
{
    echo "User ".$_SESSION['USERNAME']." has logged in and has user id [".$_SESSION['USER_ID']."].";
}
else
{
    echo "Not logged on."
}
?>

出于测试目的,您可以将上述代码存储在whatever.php中,并重定向到login.php中的whatever.php。如果在login.php上找到用户,则应该传递if语句,从而显示用户名和ID。

答案 1 :(得分:0)

您可以通过

更改isset($ _ POST [&#39;提交&#39;])
if(isset($_POST['username']) && isset($_POST['password'])){
        //do your stuff here
}