2012-07-08 147 views
3

我刚刚开始使用PHP中的OOP编程,并且已经创建了一个cookie类。如何正确使用我的Cookie类

随着这样做,我已经得到解答了几个问题

  • 是我的课是否正确?

  • 如何在页面中正确使用它? (可以认为我希望看到访问者有多少次访问我的网站之前,并将结果输出给用户)

我已经在使用此代码测试它登录电子后:

$cookie = new Cookie(); 
$cookie->store(); 
print_r($_COOKIE); 

(我有一个结果,但我不知道它的好结果) 贝娄,你可以找到我的Cookie类。

<?php 
class Cookie { 
    /* cookie $id */ 
    private $id = false; 

    /* cookie life $time */ 
    private $time = false; 

    /* cookie $domain */ 
    private $domain = false;  

    /* cookie $path */ 
    private $path = false; 

    /* cookie $secure (true is https only) */ 
    private $secure = false; 


    public function __construct ($id, $time = 3600, $path = false, $domain = false, $secure = false) { 
     $this->id = $id; 
     $this->time = $time;     
     $this->path = $path; 
     $this->domain = $domain;  
     $this->secure = $secure; 
    } 

    public function store() { 
     foreach ($this->parameters as $parameter => $validator) { 
      setcookie($this->id . "[" . $parameter . "]", $validator->getValue(), time() + $this->time, $this->path, $this->domain, $this->secure, true);   
     }    
    }  

    public function restore() { 
     if (isset($_COOKIE[$this->id])) { 

      foreach ($_COOKIE[$this->id] as $parameter => $value) { 
       $this->{$parameter} = $value; 
      } 
     } 
    }   

    public function destroy() { 
     $this->time = -1; 
    } 
} 
?> 

我希望有人能给我一个很好的例子!我在这里先向您的帮助表示感谢!

+0

你在哪里可以在'public function store()'和'public function restore()'中得到'$ this-> parameters'?我没有看到它在课堂上的任何地方定义? – tftd 2012-07-08 19:24:41

+0

你是否还计划为这个类的所有cookies提供一个实例,或者你将为每个cookie都有这个类的新实例? – tftd 2012-07-08 19:30:17

+0

我们不确定我是否通过一些网站的帮助制作了这门课。我认为它是一个不需要定义的PHP属性。但现在我一次又一次地看着这个课,我觉得这个班不会在这样的年龄段工作! – Reshad 2012-07-08 19:30:49

回答

9

此代码应该执行操作cookie所需的最频繁的任务。 不要因读取getter和setter方法而感到困惑 - 它们用于访问类中定义的私有变量。 请记住,每个cookie都使用此类,并且您需要为每个要运行的新cookie创建一个新实例。 在课堂以下,我添加了一个如何使用该课程的示例。

<?php 
/** 
* Cookie manager. 
*/ 
class Cookie 
{ 
    /** 
    * Cookie name - the name of the cookie. 
    * @var bool 
    */ 
    private $name = false; 

    /** 
    * Cookie value 
    * @var string 
    */ 
    private $value = ""; 

    /** 
    * Cookie life time 
    * @var DateTime 
    */ 
    private $time; 

    /** 
    * Cookie domain 
    * @var bool 
    */ 
    private $domain = false; 

    /** 
    * Cookie path 
    * @var bool 
    */ 
    private $path = false; 

    /** 
    * Cookie secure 
    * @var bool 
    */ 
    private $secure = false; 

    /** 
    * Constructor 
    */ 
    public function __construct() { } 

    /** 
    * Create or Update cookie. 
    */ 
    public function create() { 
     return setcookie($this->name, $this->getValue(), $this->getTime(), $this->getPath(), $this->getDomain(), $this->getSecure(), true); 
    } 

    /** 
    * Return a cookie 
    * @return mixed 
    */ 
    public function get(){ 
     return $_COOKIE[$this->getName()]; 
    } 

    /** 
    * Delete cookie. 
    * @return bool 
    */ 
    public function delete(){ 
     return setcookie($this->name, '', time() - 3600, $this->getPath(), $this->getDomain(), $this->getSecure(), true); 
    } 


    /** 
    * @param $domain 
    */ 
    public function setDomain($domain) { 
     $this->domain = $domain; 
    } 

    /** 
    * @return bool 
    */ 
    public function getDomain() { 
     return $this->domain; 
    } 

    /** 
    * @param $id 
    */ 
    public function setName($id) { 
     $this->name = $id; 
    } 

    /** 
    * @return bool 
    */ 
    public function getName() { 
     return $this->name; 
    } 

    /** 
    * @param $path 
    */ 
    public function setPath($path) { 
     $this->path = $path; 
    } 

    /** 
    * @return bool 
    */ 
    public function getPath() { 
     return $this->path; 
    } 

    /** 
    * @param $secure 
    */ 
    public function setSecure($secure) { 
     $this->secure = $secure; 
    } 

    /** 
    * @return bool 
    */ 
    public function getSecure() { 
     return $this->secure; 
    } 

    /** 
    * @param $time 
    */ 
    public function setTime($time) { 
     // Create a date 
     $date = new DateTime(); 
     // Modify it (+1hours; +1days; +20years; -2days etc) 
     $date->modify($time); 
     // Store the date in UNIX timestamp. 
     $this->time = $date->getTimestamp(); 
    } 

    /** 
    * @return bool|int 
    */ 
    public function getTime() { 
     return $this->time; 
    } 

    /** 
    * @param string $value 
    */ 
    public function setValue($value) { 
     $this->value = $value; 
    } 

    /** 
    * @return string 
    */ 
    public function getValue() { 
     return $this->value; 
    } 
} 

/** 
* Create a cookie with the name "myCookieName" and value "testing cookie value" 
*/ 
$cookie = new Cookie(); 
// Set cookie name 
$cookie->setName('myCookieName'); 
// Set cookie value 
$cookie->setValue("testing cookie value"); 
// Set cookie expiration time 
$cookie->setTime("+1 hour"); 
// Create the cookie 
$cookie->create(); 
// Get the cookie value. 
print_r($cookie->get()); 
// Delete the cookie. 
//$cookie->delete(); 

?> 

P.S.我已故意评论$cookie->delete();,以便您可以看到print_r($cookie->get())的内容。


编辑:
问:哪里的代码去看看,如果设置cookie?
答案:
您应该检查$ _COOKIE在php documentation中做了什么。
基本上,服务器将头文件发送到客户端的浏览器,该浏览器将cookie存储在客户端的计算机上。当客户端初始化一个到服务器的连接时,它会把请求发送给cookie。

问题:去哪里$ cookie-> delete();
答案:
没有直接的方法来删除cookie。所以为了做到这一点,您需要创建一个具有过去相同名称和过期时间的cookie。当你这样做时,cookie会从客户端的浏览器中删除。

+0

感谢分享!对此,我真的非常感激。 但我在测试后得到了一些小问题。代码在哪里查看Cookie是否已设置? 去哪里$ cookie-> delete();如果我在登录表单中使用它,例如? – Reshad 2012-07-08 19:58:22

+0

不客气。如果您发现它很有用,并且您认为这是您问题的答案,请将我的答案标记为正确答案。干杯:) – tftd 2012-07-08 19:59:40

+0

哈你能帮我解决上面的两个小问题吗? – Reshad 2012-07-08 20:04:06