• Paul Ille

    (@tunnleram)


    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" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="https://www.w3.org/1999/xhtml">

    at the top of the page.

Viewing 9 replies - 16 through 24 (of 24 total)
  • Thread Starter Paul Ille

    (@tunnleram)

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

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

    https://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.

    Thread Starter Paul Ille

    (@tunnleram)

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

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

    Thread Starter Paul Ille

    (@tunnleram)

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

    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…

    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.

    Thread Starter Paul Ille

    (@tunnleram)

    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.

    https://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.

    RL

    (@yofazza)

    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

    RL

    (@yofazza)

    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…

Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘Check for login outside WordPress’ is closed to new replies.