【问题标题】:php error handling using session and redirecting使用会话和重定向的 php 错误处理
【发布时间】:2012-06-27 12:36:29
【问题描述】:

我有一个从用户数据库检查登录名和密码的 php 文件,它工作正常。

但如果用户不存在或用户密码不正确,我无法验证要显示的确切错误,并在错误后返回上一页,请帮助我如何显示这些错误。

<?php // access.php
include_once 'common.php';
include_once 'db.php';

session_start();

$uid = isset($_POST['uid']) ? $_POST['uid'] : $_SESSION['uid'];
$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd'];

if(!isset($uid)) {
  ?>

  <!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>
<title>Login</title>
    <meta http-equiv="Content-Type"
      content="text/html; charset=iso-8859-1" />
<head>
<style type="text/css">
<!--
.style1 {
    font-size: 16px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
}
.style3 {
    font-size: 12px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
}
-->
</style>

  </head>
<body>
  <h1 class="style1"> <br><br>  Login Required </h1>
  <span class="style3"><br>
  You <strong>must login to access this area </strong>of the site. <br>
  <br>
  If you are not a registered user, please contact your Admin
     to sign up for instant access!</span>
  <p><form method="post" action="<?=$_SERVER['PHP_SELF']?>">
    <span class="style3">User ID:&nbsp;&nbsp;&nbsp;&nbsp;    
    <input type="text" name="uid" size="12" />
    <br>
    <br />
    Password:</span>    
    <input type="password" name="pwd" SIZE="12" />
    <br>
    <br />
    <input type="submit" value="Login" />
  </form></p>
</body>
 </html>

  <?php
  exit;
}

$_SESSION['uid'] = $uid;
$_SESSION['pwd'] = $pwd;

dbConnect("svga");
$sql = "SELECT * FROM user WHERE
        userid = '$uid' AND password = '$pwd'";
$result = mysql_query($sql);
if (!$result) {
  error('A database error occurred while checking your '.
        'login details.\\nIf this error persists, please '.
        'contact you@example.com.');
   }

if (mysql_num_rows($result) == 0) {
  unset($_SESSION['uid']);
  unset($_SESSION['pwd']);
  ?>

  <!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>
    <title> Access Denied </title>
    <meta http-equiv="Content-Type"
      content="text/html; charset=iso-8859-1" />
        <style type="text/css">
<!--
.style1 {
    font-size: 16px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
}
.style3 {
    font-size: 12px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
}
-->
</style>  

  </head>
  <body>
  <br/>
  <br/>

  <h1 class="style1"> Access Denied </h1>
  <p class="style3">Your user ID or password is incorrect, or you are not a
     registered user on this site. To try logging in again, click
     <a href="<?=$_SERVER['PHP_SELF']?>">here</a>. To access, please contact our Admin     !</a>.</p>
  </body>
  </html>

<?php
  exit;
}
$username = mysql_result($result,0,'fullname');
$_SESSION['user'] = mysql_result($result,0,'userid');
?>

【问题讨论】:

  • 不要告诉你的用户他插入了错误的字段。这是出于安全原因。只需显示常见消息,例如“用户名和/或密码不正确”。
  • @Rafael Sedrakyan 你是对的!但我的要求是我必须向用户显示错误是什么。

标签: php mysql redirect login


【解决方案1】:

简单地说

if(mysql_num_rows($result)==0) {
header('location:your_page.php');
}

这会将你重定向到页面并假设你已经定义了error()方法,只是

echo error();

【讨论】:

  • 你能解释一下如何编写error()方法以及在哪个脚本中
  • 检查行数后,如果我们将其重定向到用户输入用户名的登录页面,它将丢失所有已经输入的数据!
  • 为此,您可以使用会话将数据恢复到表单中。
猜你喜欢
  • 2013-02-11
  • 1970-01-01
  • 2011-07-03
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多