Symfony2之session與cookie用法小結(jié)
更新時間:2016年03月18日 10:14:20 作者:Terry
這篇文章主要介紹了Symfony2之session與cookie用法,結(jié)合實例形式總結(jié)分析了Symfony框架針對session與cookie的設置、獲取及刪除等操作的實現(xiàn)方法,需要的朋友可以參考下
本文實例講述了Symfony2之session與cookie用法。分享給大家供大家參考,具體如下:
session操作:
1. Set Session:
public function testSetSession() {
$session = $this->getRequest()->getSession();
$session->set($sessionName, $sessionValue );
}
2. Get Session:
public function testGetSession() {
$session = $this->getRequest()->getSession();
$username = $session->get($sessionName);
}
3. Clear Session:
public function testClearSession() {
$session = $this->getRequest()->getSession();//清除session
$session->clear();
}
cookie操作:
1. Set Cookie
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
public function testSetCookie($name, $value, $expire=0){
$response = new Response();
$response->headers->setCookie(new Cookie($name, $value, time() + $expire));
$response->send(); // 包括 sendHeaders()、sendContent()
}
2. Get Cookie:
public function testGetCookie() {
$request = $this->getRequest();
return $request->cookies->all();
}
3. Clear Cookie:
public function testClearCookie() {
$response = new Response();
$response->headers->setCookie(new Cookie($name, $value, -1));
$response->send();
}
4. twig模板調(diào)用cookie:
{{ app.request.cookies.get('cookie_name') }}
希望本文所述對大家基于Symfony框架的PHP程序設計有所幫助。
您可能感興趣的文章:
- Symfony2學習筆記之系統(tǒng)路由詳解
- 高性能PHP框架Symfony2經(jīng)典入門教程
- Symfony2使用Doctrine進行數(shù)據(jù)庫查詢方法實例總結(jié)
- Symfony2實現(xiàn)從數(shù)據(jù)庫獲取數(shù)據(jù)的方法小結(jié)
- Symfony2創(chuàng)建頁面實例詳解
- Symfony2安裝第三方Bundles實例詳解
- Symfony2聯(lián)合查詢實現(xiàn)方法
- Symfony2實現(xiàn)在controller中獲取url的方法
- Symfony2在Nginx下的配置方法圖文教程
- Symfony2 session用法實例分析
- Symfony2創(chuàng)建基于域名的路由相關示例
相關文章
Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié)
這篇文章主要介紹了Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
全世界最小的php網(wǎng)頁木馬一枚 附PHP木馬的防范方法
php網(wǎng)頁木馬代碼,大家可以看下自己的網(wǎng)站里面是不是有這樣的代碼,注意網(wǎng)站安全用mcafee限制w3wp.exe生成php或者asp文件。并在php.ini中設置一下。2009-10-10

