4

PHPでのOOPプログラミングを始めたばかりで、Cookieクラスを作成しました。

そうすることで、私はいくつかの質問に答えられませんでした

  • 私のクラスは正しいですか?

  • ページで適切に使用するにはどうすればよいですか?(訪問者が以前に私のWebサイトにアクセスした回数を確認し、ユーザーに結果を出力したいと考えてみましょう)

ログインして次のコードを使用した後、すでにテストしました。

$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;
    }   
}
?>

誰かが私に良い例を教えてくれることを願っています!事前に助けてくれてありがとう!

4

1 に答える 1

11

このコードは、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();

?>

$cookie->delete();PSの内容を確認できるように、わざと にコメントしましたprint_r($cookie->get())


編集:
質問: Cookie が設定されているかどうかを確認するコードはどこに行くのですか?
回答: PHPドキュメント
で $_COOKIE が何をするかを確認する必要があります。 基本的に、サーバーはクライアントのコンピューターに Cookie を保存するクライアントのブラウザーにヘッダーを送信します。クライアントがサーバーへの接続を初期化するとき、リクエストとともに Cookie を渡します。 質問: $cookie->delete(); はどこに行くのですか? 回答: Cookie を直接削除する方法はありません。そのためには、過去と同じ名前と有効期限を持つ Cookie を作成する必要があります。これを行うと、Cookie はクライアントのブラウザーから削除されます。




于 2012-07-08T19:48:09.100 に答える