简体   繁体   English

表单提交后PHP标头重定向不起作用

[英]PHP Header redirect after form submit not working

I have a snippet of code that posts information the user enters into a form, to the DB. 我有一段代码将用户输入的信息发布到数据库中。 After the last line of PHP that submits the data to the DB I have: 在将数据提交到数据库的PHP的最后一行之后,我有:

header("location:http://www.site.com/page.htm");

This doesn't seem to do anything. 这似乎无能为力。 It still posts all the user info to the DB, but the page clears out and thats it. 它仍然将所有用户信息发布到数据库,但是页面被清除,仅此而已。 No redirect. 没有重定向。

Here is the whole snippet: 这是整个代码段:

<?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");
        }
    }
}
?>

and yes I know, I should be using some SQL injection protection but cant get that working just yet.. that will come later ;) 是的,我知道,我应该使用一些SQL注入保护,但是暂时无法正常工作..稍后再说;)

Change 更改

header("location:http://www.site.com/page.php");

to

header("location: http://www.site.com/page.php");

Also, if u even have one character outputed before the header command (ie body of the http request) it will fail. 同样,如果您甚至在标头命令(即http请求的正文)之前输出了一个字符,它将失败。 Turn on PHP error reporting to see if this is the case. 打开PHP错误报告以查看是否存在这种情况。

Output characters can even be white chars, something between the <?php and the beginning of the page. 输出字符甚至可以是白色字符,介于<?php和页面开头之间。

Like: ======page starts below this line======= 像:======页面从此行下面开始=======

   <?php  

Notice there are white characters before the <?php 请注意<?php之前有白色字符

If your header isn't working, it is because your script produced an output (eg an error message, because of unchecked GET parameters), before the header is sent. 如果您的标头不起作用,那是因为您的脚本在发送标头之前产生了输出(例如,由于未选中GET参数而导致的错误消息)。 Check your webserver's error log, which should contain the information, what exactly fails. 检查您的网络服务器的错误日志,其中应包含信息,究竟是什么导致失败。

All headers need to be sent to the browser before any other type of output (error messages, HTML, Javascript, etc). 在所有其他类型的输出(错误消息,HTML,Javascript等)之前,所有标头都需要发送到浏览器。

In your case, you have a lot going on in the script before you call header() , and one or more of those lines of code are generating output. 在您的情况下,在调用header()之前,脚本中有很多事情要做,并且其中的一行或多行代码正在生成输出。 Once that happens, you can no longer send header information. 一旦发生这种情况,您将无法再发送标头信息。

To fix the problem, determine what is generating output and remove it, or move the header() call nearer to the top of the script before any echo statements, mysqli* statements, or any other line that can produce output. 要解决该问题,请确定正在生成输出的内容并将其删除,或者将header()调用移至脚本顶部附近,然后再执行任何echo语句, mysqli*语句或任何其他可能产生输出的行。

Got it to work using: 使用以下命令使其正常工作:

$url = '/page.php';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM