WordPress.org

Ready to get started?Download WordPress

Forums

Check for login outside WordPress (25 posts)

  1. Paul Ille
    Member
    Posted 8 years ago #

    Hi,

    I've read some posts and did some fiddling but I can't seem to get things going. I'm integrating WP into an existing webpage and from that page I'm trying to check on when a user is logged in.

    I'm using the following code.

    <?php global $user_ID; get_currentuserinfo();


    if ($user_ID)
    {
    echo "logged in";
    }
    else
    {
    echo "logged out";
    }
    ?>

    When I use this in sidebar.php to test it it works fine, but on my test page it does not.

    What am I missing? I have the


    <?php $blog = 1; require('wordpress/wp-blog-header.php'); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    at the top of the page.

  2. Paul Ille
    Member
    Posted 8 years ago #

    Any help is much appreciated.

  3. Paul Ille
    Member
    Posted 8 years ago #

    Ok, where is the get_currentuserinfo(); function defined? Do I have to include that page?

  4. Paul Ille
    Member
    Posted 8 years ago #

    Ok, well I tried to include that page, but it gave me an error. Something tells me having the wp-blog-header.php included is enough, but then why is it not able to get userdata??

    It works fine on the sidebar.php.

    It would really be appreciated if someone has any idea at all. I know the solution is probably simple, but I'm just unaware of it.

  5. Paul Ille
    Member
    Posted 8 years ago #

    Once again, the get_currentuserinfo is not pulling any info for me.


    <?php global $user_ID; get_currentuserinfo();

    if ($user_ID)
    {
    echo "logged in";
    }
    else
    {
    echo "logged out";
    }
    ?>

    Does this not work outside of WordPress?
    Do I need to include something that I'm not?

    Anyone?

  6. Paul Ille
    Member
    Posted 8 years ago #

    Not one person here has any idea about this?

  7. Kafkaesqui

    Posted 8 years ago #

    You're duplicating what a number of WordPress internals accomplish, so I can't say where it's going south on you. Does this page appear on the exact same domain as your blog?

    One thing to try is to nix get_currentuserinfo() in your statement:

    <?php
    global $user_ID;
    if ($user_ID) {
    echo "logged in";
    } else {
    echo "logged out";
    }
    ?>

    If that fails, replace $user_ID in your test with $user_level or $user_login. Any of these should be available as global vars to a logged-in visitor of the blog.

  8. Paul Ille
    Member
    Posted 8 years ago #

    I put up a quick testing page with a few variables, but none of them seem to display info.

    Here are the tests I used:
    <?php global $user_ID; get_currentuserinfo();

    if ($user_ID)
    {
    echo "logged in";
    }
    else
    {
    echo "logged out";
    }
    ?>

    Should show username, userlevel and user id:
    <?php global $userdata;
    get_currentuserinfo();

    echo('Username: ' . $userdata->user_login . '\n');
    echo('User level: ' . $userdata->user_level . '\n');
    echo('User ID: ' . $userdata->ID . '\n');
    ?>

    Should show user nickname and their email address:
    <?php global $user_nickname, $user_email;
    get_currentuserinfo();

    echo($user_nickname . "'s email address is: " . $user_email);
    ?>

    That page is here - http://www.paulille.com/testing.php

    Here's the wordpress - http://www.paulille.com/wordpress/ The very same code works fine in the sidebar.

    I also have the include line at the top of the pages
    <?php $blog = 1; require('wordpress/wp-blog-header.php'); ?>

    Any ideas?

  9. Paul Ille
    Member
    Posted 8 years ago #

    This issue is really annoying. There are so many things I can't do unless I solve this.

    It must be something stupid, but I can't figure it out.

  10. Paul Ille
    Member
    Posted 8 years ago #

    I tried to use the_excerpt_reloaded mod and when I do that doesn't work on my index page either.

    Here's the code on my index page http://www.paulille.com/files/index.txt
    (rightclick save target etc)

    Anyone see anything that could be stopping me from using some things on my site?

  11. jaredquinn
    Member
    Posted 8 years ago #

    I think using wp-blog-header.php may be a little overkill for what you want to do, as this file handles all of the wordpress internals (such as switching between pages/posts/categories etc) it also handles the permalink re-writes etc.

    Something as simple as this:

    <?php
    require_once('wp-config.php');
    get_currentuserinfo();
    print "$user_login<br/>$user_ID<br/>$user_level<br/>";
    ?>

    works fine for me (it's in my wordpress directory as that is the root of the site), but i can't see why it wouldn't work in your situation.

    cheers.
    jared.

  12. Paul Ille
    Member
    Posted 8 years ago #

    Yeah thx man, but that doesn't work either. I just don't get it. What in the world would prevent this from working?

    It's silly to me that it doesn't work outside of the wordpress folder even with the proper includes.

  13. rustin
    Member
    Posted 8 years ago #

    I'm just bumping this topic becuase I'm trying to do the very same thing... with the very same results. Any experts have input?

  14. jaredquinn
    Member
    Posted 8 years ago #

    I'm happy to have a closer look at your setups, but it may be easier if you contact me outside the forums.

    You can find my contact details on my website if you follow the link from my profile.

  15. rustin
    Member
    Posted 8 years ago #

    I don't know how, but between last night and today it's sterted working. I have a test page created in the home directory (same as my wordpress install) and it displays the login name of the person logged in and nothing if the user is not logged in.

  16. Paul Ille
    Member
    Posted 8 years ago #

    Mine still doesn't work, but I haven't tried in a while. Seems rustin is working from inside the WordPress folder while mine is outside. I can get it to work inside of that folder just fine it seems.

    Gonna fire you off an email jaredquinn, appreciate the offer.

  17. Paul Ille
    Member
    Posted 8 years ago #

    Check out how silly this is. Here is the same exact page in two different locations. The only difference is the include line.

    http://www.paulille.com/testing.php
    <?php $blog = 1; require('wordpress/wp-blog-header.php'); ?>

    http://www.paulille.com/wordpress/testing.php
    <?php $blog = 1; require('wp-blog-header.php'); ?>

    You'll see the one inside the WordPress folder works fine, but that doesn't make any sense at all.

  18. Paul Ille
    Member
    Posted 8 years ago #

    Well, emailed you the files jaredquinn at your request and never heard back. I guess you didn't find anything.

  19. cam_oai
    Member
    Posted 8 years ago #

    has anyone solved this problem yet? within the wp folder is ok but not outside....

  20. Paul Ille
    Member
    Posted 8 years ago #

    I haven't heard a thing and jaredquinn never responded to my follow up email after his initial "yeah send me the files"

  21. ed_td
    Member
    Posted 8 years ago #

    well it took a while but here's a solution (and a more specific description of the problem). i have wordpress installed in a folder in my root directory named wordpress. also in the root directory is a file called test.php with this content:

    <?php
    require_once('wordpress/wp-config.php');
    get_currentuserinfo();
    print "$user_login<br/>$user_ID<br/>$user_level<br/>";
    ?>

    the reason this doesn't work is because the function get_currentuserinfo() always returns false before setting the user info variables. it does a check to see if the correct cookies are set, and when it can't find them, you lose. the reason no cookies are found is because the cookies are set with the path of your wordpress install, and so are not sent unless accessing a page in the wordpress directory. I KNOW NOTHING ABOUT COOKIES except what i've figured out experimenting over the last few hours - this may be incredibly insecure. that said, a workaround is to set extra cookies. there are two function dealig with cookies (wp_setcookie() and wp_clearcookie()), both in [wordpress root directory]/wp-includes/pluggable-functions.php.

    to wp_setcookie() i added the two lines:
    setcookie(USER_COOKIE, $username, $expire, '/',
    COOKIE_DOMAIN);
    setcookie(PASS_COOKIE, $password, $expire, '/',
    COOKIE_DOMAIN);
    after the lines:
    setcookie(USER_COOKIE, $username, $expire, $cookiepath,
    COOKIE_DOMAIN);
    setcookie(PASS_COOKIE, $password, $expire, $cookiepath,
    COOKIE_DOMAIN);

    and the lines:
    setcookie(USER_COOKIE, $username, $expire, '/',
    COOKIE_DOMAIN);
    setcookie(PASS_COOKIE, $password, $expire, '/',
    COOKIE_DOMAIN);

    after
    setcookie(USER_COOKIE, $username, $expire,
    $sitecookiepath, COOKIE_DOMAIN);
    setcookie(PASS_COOKIE, $password, $expire,
    $sitecookiepath, COOKIE_DOMAIN);

    to wp_clearcookie() i added the following lines at the end:
    setcookie(USER_COOKIE, ' ', time() - 31536000, '/',
    COOKIE_DOMAIN);
    setcookie(PASS_COOKIE, ' ', time() - 31536000, '/',
    COOKIE_DOMAIN);

    seems to work - if ive done something horrible id appreciate anyone pointing out...

  22. jenniemai
    Member
    Posted 7 years ago #

    I was having the same problems when I found this post. Another alternative to Ed's solution is to change the wp-settings.php file. You can change the COOKIEPATH and SITECOOKIEPATH to point to your root site. This way, the cookies are found outside the blog directory as long as you are in your root site.

    change:
    define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_settings('home') . '/' ) );

    to:
    define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', 'your root site url' ) );

    changed:
    define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_settings('siteurl') . '/' ) );
    to:
    define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', 'your root site url' ) );

    Disclaimer: I know very little about cookies also, so I don't know if this exposes any harmful behavior. It worked for me, though.

    If anyone knows the difference between COOKIEPATH and SITECOOKIEPATH, I appreciate any explanations. Please let me know if there is a better way of solving this problem.

  23. Paul Ille
    Member
    Posted 7 years ago #

    First I have to say thanks you guys. As you can see this topic was open for 9 months hehe

    I found this which might make changing this path much easier. I haven't tried it yet.

    http://www.linickx.com/blog/archives/126/cookie-path-plugin-for-wordpress-20-root-cookie/

    Also I wasn't able to find a thing about sitecookiepath.

  24. Yofazza
    Member
    Posted 7 years ago #

    I was also having the same problem. After reading many posts including posts in this thread (which some of them works), instead of changing the core files, I tried changing the Blog Address (URI) in General Options, while the WordPress address (URI) remained pointing to the folder where WordPress installed.

    It seems to work now (yay!), a test.php page in another folder (not in the root folder) which contains:

    require('../blog/wp-blog-header.php');
    $user = wp_get_current_user();
    echo '<pre>' . print_r ($user, true) . '</pre>';

    ...is now working well on login/logout state.

    /blog is the folder where WordPress got installed.

    Proceeding with works... I'll let you people know if this method has any flaw. 0_o

  25. Yofazza
    Member
    Posted 7 years ago #

    Big problem

    the whole path (like in categories, posts) now points to the website root :|

    nvm about my post above, silly solution. stick to ed or jenniemai's above...

Topic Closed

This topic has been closed to new replies.

About this Topic