怎么在 Phalcon 框架里管理使用cookie

怎么在 Phalcon 框架里管理使用cookie,第1张

Phalcon\Http\Response\Cookies(不是Phalcon\Http\Cookie)是一个为响应设置cookie的容器,类似于PHP中的setcookie()函数。

这个容器中设置的脊派每个cookie都是一个Phalcon\Http\Cookie类实例,容器的send()方法实际是添加响应头Set-Cookie指令。

在Phalcon中,最终会调用:

Phalcon\Http\Response->sendHeaders()

Phalcon\Http\Response->sendCookies()

这里的Phalcon\Http\Response->sendCookies()内部实际调用Phalcon\Http\Response\Cookies的send()方法。

如下例子:

//在控制器方法中添加如下代码:

$this->cookies->useEncryption(false)

$this->cookies->set("test-cookie","ffffffffffffffff")

$this->cookies->send()

return

//响应头

Connection Keep-Alive

Content-Length 10

Content-Typetext/html

DateMon, 08 Sep 2014 04:20:17 GMT

Keep-Alive timeout=5, max=100

Server Apache/2.2.27 (Unix) PHP/5.5.15

Set-Cookie test-cookie=ffffffffffffffffpath=/httponly test-cookie=ffffffffffffffffpath=/httponly

代码中设置了一个cookie,然后调用容器的send()方法,在客户端查看响应头,发现Set-Cookie指令中test-cookie设置了两次,这个说明Phalcon在响应时也调用了一次send()方法,这个就可以证明Phalcon在响应时调用的sendCookies()方法内部就是调用cookie容器的send()方法。

Phalcon\Http\Response有三个和cookie有关的方法:

Phalcon\Http\Response::getCookies()

Phalcon\Http\Response::setCookies($cookies)

Phalcon\Http\Response::sendCookies()

分别获取和设置Response的Cookies容器(\Phalcon\Http\Response\CookiesInterface 实例喊穗),sendCookies()是调用容器的send()方法发送cookie。

还有一个问题是,如何获取来自请求的cookie?你可以发现,在Phalcon\Http\Request中并不存在类似getCookies()方法,郑野卜难道要使用$_COOKIE全局数组?Phalcon文档对于这个并没有提及。使用如下例子:

//使用的请求头信息

