2012-07-08 168 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