2013-03-16 63 views
1

當我發佈到這個腳本而沒有指定任何POST變量時,腳本仍然會重定向,但仍然會說。當我把die();在第一個if函數之後,頁面實際上被重定向。不等待代碼執行的PHP腳本

$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$email = $_POST['email']; 
$phone = $_POST['phone']; 
$gender = $_POST['gender']; 
$dob = $_POST['dob']; 
$school = $_POST['school']; 
$grade = $_POST['grade']; 
$password = $_POST['password']; 
$password2 = $_POST['password2']; 


$test=FALSE; 
$required = array  ('firstname','lastname','email','phone','gender','dob','school','grade','password','password2'); 

foreach ($required as $value) 
{ 
if(!isset($_POST[$value]) || empty($_POST[$value])) 
{ 
     $test = TRUE; 

} 
} 

if($test) 
{ 
header("location:../register.php?error=1"); 
} 


$id = getRand(9); 
$conn = getConnection(); 

$check =  saveUser($firstname,$lastname,$email,$phone,$gender,$dob,$school,$grade,$password,$id,$conn); 


if($check) 
{ 
header("location:../quiz.php"); 
} 
else 
{ 
header("location:../register.php"); 
} 
+1

你應該看看這個問題 - http://stackoverflow.com/questions/3553698/php-should-i-call-exit-after-calling-location-header - 你應該停止腳本執行後重定向,或者退出;或死() – DrBeza 2013-03-16 20:51:36

回答

1

您需要設置一個die()header(...)命令後,否則一切後,將被處決。像這樣做:

if($test) { 
    header("location:../register.php?error=1"); 
    die(); // Abort everything else 
} 

我不明白你的另一個問題 - 你if ($test)之前重定向?你能爲我提供樣本數據嗎?

+0

我的意思是當我在if函數後面殺死腳本時,它就工作了。所以我想知道在重定向後是否必須殺死腳本。 – 2013-03-16 20:56:05

+0

是的,你必須使用die()或exit()來殺死它 – Stefan 2013-03-17 09:08:24

1

您的header命令不會重定向,只是向瀏覽器發送位置標頭,並且腳本繼續運行。 (然後,瀏覽器在腳本運行後跟隨此位置標題,從而創建重定向。)

但是,如果在第一個標頭(在同一腳本期間)之後發送另一個位置標頭,則此標頭將覆蓋第一個,然後是唯一發送到瀏覽器的。

你需要exitdie之後的標題命令。