PHP在另一页上显示错误

时间:2012-11-14 07:30:01

标签: php error-handling

我遇到了将问题发送到另一个页面的问题。 我已经创建了错误将被发送到的页面,我尝试了一个头功能。但这似乎不起作用。这是我用于页面的php代码。

<?php


if(isset($_POST['username'], $_POST['password'])){

    //login the user here
     $connect = mysql_connect("","","")or die(mysql_error());
     mysql_select_db("")or die(mysql_error());

    $errors = array();


    $username = strip_tags(mysql_real_escape_string($_POST['username']));
    $password = strip_tags(mysql_real_escape_string($_POST['password']));


    if (empty($Regi_Username) || empty($Regi_password)) {
        $errors[] = 'All fields are requerid';

    } else {

    if (strlen($Regi_Username) > 25) {
        $errors[] = 'Username is to long';

    }

    if (strlen($password) > 25) {
        $errors[] = 'Password is to long';
    }
}

         $password = md5($_POST['password']);

         $loginquery = "SELECT * FROM regi WHERE username='$username' and password='$password'" or die(mysql_error());
         $result = mysql_query($loginquery);
         $count = mysql_num_rows($result);

         mysql_close();
         if($count==1){
            $seconds = 2000 + time();
            setcookie(loggedin, date("F jS - g:i a"), $seconds);
            header("location:member.php");
        } else {
           echo 'Wrong username and password please try agian.';
      }
     }
?>

3 个答案:

答案 0 :(得分:3)

在您的网址中传递GET变量,例如..

header('Location:page.php?err=1');
exit;

在另一页上使用此

if(isset($_GET['err'] && $_GET['err'] == 1) {
   echo 'Error Occured';
}

答案 1 :(得分:0)

您可以使用Alien的方法,但如果使用Session:

则会更好
// Assume you init the session already; Use json_encode since you use array for $errors
$_SESSION['errors_msg'] = json_encode($errors);
header("location:member.php");
// Remember to exit here after we call header-redirect
exit;

此外,您目前的代码存在很多问题:

  1. 使用salt进行哈希密码
  2. 使用mysqli over mysql
  3. 过滤输入,转出输出
  4. ..请阅读本主题中的其他建议..
  5. 请阅读http://www.phptherightway.com/。 PHP有很多正确的推荐(当然不是全部)。

答案 2 :(得分:0)

这是session based方法。这是将消息从一个页面传递到另一个页面的最佳方式,因为它们存储在用户的会话中(与每个用户相关并存储在服务器端的一段数据)而不是在浏览器中(如cookie或URL GET参数,这很容易被破坏,所以操纵来自第三方的消息真的很难。

Page process.php

<?php
// Very top of your page
session_start();
$_SESSION['errors'] = array();
// Do stuff now...
// ...
// Hey it's a X error!
$_SESSION['errors']['X'] = 'Message for X error';
// Continue doing stuff...
// ...
// OMG! It's a Y error now!
$_SESSION['errors']['Y'] = 'Message for Y error';
// Keep doing stuff till you're done...
// All right, process is finished. Any Errors?
if (count($_SESSION['errors']) > 0) {
    // It seems there's been any errors
    // time to redirect to error-displaying page
    header('Location: error-page.php');
    exit;
}

Page error-page.php

<?php
// Very top of your page
session_start();
// Let's check if there is any error stored in the session.
// In the case no errors found, it is better to redirect to another page...
// ...why anybody would end in this page if no errors were thrown?
if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) {
    header('Location: home.php');
    exit;
}
// If we reach this point it means there's at least an error
foreach ($_SESSION['errors'] as $errorCode => $errorMessage) {
    // Here we can display the errors...
    echo '<p>Error ', $errorCode, ': ', $errorMessage, '</p>', PHP_EOL;
}
// You can also do stuff only if a certain error is received
if (array_key_exists('X', $_SESSION['errors'])) {
    // Error `X` was thrown
    echo '<p>Oh no! It seems you suffered a X error!!</p>', PHP_EOL;
    echo '<a href="home.php">Click here to go back home.</a>', PHP_EOL;
}
// At the end you should to remove errors from the session
$_SESSION['errors'] = array();
// or
unset($_SESSION['errors']);