本文实例讲述了Symfony2之session与cookie用法。分享给大家供大家参考,具体如下:
session操作:
1. Set Session:
?
1
2
3
4
|
public function testSetSession() {
$session = $this ->getRequest()->getSession();
$session ->set( $sessionName , $sessionValue );
}
|
2. Get Session:
?
1
2
3
4
|
public function testGetSession() {
$session = $this ->getRequest()->getSession();
$username = $session ->get( $sessionName );
}
|
3. Clear Session:
?
1
2
3
4
|
public function testClearSession() {
$session = $this ->getRequest()->getSession(); //清除session
$session ->clear();
}
|
cookie操作:
1. Set Cookie
?
1
2
3
4
5
6
7
|
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:
?
1
2
3
4
|
public function testGetCookie() {
$request = $this ->getRequest();
return $request ->cookies->all();
}
|
3. Clear Cookie:
?
1
2
3
4
5
|
public function testClearCookie() {
$response = new Response();
$response ->headers->setCookie( new Cookie( $name , $value , -1));
$response ->send();
}
|
4. twig模板调用cookie:
?
1
|
{{ app.request.cookies.get( 'cookie_name' ) }}
|
希望本文所述对大家基于Symfony框架的PHP程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 11
-
2025-06-04 61
-
2025-06-04 62
-
2025-05-25 94
-
2025-05-29 47
热门评论