Accept text/html,application/xhtml+xml,application/xmlq=0.9,*/*q=0.8

Accept-Encoding gzip, deflate

Accept-Language zh-cn,zhq=0.8,en-usq=0.5,enq=0.3

Cache-Control max-age=0

Connection keep-alive

Cookie test-cookie=only+for+test+cookie.

Host192.168.1.168

User-Agent Mozilla/5.0 (Windows NT 6.1WOW64rv:32.0) Gecko/20100101 Firefox/32.0

//控制器代码

$this->cookies->set("hello-cookie","hello cccccc kie.")

if($this->cookies->has("test-cookie")){

echo $this->cookies->get("test-cookie")->getValue()

}

print_r($this->cookies)

$this->view->disable()

//输出

only for test cookie.

Phalcon\Http\Response\Cookies Object

(

[_dependencyInjector:protected] =>Phalcon\DI\FactoryDefault Object

[_registered:protected] =>

[_useEncryption:protected] =>

[_cookies:protected] =>Array

(

[hello-cookie] =>Phalcon\Http\Cookie Object

(

[_readed:protected] =>1

[_restored:protected] =>

[_useEncryption:protected] =>

[_dependencyInjector:protected] =>Phalcon\DI\FactoryDefault Object

[_filter:protected] =>

[_name:protected] =>hello-cookie

[_value:protected] =>hello cccccc kie.

[_expire:protected] =>0

[_path:protected] =>/

[_domain:protected] =>

[_secure:protected] =>

[_httpOnly:protected] =>1

)

)

)

请求中发送了cookie(test-cookie=only+for+test+cookie.)

$this->cookies->get("test-cookie")获取到了这个来自请求的cookie,但是从Phalcon\Http\Response\Cookies输出来看,这个cookie并不在它的_cookies数组中,观察到代码中设置了一个叫hello-cookie的cookie,而它在_cookies数组中,所以应该是只有设置了才进入_cookies数组,$this->cookies->get("test-cookie")先判断在不在_cookies数组中,否则再判断是不是来自请求的cookie。

注意,$this->cookies->get("test-cookie")返回的是一个来自请求的Phalcon\Http\Cookie的类实例,所以可以调用相关方法。

Phalcon对管理来自请求的cookie比较隐晦,它只能通过Phalcon\Http\Response\Cookies容器相关方法调用,从名字来看,不应该是它的一部分。

HTTP 请求环境(Request Environment)

每个HTTP请求(通常来源于浏览器)会包含请求的附加信息,比如头部数据,文件,变量等。一个基于Web的应用需要解析这些信息以提供正确的响应返回到请求者。Phalcon\Http\Request封装了这些请求的信息,允许你以一个面向对象的方式访问它。

<?php

// Getting a request instance

$request = new \Phalcon\Http\Request()

// Check whether the request was made with method POST

if ($request->isPost() == true) {

// Check whether the request was made with Ajax

if ($request->isAjax() == true) {

echo "Request was made using POST and AJAX"

}

}

获取值(Getting Values)

PHP更加请求的类型自动地填充$_GET和$_POST超全局数组。这些数组保存了来自提交表单的值或者通过URL传递的参数值。在这些数组中的变量没有被清理兵器包含非法字符,甚至是可能导致SQL注入或跨站脚本(XSS)攻击的恶意代码。

Phalcon\Http\Request allows you to access the values stored in the $_REQUEST, $_GET and $_POST arrays and sanitize or filter them with the ‘filter’ service, (by default Phalcon\Filter). The following examples offer the same behavior:

Phalcon\Http\Request允许你访问这些存储在$_REQUEST, $_GET 和 $_POST数组中的值并且通过使用'filter'服务清理或过滤它们,(默认是Phalcon\Filter)。

以下例子提供了相同的行为:

<?php

// Manually applying the filter

$filter = new Phalcon\Filter()

$email = $filter->sanitize($_POST["user_email"], "email")

// Manually applying the filter to the value

$filter = new Phalcon\Filter()

$email = $filter->sanitize($request->getPost("user_email"), "email")

// Automatically applying the filter

$email = $request->getPost("user_email", "email")

// Setting a default value if the param is null

$email = $request->getPost("user_email", "email", "[email protected]")

// Setting a default value if the param is null without filtering

$email = $request->getPost("user_email", null, "[email protected]")

(这写个用法基本要牢记了,要么直接使用filter清理,要么间接使用)

控制器中访问请求(Accessing the Request from Controllers)

最通用的方法请求环境的地方是在控制器的动作中。为了从访问控制器访问Phalcon\Http\Request对象你将需要使用$this->request控制器的公共属性:(注意,DI资源通过魔术方法映射到公共属性,并非真的是它的公共属性)

<?php

use Phalcon\Mvc\Controller

class PostsController extends Controller {

public function indexAction() {

}

public function saveAction() {

// Check if request has made with POST

if ($this->request->isPost() == true) {

// Access POST data

$customerName = $this->request->getPost("name")

$customerBorn = $this->request->getPost("born")

}

}

}

文件上传(Uploading Files)

另一个常见的任务是文件上传。

Phalcon\Http\Request提供了一个面向对象的方法去完成这个任务:

<?php

use Phalcon\Mvc\Controller

class PostsController extends Controller {

public function uploadAction() {

// Check if the user has uploaded files

if ($this->request->hasFiles() == true) {

// Print the real file names and sizes

foreach ($this->request->getUploadedFiles() as $file) {

//Print file details

echo $file->getName(), " ", $file->getSize(), "\n"

//Move the file into the application

$file->moveTo('files/' . $file->getName())

}

}

}

}

通过Phalcon\Http\Request::getUploadedFiles()返回的每个对象是一个Phalcon\Http\Request\File类的实例。

使用$_FILES超全局数组提供类似行为。Phalcon\Http\Request\File仅封装了请求的跟每个上传文件相关的信息。

使用头信息(Working with Headers)

跟上面提到的一样,请求头包含了允许我们去发送合适的响应到用户的有用的信息。以下例子展示这些信息的用法:

<?php

// get the Http-X-Requested-With header

$requestedWith = $response->getHeader("HTTP_X_REQUESTED_WITH")

if ($requestedWith == "XMLHttpRequest") {

echo "The request was made with Ajax"

}

// Same as above

if ($request->isAjax()) {

echo "The request was made with Ajax"

}

// Check the request layer

if ($request->isSecureRequest() == true) {

echo "The request was made using a secure layer"

}

// Get the servers's ip address. ie. 192.168.0.100

$ipAddress = $request->getServerAddress()

// Get the client's ip address ie. 201.245.53.51

$ipAddress = $request->getClientAddress()

// Get the User Agent (HTTP_USER_AGENT)

$userAgent = $request->getUserAgent()

// Get the best acceptable content by the browser. ie text/xml

$contentType = $request->getAcceptableContent()

// Get the best charset accepted by the browser. ie. utf-8

$charset = $request->getBestCharset()

// Get the best language accepted configured in the browser. ie. en-us

$language = $request->getBestLanguage()

Phalcon是一个使用c扩展写的PHP框架, 使用c扩展意味着在运行速度上枝贺要优于直接使用php写的框架

因为 Phalcon 是用 c扩展 写的, 所以并不像其他的PHP框架, 比如 laravel , 从git上clone到本地就可以直接运行。而是先要安装phalcon的扩展。

在Mac下安装非常方便, 直接使用homebrew就可以安装了

这里我使用了 php7 , 在 php7 性能较5.x有成倍的提升之后, 使用 php7 再合适不过.

如果你没有安装 php7 , 你也可以使用更低的版本

按照下面的命令显示, 就说明已经安装好 phalcon 了

虽说有了州备扩展, 我们还是需要一些php的文件才能真正运行 Phalcon

这里我推荐大家安装 Phalcon Tools

可以使用 composer 在全局下安装(我已猛迹派经在全局环境下安装好composer了)

显示如下内容, 安装成功, 并且可以看到他可以使用的命令

使用Phalcon Tools, 新建一个Phalcon项目就非常简单了

生成的目录如下:

在浏览器中键入 localhost:8008 就可以看到欢迎页面了


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8176078.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存