2011-11-18 40 views
3

我的应用程序是一个简单的登录页面。当它失败时,我打印一条错误消息。我的问题是,为什么当我重新加载页面时,邮件又被打印了?我该如何解决这个问题? 代码工作正常,我做了另一个php文件执行数据库检查&连接。为什么POST ['submit']在我重新加载时设置?

<?php 
require_once("include/database.php");  
if(isset($_POST['submit'])) { 
    connect_bookstore(); // custom function 
    $spassword = sha1($_POST['password']); 
    $username = $_POST['username']; 
    if (checkpassword($username,$spassword)) { //custom function 
     header('Location:insert.php'); 
     exit; 
    } else { 
     $message = "Login failed!";   
    } 
} 
?> 

在html正文中。

<?php 
if (isset($message)) { 
    echo $message; 
} 
?> 
+0

[只能使用$ _POST信息一次](http://stackoverflow.com/questions/8171227/using-post-information-only-once) – Gian

+2

你应该给你的哈希值加盐。 – SLaks

回答

11
<?php 
session_start(); 

require_once("include/database.php");  
if(isset($_POST['submit'])) { 
    connect_bookstore(); // custom function 
    $spassword = sha1($_POST['password']); 
    $username = $_POST['username']; 
    if (checkpassword($username,$spassword)) { //custom function 
     header('Location:insert.php'); 
     exit; 
    } else { 
     $_SESSION['message'] = "Login failed!"; 
     header('location: /yourfile.php'); 
     exit;  
    } 
} 

if(isset($_SESSION['message'])) 
{ 
    echo $_SESSION['message']; 
    unset($_SESSION['message']); 
} 
?> 

基本上,是的,后/重定向/得到...但有时一个简单的解释是更好的。

我使用会话来存储Flash消息,然后像这样显示它们。

+0

感谢您的示例。 –

1

那是因为你重发相同的POST数据时,你刷新,如果你做一个GET请求,你会在你的参数要传递是那里的URL注意到,因此,如果您刷新这些参数再次是发送。与POST一样的东西。

0

当您重新加载页面时,浏览器将发送与原始页面相同的请求。

你想要一个POST-Redirect-GET

相关问题