• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Simple PHP Login Check

#1
Hi,

I need a simple PHP that checks if the a member is logged in and from usergroup 2,3 or 4. If not, then redirect to the page where it says "You must be logged in to do that". (Kind of like trying to download an attachment as a guest)

Will pay via PayPal.

Thanks.
OnyxChase
 
F

Floris

Guest
#2
PHP:
if ({$visitor.user_id}) { // user is logged in } else { // it is a guest }
^ from memory, untested. Hope it's what you're looking for.
 
#3
Thank you Floris. Do you know which file I must include in the PHP? I want to use it for a separate section of my site but make sure the user is logged in.
 
F

Floris

Guest
#4
No, I don't know your private code. But after hooking into the framework, that var is available to you.
 
#5
Thank you again Floris. Sorry, I only know basic PHP. In vBulletin I would do require_once('./global.php') to get in, but how would I hook in with XenForo?
 
R

ragtek

Guest
#9
You have to include the necessary XF files too, also you have to create the $visitor object^^

PHP:
$startTime = microtime(true);
$fileDir = dirname(__FILE__);

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);


$visitor = XenForo_Visitor::getInstance();
And then you can use the code providen by floris.


Edit: oh too slow:(
 
#10
Ah thank you Ragtek for your help. Here is my current code:

PHP:
<?php
$startTime = microtime(true);
$fileDir = '/home/user/public_html/community';

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

XenForo_Session::startPublicSession();

$visitor = XenForo_Visitor::getInstance();

$user_id = $visitor->getUserId();

$url = "http://" . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];

if ($user_id) {
    echo 'you are not guest';
} else {
    header("Location: http://www.website.com/community/login?redirect=$url");
}
?>
However, how do I give the error message that says "You must be logged in for that" and redirecting after logging in? I've attached an image of what I mean.
 

Attachments

#11
Hi. My script works perfectly, but I have one last problem. If my member stays in there too long without going back to the forums, their session expired. Is there a way to keep the session active? And is it possible to set the Location so we know where they are in Activity?

Sorry I don't know much about PHP. I'm learning tho! Thanks again for your help!
 
R

ragtek

Guest
#12
IMHO you should create an own Controller for this, then you could use the full power of the XF framework;) (for activity, for error output,etc...)