gilgimech Posted October 6, 2009 Share Posted October 6, 2009 I'm trying to set up my site so users have to be logged into the forum to access the site.I've been working with this bit of code <?php writeLog(); if ($context['user']['is_logged']) ?> <?php else if ($context['user']['is_guest']) { redirectexit('action=login'); } ?> But the problem is that it won't let me stay on the requested page. It'll still redirects even if you are logged in. I want it to check if you are logged in. Then stay on current page as normal, and if you'r not logged in. To redirect you to the login page of the forum. Any help on this would be great. Quote Link to comment https://forums.phpfreaks.com/topic/176692-required-to-login-help/ Share on other sites More sharing options...
Jahren Posted October 6, 2009 Share Posted October 6, 2009 where is context from ? is it a session variable? session_start(); is required to access those vars. using $_SESSION['assoc_array_key'] Quote Link to comment https://forums.phpfreaks.com/topic/176692-required-to-login-help/#findComment-931534 Share on other sites More sharing options...
KevinM1 Posted October 6, 2009 Share Posted October 6, 2009 I'm trying to set up my site so users have to be logged into the forum to access the site.I've been working with this bit of code <?php writeLog(); if ($context['user']['is_logged']) ?> <?php else if ($context['user']['is_guest']) { redirectexit('action=login'); } ?> But the problem is that it won't let me stay on the requested page. It'll still redirects even if you are logged in. I want it to check if you are logged in. Then stay on current page as normal, and if you'r not logged in. To redirect you to the login page of the forum. Any help on this would be great. A couple things: 1. You don't need to exit, then re-enter PHP between statements. You can also get rid of one of your conditionals, simplifying the code. The following is better style: <?php writeLog(); if (!$context['user']['is_logged'] || $context['user']['is_guest']) { redirectexit('action=login'); } ?> 2. This kind of thing is better done with sessions, as they naturally persist from page to page. Quote Link to comment https://forums.phpfreaks.com/topic/176692-required-to-login-help/#findComment-931535 Share on other sites More sharing options...
gilgimech Posted October 6, 2009 Author Share Posted October 6, 2009 Unfortunately I don't know php all that well. I basically know enough to cut and past code and do some minor modifications to it. I'm more of an actionscript guy. This code, I got it from my forum help forum. I don't know enough to write new code using sessions. I was just hoping there would be an easy tweak to this code to keep you on the page instead of redirecting when logged in. Quote Link to comment https://forums.phpfreaks.com/topic/176692-required-to-login-help/#findComment-931561 Share on other sites More sharing options...
Jahren Posted October 6, 2009 Share Posted October 6, 2009 you will need to read some tutorial on the subject unfortunately (or fortunately 'cuz learning is great ^^) Otherwise, you will never learn, and if you don't want to learn, we won't do your homework We are here to help if you have questions tho. http://www.tizag.com/phpT/phpsessions.php Quote Link to comment https://forums.phpfreaks.com/topic/176692-required-to-login-help/#findComment-931563 Share on other sites More sharing options...
gilgimech Posted October 6, 2009 Author Share Posted October 6, 2009 Yeah I am learning. got a Sams teach yourself php book and everything. I'm just under a time resistant to get the site up and running. I'm not looking for a free hand out just some help. From what I know from working with other scripts languages, is that most have a similar structure. I wish I could just find a snippet that says. If logged in run page as normal. I kinda figured that it would be hard for someone to help with my limited php knowledge. But thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/176692-required-to-login-help/#findComment-931572 Share on other sites More sharing options...
PFMaBiSmAd Posted October 6, 2009 Share Posted October 6, 2009 It'll still redirects even if you are logged in. How do you know you are logged in at that point in the code? The posted code is dependent on what the $context['user']['is_logged'] variable has in it. Does it have the value in it that you expect? Have you checked by echoing it? Taken out of context, that few lines of code has no meaning to anyone else but you. How could we possible know what code is setting the $context variable or what value it has in it when you execute the code on your server? It is up to you to troubleshoot what your code is doing on your server. Quote Link to comment https://forums.phpfreaks.com/topic/176692-required-to-login-help/#findComment-931578 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.