Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/196646-break-error/
Share on other sites

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");
}

Link to comment
https://forums.phpfreaks.com/topic/196646-break-error/#findComment-1032435
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/196646-break-error/#findComment-1032472
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.