Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/176692-required-to-login-help/
Share on other sites

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.

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.

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

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.

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.

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.