Php 如何正确使用Cookie类
我刚开始在PHP中进行OOP编程,并创建了一个cookie类Php 如何正确使用Cookie类,php,cookies,Php,Cookies,我刚开始在PHP中进行OOP编程,并创建了一个cookie类 <?php class Cookie { /* cookie $id */ private $id = false; /* cookie life $time */ private $time = false; /* cookie $domain */ private $domain = false; /* cookie $path */ priva
<?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 = new Cookie();
$cookie->store();
print_r($_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方法时不要感到困惑——它们用于访问类中定义的私有变量。 请记住,这个类是按每个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()代码>以便您可以查看打印的内容($cookie->get())
编辑:
问题:代码去哪里查看cookie是否已设置?
回答:
您应该检查$\u COOKIE在中的作用
基本上,服务器将头发送到客户端的浏览器,该浏览器将cookie存储在客户端的计算机上。当客户端初始化与服务器的连接时,它会将cookies与请求一起传递。
问题:$cookie->delete()在哪里
回答:
没有直接的方法来删除cookies。因此,为了做到这一点,您需要创建一个具有过去相同名称和过期时间的cookie。执行此操作时,cookie将从客户端浏览器中删除。在
公共函数存储()和公共函数还原()中从何处获取$this->parameters
?我没有看到它在类中的任何地方定义?您是否计划为所有cookie创建一个此类实例,或者为每个cookie创建一个新的此类实例?我不确定我是否在几个网站的帮助下创建了此类。我认为这是一个不需要定义的PHP属性。但是现在我一次又一次地看这个,我认为这个班在这么长的时间里是不会工作的!首先,我希望每个cookie都有一个这个类的新实例。但我不知道如何创建一个好的实例,所以我想也许可以从这个类的一个实例开始。谢谢分享!我真的很感激。但我在测试后有一些小问题。代码在哪里查看cookie是否已设置?$cookie->delete()在哪里;如果我在登录表单中使用它?不客气。如果您觉得它有用,并且您认为这是您问题的答案,请将我的答案标记为正确答案。干杯:)哈,你能帮我回答上面的两个小问题吗?代码去哪里看cookie是否设置了?(例如一个管理页面左右)哪里有$cookie->delete();例如,如果我在登录表单中使用它,我得到了一个很好的解决方案!非常感谢!至于删除我不知道;我还没有找到答案,但我希望我能尽快找到解决办法!