我刚开始使用 PHP 进行 OOP 编程,并且制作了一个 cookie 类。
这样做我有几个问题没有得到解答
我的类(class)正确吗?
如何在我的页面中正确使用它? (假设我想看看访问者之前访问过我的网站的次数并为用户输出结果)
我已经在登录并使用此代码后对其进行了测试:
$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;
}
}
?>
希望有人能给我一个很好的例子!提前感谢您的帮助!
最佳答案
此代码应该执行操作 cookie 所需的最常见任务。 不要因阅读 getter 和 setter 方法而感到困惑——它们用于访问类中定义的私有(private)变量。 请记住,此类是针对每个 cookie 使用的,您需要为每个要操作的新 cookie 创建一个新实例。 在类(class)下方,我添加了一个如何使用该类(class)的示例。
<?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();
,这样你就可以看到print_r($cookie->get())
的内容了。
编辑:
问题:代码去哪里查看是否设置了cookie?
答案:
您应该检查 $_COOKIE 在 php documentation 中做了什么.
基本上,服务器将 header 发送到客户端浏览器,客户端浏览器将 cookie 存储在客户端计算机上。当客户端初始化与服务器的连接时,它会随请求传递 cookie。
问题:$cookie->delete();
去哪儿了
答:
没有直接的方法来删除 cookie。因此,为了做到这一点,您需要创建一个具有相同名称和过去过期时间的 cookie。当您这样做时,cookie 将从客户端的浏览器中删除。
关于php - 如何正确使用我的 Cookie 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11385907/