Home > Backend Development > PHP Tutorial > Summary of session and cookie usage in Symfony2, symfony2cookie_PHP tutorial

Summary of session and cookie usage in Symfony2, symfony2cookie_PHP tutorial

WBOY
Release: 2016-07-12 08:56:27
Original
796 people have browsed it

Symfony2 session and cookie usage summary, symfony2cookie

This article describes the Symfony2 session and cookie usage with examples. Share it with everyone for your reference, the details are as follows:

session operation:

1. Set Session:

public function testSetSession() {
  $session = $this->getRequest()->getSession();
  $session->set($sessionName, $sessionValue );
}

Copy after login

2. Get Session:

public function testGetSession() {
 $session = $this->getRequest()->getSession();
 $username = $session->get($sessionName);
}

Copy after login

3. Clear Session:

public function testClearSession() {
  $session = $this->getRequest()->getSession();//清除session
  $session->clear();
}

Copy after login

Cookie operation:

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()
}

Copy after login

2. Get Cookie:

public function testGetCookie() {
 $request = $this->getRequest();
 return $request->cookies->all();
}

Copy after login

3. Clear Cookie:

public function testClearCookie() {
 $response = new Response();
 $response->headers->setCookie(new Cookie($name, $value, -1));
 $response->send();
}

Copy after login

4. twig template calls cookie:

{{ app.request.cookies.get('cookie_name') }}
Copy after login

I hope this article will be helpful to everyone’s PHP program design based on the Symfony framework.

Articles you may be interested in:

  • Symfony2 joint query implementation method
  • Detailed explanation of creating page instances in Symfony2
  • Analysis of date usage in twig of symfony2.4
  • A summary of the method of obtaining data from the database in Symfony2
  • Detailed explanation of form usage of Symfony2 framework study notes
  • Plug-in format analysis of Symfony2 study notes
  • Symfony2 learning Detailed explanation of the system routing of notes
  • Detailed explanation of the controller usage of Symfony2 study notes
  • Detailed explanation of the template usage of Symfony2 study notes
  • Detailed explanation of the installation of third-party Bundles instances of Symfony2
  • Symfony2 function usage example analysis

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1111905.htmlTechArticleSymfony2 session and cookie usage summary, symfony2cookie This article describes the Symfony2 session and cookie usage with examples. Share it with everyone for your reference, the details are as follows: session operation:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template