0

発生したエラーを別のページに送信する際に問題が発生しています。エラーが送信されるページを既に作成しており、ヘッダー機能を試しました。しかし、それはうまくいかないようです。これが、ページに使用している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.';
      }
     }
?>
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パラメータなど)ではなく、ユーザーのセッション(各ユーザーに関連し、サーバー側に保存されるデータ)に保存されるため、あるページから別のページにメッセージを渡すための最良の方法です。これは簡単に破損する可能性があります)、そのため、サードパーティからのメッセージを操作することは非常に困難です。

ページ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 に答える