Take the 2-minute tour ×

I have a page I want to password-protect. I've tried the code below, but I am unable to put logout to the page. Any other quick (and easy) way to do this? Thanks!

How I can add logout to this password protected page as described below

$username = "the_username_here";
$password = "the_password_here";
$nonsense = "supercalifragilisticexpialidocious";

if (isset($_COOKIE['PrivatePageLogin'])) {
if ($_COOKIE['PrivatePageLogin'] == md5($password.$nonsense)) {


} else {
  echo "Bad Cookie.";

if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST['user'] != $username) {
  echo "Sorry, that username does not match.";
 } else if ($_POST['keypass'] != $password) {
  echo "Sorry, that password does not match.";
 } else if ($_POST['user'] == $username && $_POST['keypass'] == $password) {
  setcookie('PrivatePageLogin', md5($_POST['keypass'].$nonsense));
  header("Location: $_SERVER[PHP_SELF]");
 } else {
  echo "Sorry, you could not be logged in at this time.";

And the login form on the page...

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
<label><input type="text" name="user" id="user" /> Name</label><br />
<label><input type="password" name="keypass" id="keypass" /> Password</label><br />

Any clue on how to get this solved ?

share|improve this question
So you want to add a logout onto a login page? That is... kinda baffling, actually. –  Palladium Aug 1 '12 at 14:58
+1 for common sense, @Palladium. OP, a user can't log out if they're not logged in. –  Matt Aug 1 '12 at 15:00
I want to logout after the user logs in some where in "LOGGED IN CONTENT HERE" –  user1561466 Aug 1 '12 at 15:04
In that case, you can create a logout button which sends, for example, $_GET['cancel'] back to the same page, and then have the page expire the cookie and refresh if $_GET['cancel'] is true. Of course, with your current design, the page loads twice for each login and twice again for each logout. –  Palladium Aug 1 '12 at 15:10
add comment

3 Answers

up vote 3 down vote accepted

On any page

<?php if(isset($_COOKIE['PrivatePageLogin'])):?>
<?php endif?>


    // delete cookie
    setcookie('PrivatePageLogin', null, time() - 1);
    // if you use sessions delete session variables as well
header('Location: index.php');
share|improve this answer
Thanks it works –  user1561466 Aug 1 '12 at 18:27
add comment

Simply provide a link to a page which clears the cookie you've set.

As a side note, saving the password in md5 alone in the client side is not secure whatsoever.

share|improve this answer
so whats the other method ? –  user1561466 Aug 1 '12 at 15:50
@user1561466: stackoverflow.com/q/4795385/871050 –  Second Rikudo Aug 2 '12 at 13:02
add comment

This doesn't answer the question of user1561466. With this answer I show you how one could use the password protected feature of WP with a logout button (without plugins):



if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
header('Location: index.php');


Perhaps you could only delete only one cookie, but this is how I can independently of WP delete the cookie.

In page.php I have this:



            if (isset($_COOKIE['wp-postpass_' . COOKIEHASH]) && wp_check_password( $post->post_password, $_COOKIE['wp-postpass_' . COOKIEHASH])) {
                echo '<a href="/logout.php">Logout</a><br /><br />';

Sources: how to delete all cookies of my website in php, http://wordpress.org/support/topic/check-in-the-loop-if-a-post-is-password-protected

share|improve this answer
add comment

Your Answer


