gilgimech Posted March 26, 2010 Share Posted March 26, 2010 I'm trying to detect if the user is a guest or is logged on. If the user is logged on, they will be redirected to the main page. If the user is a guest it'll stay on the front page. here's my code. 6. if ($context['user']['is_guest']) { 7. break; 8. } else { 9. header("Location:http:main_page.php"); 10. } When I run the page I get the error: Fatal error: Cannot break/continue 1 level in /home/netgame2/public_html/index.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/196646-break-error/ Share on other sites More sharing options...
mikesta707 Posted March 26, 2010 Share Posted March 26, 2010 um.. break commands are used in switch() statements, not in if statements. You can't really break out of an if statement. just remove the break, and the code should work as expected, though it could be simplified to if (!$context['user']['is_guest']) { header("Location:http:main_page.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/196646-break-error/#findComment-1032435 Share on other sites More sharing options...
gilgimech Posted March 26, 2010 Author Share Posted March 26, 2010 If I remove the break. It'll start a redirect loop. Can't use the code you suggested because it also causes a redirect loop. Quote Link to comment https://forums.phpfreaks.com/topic/196646-break-error/#findComment-1032448 Share on other sites More sharing options...
mikesta707 Posted March 26, 2010 Share Posted March 26, 2010 um.. no it doesn't. an if statement doesn't loop.... Quote Link to comment https://forums.phpfreaks.com/topic/196646-break-error/#findComment-1032449 Share on other sites More sharing options...
gilgimech Posted March 26, 2010 Author Share Posted March 26, 2010 Yes it does. Maybe it's not supposed to, but it does. http://www.netgamegurus.com/index.php Quote Link to comment https://forums.phpfreaks.com/topic/196646-break-error/#findComment-1032469 Share on other sites More sharing options...
mikesta707 Posted March 26, 2010 Share Posted March 26, 2010 that has nothing to do with the if statement. break is completely invalid in that context. can you post the rest of the code? Quote Link to comment https://forums.phpfreaks.com/topic/196646-break-error/#findComment-1032470 Share on other sites More sharing options...
PFMaBiSmAd Posted March 26, 2010 Share Posted March 26, 2010 It's likely that your session or the $context variable is not working and you would need to troubleshoot that problem first before you worry about getting a header() redirect to work. You also need an exit; statement after your header redirect to prevent the remainder of the code on the page from being executed. For all we know you have code on that page that is causing the current visitor to be logged out so when the code in main_page.php is executed it causes a redirect to the code you just did post. Edit: I guess I'll add that your redirect is somewhat backwards. You would normally only redirect as part of a log in script if the visitor that requested a page is not-logged in and you would stay on the requested page if the visitor is logged in. If the code on main_page.php is checking if the current visitor is logged in, but your session is not working, then yes you probably do have a redirect loop. Quote Link to comment https://forums.phpfreaks.com/topic/196646-break-error/#findComment-1032472 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.