John x
John x

Reputation: 4031

header('Location:') not redirecting

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

Upvotes: 1

Views: 3698

Answers (4)

Nimezzz
Nimezzz

Reputation: 1872

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.

Upvotes: 0

Paul
Paul

Reputation: 141827

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.

Upvotes: 2

ajtrichards
ajtrichards

Reputation: 30565

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

Upvotes: 2

user267885
user267885

Reputation:

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

Upvotes: 1

Related Questions