删除PHP中的cookie后,Cookie不会被删除

时间:2015-03-19 13:14:30

标签: php oop cookies

我陷入困境,我只想为我的简单项目创建一个注销脚本,但它不起作用,我不知道为什么。现在这是我的确切问题

我创建了一个登录脚本,该脚本在面向对象的PHP编程中连接到我的数据库。我的课程结构如下:

class Page
      class FetchPage extends Page
            object Auth
      class Auth extends Page
      class ChatCookie extends Page
      class Login extends Page
            object ChatCookie

它的工作原理如下:

  1. FetchPage使用Page class
  2. 的方法构建页面
    function __construct() {
        $this->objAuth = new Auth();
        parent::generateHeader();
        parent::generateNav();
    
        var_dump($_COOKIE);
    
        if($this->load($_GET['ref']) === true) {            
            include_once $_GET['ref'].".php";
        } else {
            echo "Failed loading page contents of function load()..<br />";
        }
    
        parent::generateFooter();           
    }
    
    1. 首先,parent::__constructor();Page的构造函数)被称为
    2. protected function __construct() {
          $this->dbCon = mysqli_connect("127.0.0.1", "root", "", "chat") or die("Error " . mysqli_error($this->dbCon));
      }
      
      1. FetchPage的构造函数创建Auth的对象,因此调用此构造函数
      2. function __construct() {
            parent::__construct();
            if(!isset($_COOKIE['usrID'])) {
                $this->guestip = get_ip_address();
                setcookie("guestip", "$this->guestip", time() + 86400, "/"); // guest ip on visit saved for 1 day
            }
        }
        
        1. 然后调用Page generateHeader()generateNav()generateFooter()的方法并创建页面的基本HTML

        2. 每当我检查访问者是否已登录时,我都会使用这个小片段

        3. if($this->objAuth->isLogged() == true) { // some code }
          
          1. 我有href指向我的文件logout.php,我在其中创建了我的类Login的对象,然后调用Login

            Login

            的构造函数
            function __construct() {
                var_dump($_COOKIE);
                parent::__construct();
                $this->objChatCookie = new ChatCookie();
            }
            

            logout.php

            <?php
            
            if($this->objAuth->isLogged() == true) {
            require_once 'classes/Login.class.php';
            $login = new Login();
                if($login->logoutUsr()) {
                    echo '<div class="alert alert-success"><strong>Success!</strong> You logged out properly from the chat. All the data has been saved and is available to you when you log in next time!</div>';
                    var_dump($_COOKIE);
                } else {
                    echo '<div class="alert alert-danger"><strong>Oh snap!</strong> Something went completly wrong here. We have a <strong><em>false</em></strong> return.. that is harsh. Contact the website administrator please.</div>';
                }
            } else {
                echo '<div class="alert alert-info"><strong>Wait a moment..</strong> How about you first create an account, little fella?</div>';
            }
            
            1. 然后导致创建我的班级ChatCookie
            2. 的对象
              function __construct() {
                  var_dump($_COOKIE);
                  parent::__construct();
                  setcookie("online", false, time() + 86400, "/");
              
                  // @function read() checks if there was a session before for this fUsrID or not
                  if(isset($_COOKIE['PHPSESSID'])) {
                      //$this->readCookie();
                  }
                      $this->tmpCookieString = "";
                  }
              
              1. 然后,我调用名为Login的{​​{1}}中的方法调用logoutUsr()类的方法deleteCookie
              2. logoutUsr()

                ChatCookie

                deleteCookie()

                function logoutUsr() {
                        if($this->objChatCookie->deleteCookie()) {
                            $this->objChatCookie = null;
                            return true;
                        } else {
                            return false;
                        }
                    }
                

                完成所有操作后,我有以下输出:

                Screenshot of output after these operations

                正如您所看到的,我的function deleteCookie() { // to do: query to update the timestampEnd in SQL $saved = false; $deleted = false; /* Save cookie data for later use */ if($this->saveCookie()) { $saved = true; } /* Delete the Cookie and all data */ foreach($_COOKIE as $key => $value) { if(is_array($value)) { // no arrays created yet } setcookie("$key", null, time() - 3600); unset($_COOKIE[$key]); } if(!isset($_COOKIE['usrID'])) { $deleted = true; } if($deleted == true && $saved == true) { $this->objChatCookie = new ChatCookie(); return true; } else { return false; } var_dump($_COOKIE); } 是一个空数组,但在完成所有操作后,cookie将填充之前的所有值和键。我一步一步地经历了一切,尝试了很多,但没有任何效果。

                注销后,我的 nav.html 文件中应该有不同的导航:

                &#13;
                &#13;
                $_COOKIE
                &#13;
                &#13;
                &#13;

                我上传了所有课程, <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <?php if($this->objAuth->isLogged() == false) : ?> <li <?php if($_GET['ref'] == "login") echo 'class="active"'; ?>><a href="index.php?ref=login">Login <?php if($_GET['ref'] == "login") echo '<span class="sr-only">(current)</span>'; ?></a></li> <li <?php if($_GET['ref'] == "register") echo 'class="active"'; ?>><a href="index.php?ref=register">Register <?php if($_GET['ref'] == "register") echo '<span class="sr-only">(current)</span>'; ?></a></li> <?php else : ?> <li <?php if($_GET['ref'] == "chat") echo 'class="active"'; ?>><a href="index.php?ref=chat">Chat</a></li> <li <?php if($_GET['ref'] == "inbox") echo 'class="active"'; ?>><a href="index.php?ref=inbox">Inbox <span class="badge">42</span></a></li> <?php endif; ?> </ul> <ul class="nav navbar-nav navbar-right"> <?php if($this->objAuth->isLogged() == true) : ?> <a class="navbar-brand" href="index.php?ref=profile&id=<?php echo $_COOKIE['usrID']; ?>"> <img class="img-responsive" src="img/thumbs/<?php echo $_COOKIE['usrID'].".jpg"; ?>" /></a> <p class="navbar-text" style="margin-left: 0px;">Hello, User!</p> <li><a href="index.php?ref=logout">Logout</a></li> <?php else : ?> <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>">not logged in..</a></li> <?php endif; ?> </ul> </div>login.phppastebin

                我做错了什么,我找不到问题?非常感谢,谢谢!

0 个答案:

没有答案