viperjts10 Posted July 15, 2010 Share Posted July 15, 2010 I've always never understood the use of headers. All I know is that it has to be the first thing output using php, and there must be no white space etc.. But how can that be if I use the header in an 'if' statement or whatnot. Here's what I'm trying to do...I'm using a simple 'if' statement to see if the user logged out: if($session->is_logged_in()) { /* Kill session variables */ $session->logout(); //$_SESSION = array(); // reset session array redirect_to('index.php'); //echo "<meta http-equiv=\"Refresh\" content=\"0;url=index.php\">"; } My redirect function is below: function redirect_to($location = NULL) { if ($location != NULL) { header("Location: {$location}"); exit; } } How can I possibly use a header without having any whitespace before hand if I need to test a condition first? Quote Link to comment Share on other sites More sharing options...
dreamwest Posted July 15, 2010 Share Posted July 15, 2010 function redirect( $url ){ if (! headers_sent( ) ){ header( "Location: ".$url ); exit( 0 ); } echo "<script language=Javascript>document.location.href='".$url."';</script>"; exit( 0 ); } redirect('http://wizecho.com'); //redirect to any url Quote Link to comment Share on other sites More sharing options...
Fergal Andrews Posted July 15, 2010 Share Posted July 15, 2010 Hi viperjts10, I ran your code and the redirection worked for me. The only thing i added was a class to create the session object with methods is_logged_in() and logout() both of which returned true. I'm not sure why you are having problems but it must be something in the code preceeding the snippets you posted. The full code I used is below. All the best, Fergal <?php class session { function is_logged_in() { return true; } function logout() { return true; } } $session = new session(); if($session->is_logged_in()) { /* Kill session variables */ $session->logout(); //$_SESSION = array(); // reset session array redirect_to('index.php'); //echo "<meta http-equiv=\"Refresh\" content=\"0;url=index.php\">"; } function redirect_to($location = NULL) { if ($location != NULL) { header("Location: {$location}"); exit; } } Quote Link to comment Share on other sites More sharing options...
j05hr Posted July 15, 2010 Share Posted July 15, 2010 I've had this problem before, I would use the code on my own hosting and when I go to use it for a client it doesn't work with the exact same code. I think it might have something to do with their hosting. Quote Link to comment Share on other sites More sharing options...
Adam Posted July 15, 2010 Share Posted July 15, 2010 It's nothing to do with the server. When you send output to the browser (even white-space) the response headers (your redirects, cookies, etc.) are sent first. This means once output has been sent, you're unable to set anymore response headers. I don't know if this is just from copying the code into the post, but I can see an empty line above your PHP tag. quite possibly this is the cause of your problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.