2013-05-17 105 views
0

如果登錄輸入不正確,我將用戶重定向到登錄頁面。重定向時POST變量到另一個頁面 - php

$sql = "select * from Driver where username=$username and pwd=$pwd"; 
$driver = mysql_query($sql); 

if(!$driver){ 
    header("Location: http://domain.de/login.php"); 
    exit(); 
} 

我是否也可以將類似"sorry, username isnot correct"的消息傳遞給登錄頁面?

我不想使用會話。得到狀態並沒有在這裏

+2

urlencode $ message – Kevin

+0

@Kevin,怎麼樣?你能說更多的話嗎? – doniyor

+0

http://php.net/manual/en/function.urlencode.php應該可以幫到你。將它附加到URL時。確保在另一端使用urldecode()。 – Kevin

回答

1

的選項,你可以不喜歡它

header("Location: http://domain.de/login.php?error=username"); 

和做其他的頁面上

if ($_GET['error'] == 'username') { 
    echo 'Sorry, username is not correct!'; 
} 

編輯: 當心SQL注入也

+0

謝謝工作。我怎樣才能做到這一點與POST? – doniyor

+0

你爲什麼要這個? –

+0

簡單:我有很大的消息,我不想把它放到網址 – doniyor

1

您可以添加獲取參數到位置標題或保存會話中的消息標誌。就像這樣:

$sql = "select * from Driver where username=$username and pwd=$pwd"; 
$driver = mysql_query($sql); 

if(!$driver){ 
    header("Location: http://domain.de/login.php?wasredirect=1"); 
    exit(); 
} 

//////// In login.php 

if (isset($_GET['wasredirect'])) { 
    echo "sorry, username isnot correct"; 
} 

或者這樣:

 $sql = "select * from Driver where username=$username and pwd=$pwd"; 
$driver = mysql_query($sql); 

if(!$driver){ 
    header("Location: http://domain.de/login.php"); 
    if (!isset($_SESSION)) { 
     session_start(); 
    } 
    $_SESSION['redirect'] = true; 
    exit(); 
} 

//////// In login.php 

if (!isset($_SESSION)) { 
    session_start(); 
} 
$_SESSION['redirect'] = true; 
if (isset($_SESSION['redirect']) &&$_SESSION['redirect']) { 
    echo "sorry, username isnot correct"; 
    unset($_SESSION['redirect']); 
} 
+0

我不想保存到會話中。這只是一個2秒的消息,它不需要保存到會話 – doniyor

+0

你可以在「使用」之後刪除會話中的值,例如,或者添加獲取參數 – Eugene

1

對於簡單地放棄了一個消息,你可以將它添加到URL。

header("Location: http://domain.de/login.php?e=1234"); 

爲了獲得更好的靈活性,我建議使用錯誤代碼而不是全長消息。

請注意,如何做到這一點需要實現MVC模式,然後在內部加載錯誤頁面的路由。但是對於一個小腳本來說這可能太多了。

我知道你不會反饋給你的查詢。無需擔心,除非您對SQL注入的含義毫無頭緒。

最佳方面

索爾特

-3

查詢更改爲:

$sql = "select * from `Driver` where `username`='$username' and `pwd`='$pwd'"; 

注意反引號和單引號

+0

這不是問題,我的查詢很好 – doniyor

+0

字符串SQL查詢哪些不在引號可以使問題 – SAVAFA

+0

在什麼意義上的問題? – doniyor

1

我認爲最好的解決方案是加載頁面的login.php作爲當前腳本(控制器)的一部分(視圖),並使用消息的值設置變量。類似於:

if(!$driver){ 
$message = "Sorry, username isnot correct"; 
} 
else { 
$message = "Whatever"; 
} 
include('login.php'); 

$ login.php腳本中將提供您的消息。

+0

我沒有明白你的意思。在哪裏加載?我沒有使用MVC btw – doniyor

相關問題