Symfony2之session與cookie用法小結(jié)
本文實例講述了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程序設(shè)計有所幫助。
- Symfony2學(xué)習(xí)筆記之系統(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)建基于域名的路由相關(guān)示例
相關(guān)文章
destoon實現(xiàn)不同會員組公司名稱顯示不同的顏色的方法
這篇文章主要介紹了destoon實現(xiàn)不同會員組公司名稱顯示不同的顏色的方法,是非常實用的方法,代碼簡潔思路簡單,需要的朋友可以參考下2014-08-08ThinkPHP提交表單時默認自動轉(zhuǎn)義的解決方法
這篇文章主要介紹了ThinkPHP提交表單時默認自動轉(zhuǎn)義的解決方法,可解決針對單引號和雙引號的自動轉(zhuǎn)移問題,提供了兩種解決方法供大家對比選擇,具有一定的實用價值,需要的朋友可以參考下2014-11-11laravel excel 上傳文件保存到本地服務(wù)器功能
今天小編就為大家分享一篇laravel excel 上傳文件保存到本地服務(wù)器功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11PHP隱藏手機號碼、銀行卡號、真實姓名部分內(nèi)容的方法
這篇文章主要介紹了PHP隱藏手機號碼、銀行卡號、真實姓名部分內(nèi)容的方法的實現(xiàn)代碼,有時候我們需要將部分內(nèi)容隱藏那么就可能需要下面的代碼了,需要的朋友可以參考下2023-11-11