我刚刚开始使用PHP进行OOP编程,并且我已经创建了一个cookie类.
通过这样做,我有几个问题没有答案
我的班级是否正确?
如何在我的页面中正确使用它?(我想我想看看访问者之前访问过我的网站的次数并为用户输出结果)
我在登录并使用此代码后已对其进行了测试:
$cookie = new Cookie();
$cookie->store();
print_r($_COOKIE);
Run Code Online (Sandbox Code Playgroud)
(我有一个结果被抛回但我不知道它是否是好结果)Bellow你可以找到我的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;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我希望有人能给我一个好榜样!我在这里先向您的帮助表示感谢!
tft*_*ftd 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();
?>
Run Code Online (Sandbox Code Playgroud)
PS我已经评论了这个$cookie->delete();
目的,以便你可以看到的内容print_r($cookie->get())
.
编辑:
问题:代码在哪里查看cookie是否已设置?
答:
您应该检查$ _COOKIE在php文档中做了什么.
基本上,服务器将标头发送到客户端的浏览器,该浏览器将cookie存储在客户端的计算机上.当客户端初始化与服务器的连接时,它会通过请求传递cookie.
问题:$ cookie-> delete();
答:
没有直接删除cookie的方法.因此,为了做到这一点,您需要创建一个具有相同名称和过期时间的cookie.当您这样做时,cookie将从客户端的浏览器中删除.