dflow Posted January 2, 2011 Share Posted January 2, 2011 this code works if i use the header() in the else section it doesnt but it will echo the commented part :confused: <? /** * User has already logged in, so display relevant links, including * a link to the admin center if the user is an administrator. */ if($session->logged_in){ echo "<li>Logged In "; echo "<b>$session->username</b></li>"; if($session->isAdmin()){ echo "<li><a href=\"LoginSystem/admin/admin.php\">Admin Center</a></li>"; } echo "<li><a href=\"../LoginSystem/process.php\">Logout</a</li>"; } else{header("Location: ../LoginSystem/main.php"); //echo "<li><a href=\"../LoginSystem/process.php\">Login</a></li>"; } ?> Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/ Share on other sites More sharing options...
BlueSkyIS Posted January 2, 2011 Share Posted January 2, 2011 where is $session defined? use <?php instead of <? use exit after header() Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1153865 Share on other sites More sharing options...
fortnox007 Posted January 2, 2011 Share Posted January 2, 2011 aren't the headers already sent after you echo out? Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1153883 Share on other sites More sharing options...
dflow Posted January 3, 2011 Author Share Posted January 3, 2011 $session is defined before with : include("include/session.php"); the exit() as you suggested is executed it just doesnt redirect Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154142 Share on other sites More sharing options...
dflow Posted January 3, 2011 Author Share Posted January 3, 2011 aren't the headers already sent after you echo out? in case the user is not logged in then no Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154144 Share on other sites More sharing options...
the182guy Posted January 3, 2011 Share Posted January 3, 2011 Try without using the relative path in the header. Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154145 Share on other sites More sharing options...
Pikachu2000 Posted January 3, 2011 Share Posted January 3, 2011 Do you have error reporting at maximum, and display_errors on? Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154178 Share on other sites More sharing options...
dflow Posted January 3, 2011 Author Share Posted January 3, 2011 Try without using the relative path in the header. doesnt help Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154201 Share on other sites More sharing options...
dflow Posted January 3, 2011 Author Share Posted January 3, 2011 Do you have error reporting at maximum, and display_errors on? display_errors STDOUT error_reporting 6135 Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154202 Share on other sites More sharing options...
jake2891 Posted January 3, 2011 Share Posted January 3, 2011 just remove the line above your opening php tag and place the php tag right at the top with no space and it should be fine. Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154246 Share on other sites More sharing options...
dflow Posted January 3, 2011 Author Share Posted January 3, 2011 just remove the line above your opening php tag and place the php tag right at the top with no space and it should be fine. nada removed the line and still not redirecting Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154296 Share on other sites More sharing options...
Pikachu2000 Posted January 3, 2011 Share Posted January 3, 2011 Is there anything at all on the page when the redirect fails? Perhaps a logout link? Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154313 Share on other sites More sharing options...
dflow Posted January 3, 2011 Author Share Posted January 3, 2011 Is there anything at all on the page when the redirect fails? Perhaps a logout link? no but if i try echo : echo "<li><a href=\"../LoginSystem/process.php\">Login</a></li>"; it is shown Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154320 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 header('Location: ') needs absolute url to work if we are specific. So use: header('Location: http://mysite.com/loginsystem/main.php'); Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154322 Share on other sites More sharing options...
Pikachu2000 Posted January 3, 2011 Share Posted January 3, 2011 Comment out the current code, and put this in its place. <?php /** * User has already logged in, so display relevant links, including * a link to the admin center if the user is an administrator. */ if($session->logged_in){ echo "<li>Logged In "; echo "<b>$session->username</b></li>"; if($session->isAdmin()){ echo "<li><a href=\"LoginSystem/admin/admin.php\">Admin Center</a></li>"; } echo "<li><a href=\"../LoginSystem/process.php\">Logout</a</li>"; } else { //header("Location: ../LoginSystem/main.php"); //echo "<li><a href=\"../LoginSystem/process.php\">Login</a></li>"; if( headers_sent() ) { echo 'Headers already sent.'; } else { echo 'Headers not yet sent.'; } } ?> Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154332 Share on other sites More sharing options...
jcbones Posted January 3, 2011 Share Posted January 3, 2011 Headers will not re-direct in Chrome, unless you send a Status: 200 with it. Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154393 Share on other sites More sharing options...
dflow Posted January 3, 2011 Author Share Posted January 3, 2011 Comment out the current code, and put this in its place. <?php /** * User has already logged in, so display relevant links, including * a link to the admin center if the user is an administrator. */ if($session->logged_in){ echo "<li>Logged In "; echo "<b>$session->username</b></li>"; if($session->isAdmin()){ echo "<li><a href=\"LoginSystem/admin/admin.php\">Admin Center</a></li>"; } echo "<li><a href=\"../LoginSystem/process.php\">Logout</a</li>"; } else { //header("Location: ../LoginSystem/main.php"); //echo "<li><a href=\"../LoginSystem/process.php\">Login</a></li>"; if( headers_sent() ) { echo 'Headers already sent.'; } else { echo 'Headers not yet sent.'; } } ?> i get Headers already sent. Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154409 Share on other sites More sharing options...
dflow Posted January 3, 2011 Author Share Posted January 3, 2011 Headers will not re-direct in Chrome, unless you send a Status: 200 with it. ok thanks Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154410 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 Then you cannot send anymore headers. You have to check your code so that you don't echo anything to the page. You may not have any static html before the redirect either. Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154411 Share on other sites More sharing options...
Pikachu2000 Posted January 3, 2011 Share Posted January 3, 2011 Nothing at all, not even white space can be sent to the browser before a header() redirect. You'll have to figure where the output is coming from and kill it before the redirect will work. Is there anything in a View -> Page Source? Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154414 Share on other sites More sharing options...
dflow Posted January 4, 2011 Author Share Posted January 4, 2011 ok thanks, it redirects now but this means i need to divide up an include file and set on top of each page in the system this header any work arounds? Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154592 Share on other sites More sharing options...
johnny86 Posted January 4, 2011 Share Posted January 4, 2011 Use output buffering and dump everything at the end of your script. ob_start() - http://php.net/ob_start ob_end_flush() - http://php.net/ob_start Link to comment https://forums.phpfreaks.com/topic/223192-header-redirect-not-working-no-error/#findComment-1154820 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.