1

我有一个想要密码保护的页面。我已经尝试了下面的代码,但我无法注销页面。还有其他快速(简单)的方法吗?谢谢!

如何将注销添加到此受密码保护的页面,如下所述

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

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


LOGGED IN CONTENT HERE

  exit;
} else {
  echo "Bad Cookie.";
  exit;
}
}

if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST['user'] != $username) {
  echo "Sorry, that username does not match.";
  exit;
 } else if ($_POST['keypass'] != $password) {
  echo "Sorry, that password does not match.";
  exit;
 } 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.";
}
}

页面上的登录表单...

<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 />
<input type="submit" id="submit" value="Login" />
</form>

关于如何解决这个问题的任何线索?

4

3 回答 3

4

在任何页面上

<?php if(isset($_COOKIE['PrivatePageLogin'])):?>
   <a href="logout.php">Logout</a>
<?php endif?>

注销.php

if(isset($_COOKIE['PrivatePageLogin'])){
    // delete cookie
    setcookie('PrivatePageLogin', null, time() - 1);
    // if you use sessions delete session variables as well
}
header('Location: index.php');
于 2012-08-01T15:11:55.237 回答
0

只需提供一个指向清除您设置的 cookie 的页面的链接。

作为旁注,将密码md5单独保存在客户端是不安全的。

于 2012-08-01T15:38:47.443 回答
0

这并不能回答 user1561466 的问题。通过这个答案,我向您展示了如何使用带有注销按钮(不带插件)的 WP 的密码保护功能:

logout.php:

<?php

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');

?>

也许您只能删除一个 cookie,但这就是我可以独立于 WP 删除 cookie 的方式。

page.php我有这个:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php
        if (!empty($post->post_password)) { // if there's a password

            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 />';
            }
        }
    ?>

资料来源:如何在 php 中删除我网站的所有 cookiehttp://wordpress.org/support/topic/check-in-the-loop-if-a-post-is-password-protected

于 2013-07-31T14:17:44.530 回答