2012-11-14 31 views
0

我遇到了問題,要將發生錯誤發送到另一頁面。 我已經創建了錯誤將被髮送到的頁面,並且我嘗試了一個頭部函數。但這似乎並不奏效。這裏是我用於頁面的php代碼。另一個頁面上的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.'; 
     } 
    } 
?> 
+1

你應該考慮使用會話的登錄與存儲,而不是隻使用cookie。並非所有人都喜歡在機器上存儲這些文件,有些人也不能。代碼應該幾乎適合 - 你會得到任何錯誤嗎? – Jacta

+0

我按下按鈕時顯示錯誤,但我希望它們進入錯誤頁面。 –

+0

在您的'PHP'代碼之前是否有'HTML',並且您在Web服務器日誌中有任何錯誤? – core1024

回答

3

傳遞GET變量在您的網址一樣..

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

在其他頁面使用此

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

你確定代碼沒有標題錯誤嗎? – Jacta

+0

@Jacta你沒有得到你,你說什麼錯誤? –

+1

只是想我的錯誤去onther頁,我有他們所有的樣式 –

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; 

此外, E均爲很多問題是你目前的代碼:

  1. 使用鹽的哈希密碼
  2. 使用的mysqli對MySQL
  3. 過濾輸入,轉義輸出
  4. ..這個話題在這裏閱讀其他建議。 。
  5. 請閱讀http://www.phptherightway.com/。 PHP有很多正確的建議(當然不是全部)。
0

這是一個session based方法。這是將消息從一個頁面傳遞到另一個頁面的最佳方式,因爲它們存儲在用戶的會話中(與每個用戶相關並存儲在服務器端的一段數據),而不是在瀏覽器中(如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']) && !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']); 
相關問題