我有一段代碼將用戶輸入到表單中的信息發佈到數據庫。在將數據提交到數據庫的最後一行PHP後,我有:PHP表頭提交後不工作的PHP頭重定向
header("location:http://www.site.com/page.htm");
這似乎沒有任何作用。它仍然會將所有用戶信息發佈到數據庫,但該頁面會清除併發布它。沒有重定向。
這裏是整個片段:
<?php
if(isset($_POST['register'])){
$username = ($_POST['username']);
$password = ($_POST['password']);
$email = ($_POST['email']);
$email2 = ($_POST['email2']);
$password2 = ($_POST['password2']);
$commname = ($_POST['commname']);
$outpostname = ($_POST['outpost']);
$special = ($_POST['specialty']);
if($username == "" || $password == "" || $email == "" || $password2 == "" || $email2 == "" || $commname == "" || $outpostname == "" || $special == "no"){
echo "Please supply all required fields!";
}elseif(strlen($username) > 20){
echo "Username must be less than 20 characters!";
}elseif(strlen($email) > 100){
echo "E-mail must be less than 100 characters!";
}elseif(strlen($email2) > 100){
echo "E-mail verify must be less than 100 characters!";
}elseif(strlen($commname) > 20){
echo "Your Commander's name must be less than 20 characters!";
}elseif(strlen($outpostname) > 20){
echo "Your Outpost's name must be less than 20 characters!";
}else{
$register1 = mysqli_query($myConnection,"SELECT `id` FROM `user` WHERE `username`='$username'") or die(mysqli_error($myConnection));
$register2 = mysqli_query($myConnection,"SELECT `id` FROM `user` WHERE `email`='$email'") or die(mysqli_error($myConnection));
if(mysqli_num_rows($register1) > 0){
echo "That username is already in use!";
}elseif(mysqli_num_rows($register2) > 0){
echo "That e-mail address is already in use!";
}else{
$spec = ($_POST['specialty']);
$comnam = ($_POST['commname']);
$postnam = ($_POST['outpost']);
$rand_col = rand(1,5); /*Number of Colonies that exist in the game*/
$ins1 = mysqli_query($myConnection,"INSERT INTO `stats` (`username`,`credits`,`food`,`land`,`energy`,`turns`,`turns_max`,`gas`,`ore`,`population`,`buildeff`,`offpts`,`defpts`,`score`,`specialty`) VALUES ('$username',2000,2000,100,2000,30,30,2000,2000,500,100,0,0,0,'$spec')") or die(mysqli_error($myConnection));
$ins2 = mysqli_query($myConnection,"INSERT INTO `unit` (`username`,`trainee`,`juggernaut`,`infantry`,`marauder`,`terminator`,`reconsq`,`prisoner`,`destroyer`,`colossus`) VALUES ('$username',100,0,50,0,0,0,0,0,0)") or die(mysqli_error($myConnection));
$ins3 = mysqli_query($myConnection,"INSERT INTO `user` (`colonynum`,`username`,`password`,`email`,`specialty`,`commname`,`outpostname`) VALUES ('$rand_col','$username','".md5($password)."','$email','$spec','$comnam','$postnam')") or die(mysqli_error($myConnection));
$ins4 = mysqli_query($myConnection,"INSERT INTO `structure` (`username`,`agridome`,`spaceport`,`barrack`,`researchfac`,`laserbat`,`factory`,`infirmary`,`trainingfac`) VALUES ('$username',1,0,5,0,0,0,1,1)") or die(mysqli_error($myConnection));
header("location:http://www.site.com/page.php");
}
}
}
?>
是的,我知道,我應該使用一些SQL注入保護,但不能得到,只是還沒有工作..這將留待以後;)
是HTTP報頭是否區分大小寫?或者至少在某些瀏覽器中?事情可以挑剔這樣的東西。試試:'header(「Location:http://www.site.com/page.htm」);' – David
我試過首都,位置和URL之間沒有空格,效果相同。 –
檢查重定向之前什麼都沒有輸出? – JBES