WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Setting Cookie from Outside WordPress (5 posts)

  1. flicknut
    Member
    Posted 7 years ago #

    I have my main site at http://www.fantasyblitz.com, and then I have my blog at http://www.fantasyblitz.com/blog/.

    When users register on my main site, it creates two accounts: one in my main database and one in the WordPress database. The accounts share the same username and password.

    Now when users login to my main site, I want to set the WordPress cookie so they don't have to login again on my blog. How can I do this? Thanks.

  2. flicknut
    Member
    Posted 7 years ago #

    Okay here is the solution:


    // Begin WordPress Cookie Handling require_once("blog/wp-includes/pluggable-functions.php");
    $siteurl = "http://www.fantasyblitz.com";
    $blogurl = "http://www.fantasyblitz.com/blog";
    $cookiehash = md5($blogurl);
    define('COOKIEHASH', $cookiehash);
    define('USER_COOKIE', 'wordpressuser_'. COOKIEHASH);
    define('PASS_COOKIE', 'wordpresspass_'. COOKIEHASH);
    define('COOKIE_DOMAIN', false);
    wp_setcookie($user_username, $login_password, false, $siteurl, $siteurl, $remember);
    // End WordPress Cookie Handling

    Firefox is great for troubleshooting problems with your cookies. Just login to WordPress the regular way, and then compare that cookie to the one you are trying to create outside of WordPress.

  3. moshu
    Member
    Posted 7 years ago #

    As it usually happens... after 5 months somebody will find this thread and will desperately asking you: where to put this code? :)

  4. flicknut
    Member
    Posted 7 years ago #

    Okay here are some adjustments to the code...there was a bug with destroying the cookie when logging out.

    You put this code immediately after you check authentication on your main site (in this case http://www.fantasyblitz.com/login/):


    // Begin WordPress Cookie Handling require_once("blog/wp-includes/pluggable-functions.php");
    $blogurl = "http://www.fantasyblitz.com/blog";
    $cookiehash = md5($blogurl);
    define('COOKIEHASH', $cookiehash);
    define('USER_COOKIE', 'wordpressuser_'. COOKIEHASH);
    define('PASS_COOKIE', 'wordpresspass_'. COOKIEHASH);
    define('COOKIE_DOMAIN', false);
    wp_setcookie($user_username, $login_password, false, $blogurl, $blogurl, $remember);
    // End WordPress Cookie Handling

    Here's the code for logging out:


    // Begin WordPress Cookie Handling
    require_once("blog/wp-includes/pluggable-functions.php");
    $blogurl = "http://www.fantasyblitz.com/blog";
    $cookiehash = md5($blogurl);
    define('COOKIEHASH', $cookiehash);
    define('USER_COOKIE', 'wordpressuser_'. COOKIEHASH);
    define('PASS_COOKIE', 'wordpresspass_'. COOKIEHASH);
    define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', $blogurl . '/' ) );
    define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', $blogurl . '/' ) );
    define('COOKIE_DOMAIN', false);
    wp_clearcookie();
    // End WordPress Cookie Handling

  5. treelovinhippie
    Member
    Posted 6 years ago #

    Bah, this isn't working for me... any ideas?

    I'm trying to integrate the login in phpizabi and wordpress.

Topic Closed

This topic has been closed to new replies.

About this Topic