3

When you login to a password protected page, WordPress sets a cookie that looks like wp-postpass_hash.

This cookie seems to stick around forever. I'd like to be able to provide the user with a 'log out' link. Is there a way for me to find and delete this particular cookie if I don't know the hash? Maybe there's a way to get the cookie by finding it based on 'wp-postpass'?

I've seen other solutions for altering WordPress' core files to change the expiration date of the cookie, but this won't last when there's an update. Maybe there's a function I could write to alter the expiration of this particular cookie?

Any tips? Thanks!

4 Answers 4

3

That's okay if you don't know the hash. Actually cookie hash is the MD5 of the blog url. If you want to know your site cookie hash then try this:

<?php

    $url="http://www.your-blog.com";
    $COOKIEHASH = md5($url);

    //Now, your cookie will be,
    echo 'wp-postpass_' . $COOKIEHASH; // Name of the cookie of your blog

    //Now, you can delete it whenever you want. :)

    //setting your cookie
    //setcookie('wp-postpass_' . $COOKIEHASH, $password, time() + 864000, COOKIEPATH);
?>
0
0
<?php
$pass_cookie='';
foreach($_COOKIE as $key=>$value){
  if(!strncmp($key,"wp-postpass_",12)){
    $pass_cookie = $key;
    break;
  }
}
if($pass_cookie){
  setcookie($pass_cookie,'',0);
}
?>

I would rather use something like the above - it doesn't depend on the position of the wp_postpass cookie

0

Have you tried providing the user a link to wp_logout_url(), instead of programming it for yourself?

1
  • 4
    wp_logout_url() is for logging out registered users. I'm referring to password protected pages, which is cookie based.
    – HWD
    Feb 5, 2011 at 2:53
0

I ended up doing this:

<?php
// turn all cookies into string
$cookie_string = implode(array_keys($_COOKIE));
// find position of desired cookie
$pos = strpos($cookie_string,'wp-postpass');
// extract string starting at $pos
$pass_cookie = substr($cookie_string,$pos);
// set cookie to expire on browser close
setcookie($pass_cookie,'',0);
?>

This probably isn't the most elegant solution, especially because it requires the wp-postpass cookie to be last in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.