2011-08-26 135 views
1

我已經創建了一個名爲profile.php的頁面,通過使用「get」方法從mainpage.php獲取值。但是,如果從mainpage.php發送的值爲空或錯誤,則頁面將重定向到mainpage.php。不過,我在profile.php中使用了「post」方法,允許用戶在profile.php中發佈內容。所以,如果用戶提交的東西,profile.php重新加載和重新加載profile.php使變量,我從mainpage獲得,爲空,並指示頁面不主頁page.php不情願。我可以如何解決它?代碼;Post方法阻止頁面加載

$another_user = $_GET['username'];//gets a value from mainpage.php 
$check = mysql_query("SELECT * FROM users WHERE user_name = '".$another_user."'"); 
$sent = mysql_fetch_array($check); 
if(!$sent) 
{ 
header('Location: mainpage.php'); 
    exit(); 
} 

//some codes around here 

<form action="profile.php" method="post"> 
Commet: <input type="text" name="comment" placeholder = "comments?"/> 
<input type="submit"/> 
</form> 
<?php 
if(isset($_POST['comment'])&&!($_POST['comment']="")) 
{ 
$writing = $_POST['comment']; 
echo $writing; 
} 

感謝

+0

你知不知道你是在你的SQL查詢結束時丟失了「還有,看到整個文件會更有幫助,而不僅僅是這個片段。 – Prisoner

+0

我不同意,囚徒。他的代碼片段足以證明他的問題。錯過了一個「,正如我在我的回答中指出的那樣。 – Jules

+0

是的,我意識到:(但實際上,我把它翻譯成英文時意外地使它變得更有意義。代碼在語法方面很好,但除了問題中提到的問題:) – user893970

回答

1

要開始與你在第二行有一個語法錯誤,因爲顏色告訴你了。所以改成:

$check = mysql_query("SELECT * FROM users WHERE user_name = '".$another_user."')"; 

有很多解決方案,您的問題。其中一個可能是驗證用戶來自哪個頁面。如果這是mainpage.php,則可以驗證username,如果這是profile.php,則應檢查comment變量。

您可以使用$_SERVER['HTTP_REFERER']變量來檢查引用者是哪一個。因此,像:

if ($_SERVER['HTTP_REFERER'] == "http://www.domain.com/mainpage.php") { 
    //do your username check 
} 
else if ($_SERVER['HTTP_REFERER'] == "http://www.domain.com/profile.php") { 
    //do your comment check 
} 

另一個也許更簡單的方法是用username打交道時。像這樣,以確保您的$_POST['comment']尚未設定:

if (!isset($_POST['comment'])) { 
    $another_user = $_GET['username'];//gets a value from mainpage.php 
    $check = mysql_query("SELECT * FROM users WHERE user_name = '".$another_user."'"); 
    $sent = mysql_fetch_array($check); 
    if(!$sent) { 
     header('Location: mainpage.php'); 
     exit(); 
    } 
} 
else { 
    //... 
}