2012-04-27 210 views
0

我刚开始学习PHP,并且在简单的登录页面工作时遇到了一些麻烦。当我第一次加载这个脚本时,文本“错误的密码/用户名”。并且注销按钮被打印,但不是登录表单。为什么会发生这种情况,以及如何更改代码以使登录表单和注销按钮按预期一起工作?PHP登录表单和注销按钮不能正常显示

<?php 

if (isset($_POST['log_out'])) { // If the page was reloaded as a result of the user having clicked the logout button, 
session_destroy(); // kill session. 
} 

session_start(); // Start a new session with 

$_SESSION['user'] = 'foo'; // a static username, and 
$_SESSION['pass'] = 'bar'; // a static password. 
// If I insert $_SESSION['logged_in'] = 'false'; here to start things off, a blank alert box will be returned no matter from where on the page I alert() the value with JS. On the other hand, without this line the same alert will return "1" both here and further down in the script. 

if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { // If the username and password filled in before the page reload match the static ones, 
    $_SESSION['logged_in'] = true; // the user is logged in. 
} else { // If there is no match ... 
    echo 'Wrong password/username.'; 
} 

include("head.php"); // HTML snippet with everything from the DOCTYPE to the opening BODY tag. 

?> 
      <div> // HTML interlude ... 
<?php 

// If the user is logged in, print out a logout button in HTML at the top of the page: 

if (isset($_SESSION['logged_in']) && ($_SESSION['logged_in'] == true)) { 
    echo '   <form action="index.php" method="post">'; 
    echo '   <input type="submit" name="log_out" value="Log out">'; 
    echo '   </form>'; 
} 

?> 
       <p>HTML interlude ...</p> 

<?php 

// If the user is not logged in, print out a login form in HTML at the bottom of the page: 

if ($_SESSION['logged_in'] != true) { 

    echo '   <form action="index.php" method="post">'; 
    echo '    <label for="username">Username</label><br>'; 
    echo '    <input type="text" name="username" id="username"><br>'; 
    echo '    <label for="password">Password</label><br>'; 
    echo '    <input type="text" name="password" id="password"><br>'; 
    echo '    <input type="submit" name="submit" id="submit" value="Log in">'; 
    echo '   </form>'; 

} 

?> 
      </div> 
<?php include("footer.php"); ?> 
+1

您能添加输出的快照吗? – 2012-04-27 07:58:47

回答

1
if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { the static ones, 
    $_SESSION['logged_in'] = true; // the user is logged in. 
} else { 
    echo 'Wrong password/username.'; 
} 

页面加载后的第一次,因为如果没有来自$_POST['username']病情会是假的这部分将显示“密码错误/用户名”。

将条件isset($_POST['username']) && isset($_POST['password'])添加到两个选项中。像这样:

if(isset($_POST['username']) && isset($_POST['password'])){ 
    if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { the static ones, 
     $_SESSION['logged_in'] = true; // the user is logged in. 
    } else { 
     echo 'Wrong password/username.'; 
    } 
} 

这种方式不会在第一次加载页面时评估,但只有在发布凭证时才会对此进行评估。

+0

另外,我不会建议在$ _SESSION中存储正确的用户名和密码。例如,最好从用户数据库中获取。 – Gustav 2012-04-27 08:08:32

+0

非常感谢,@Gustav。一旦某人为你指出这些事情,有些事情是非常明显的。现在只要cookie被清除,我就可以按照预期在页面加载登录表单,并且不会输入“错误的密码/用户名”。文本。然而,一旦我登录后,我无法注销(注销按钮在那里,但点击它什么也不做)会话,甚至没有页面重新加载后。 (我应该为此创建另一个问题吗?) – Johanna 2012-04-27 08:47:01

+0

静态用户名和密码仅用于测试目的;一旦我登录功能正常工作,我会尝试使用数据库。 – Johanna 2012-04-27 08:50:31

0

您可以通过调试脚本发现:商店的预期为变量,并打印出来,例如:

$isLoggedIn = $_SESSION['logged_in']; # Give things a name! (always useful) 
echo 'isLoggedIn: ', var_dump($isLoggedIn), "\n"; # debugging 
if ($isLoggedIn != true) { 

    echo '   <form action="index.php" method="post">'; 
    echo '    <label for="username">Username</label><br>'; 
    echo '    <input type="text" name="username" id="username"><br>'; 
    echo '    <label for="password">Password</label><br>'; 
    echo '    <input type="text" name="password" id="password"><br>'; 
    echo '    <input type="submit" name="submit" id="submit" value="Log in">'; 
    echo '   </form>'; 

} 
+0

感谢您的提示!我用JS提醒做了一些“丑陋”的调试 - 即使我使用调试器,我仍然处于这样的阶段,我经常不明白它要告诉我什么,但是实践是唯一能够通过的方法那。 – Johanna 2012-04-27 08:59:40

+0

JS调试与PHP调试不同。对于PHP,您默认没有控制台。 – hakre 2012-04-27 11:02:32

0

第一次加载你的页面,然后除非你传递所需的后变量,您的条件检查

if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) 

将失败,因此显示Wrong password/username.消息。

+0

感谢您指出!现在很明显,我知道它。 :) – Johanna 2012-04-27 09:18:32

0

代码看起来不错,实际上对于“刚刚学习”的人来说做得很好。

鉴于代码没有明显错误(几个粗糙的边缘 - 比较布尔值时应该考虑使用'==='而不是'=='),那么也许这就是会话问题。

尝试...

print_r($ _ SESSION);

after session_start();并在选择是否显示注销按钮的条件之前。

每当您不要将参数发布到页面以及发布错误值时,都会显示错误的用户名消息。试试这个:

if (($_POST['username']) 
    && ($_POST['username'] == $_SESSION['user']) 
    && ($_POST['password'] == $_SESSION['pass'])) { 

顺便说一下,这个会话不是存储用户名和密码的好储存库 - 它是特定于当前用户的。对于多用户系统,这些将存储在单独的数据库中,例如,

if (($_POST['username']) && lookup($_POST['username'], $_POST['password'])) { 
... 
+0

谢谢@symcbean鼓励人物的话和关于print_r()的小技巧 - 我会把这个好用的!现在,在测试完参数是否发布之后,在第一页加载时,没有错误消息和没有注销按钮,只是登录窗体像预期的那样,但是一旦登录,我无法注销 - 注销按钮在那里,但是没有做任何事情。 (就像我上面提到的古斯塔夫一样,也许正确的方法是为此创建一个新问题?) – Johanna 2012-04-27 09:15:59