0

I'm having issues to send an occuring error to another page. I have already created the page the error will be sent to, and I've tried a header function. But that doesn't seem to work. Here is the php code that I am using for the page.

<?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.';
      }
     }
?>
4

3 回答 3

3

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

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

在另一页上使用这个

if(isset($_GET['err'] && $_GET['err'] == 1) {
   echo 'Error Occured';
}
于 2012-11-14T07:32:40.967 回答
1

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

页面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;
}

页面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']) || !is_array($_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']);
于 2012-11-14T08:33:32.540 回答
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. 使用盐对密码进行哈希处理
  2. 在 mysql 上使用 mysqli
  3. 过滤输入,转义输出
  4. ..阅读本主题中的其他建议..
  5. 请阅读http://www.phptherightway.com/。PHP 有很多正确的推荐(当然不是全部)。
于 2012-11-14T08:23:41.470 回答