1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. 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

Discussion in 'Add-on Requests [Archive]' started by onyxchase, Feb 15, 2011.

  1. onyxchase

    onyxchase Active Member

    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
     
  2. Floris

    Floris Guest

    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.
     
    onyxchase likes this.
  3. onyxchase

    onyxchase Active Member

    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.
     
  4. Floris

    Floris Guest

    No, I don't know your private code. But after hooking into the framework, that var is available to you.
     
  5. onyxchase

    onyxchase Active Member

    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?
     
  6. onyxchase

    onyxchase Active Member

  7. Floris

    Floris Guest

  8. Floris

    Floris Guest

    Ah, and you found the other thread I was searching for :) Good luck
     
  9. ragtek

    ragtek Guest

    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:(
     
    onyxchase likes this.
  10. onyxchase

    onyxchase Active Member

    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.
     

    Attached Files:

  11. onyxchase

    onyxchase Active Member

    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!
     
  12. ragtek

    ragtek Guest

    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...)
     

Share This Page