doubo1883 2012-08-01 14:56
浏览 46
已采纳

添加注销到密码保护页面

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)) {


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.";
}
}

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

Any clue on how to get this solved ?

  • 写回答

3条回答 默认 最新

  • ds3016 2012-08-01 15:11
    关注

    On any page

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

    logout.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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度