-->

header('Location:') not redirecting

2020-04-26 02:32发布

问题:

I have a login page which checks the username and the password and if the credentials provided by the user matches it sets the session and redirects the user, it works on my godadd hosting account but now i have shifted my hosting and suddenly it has stopped working, following is the code

if (count($result) > 0)
            {               
                $row= mysql_fetch_array($result);
                $userID = $row['userID'];               
                $firstName = $row['FirstName'];         

                $isActive = $row['IsActive'];

                if ($isActive == '1')
                {

                    $_SESSION['user'] = $firstName;     
                    //var_dump($firstName); //it gives me the right username                
                    //echo" - here";  //this also gets echoed
                    header("Location: http://google.com/"); //it just dies silently
                    exit;
                }

please help me find out where i went wrong

回答1:

You need to make sure you don't output anything before the header tag. Since it was working before, I assume it's not an error where you are echo'n stuff before the call to header, and since you see no output, I assume it is outputting white-space. There are a few places where some whitespace and/or invisible characters can sneak into a file:

Put the cursor before your opening PHP tag (<? or <?php) and hit backspace a few times, then hit delete to delete the < and then retype the <. That will make sure there are no invisible characters being output to the browser before your headers.

Then go through any files that are included by that file, and do the same thing, as well as delete any ?> that you have at the end of the file. If those files include any othe files repeat this on them.



回答2:

If you want to use header(), you must not echo anything prior to it.



回答3:

Are you outputting anything before calling header()?

If so, put ob_start() at the beginning of your script or make sure you're not echoing anything before header() is called



回答4:

add ob_start(); to the beginning of your script and add ob_end_flush(); to the end of your script to avoid this issue.

ob_start(); will turn output buffering on and ob_end_flush(); will send the contents of the topmost output buffer and turn this output buffer off.