Session and cookie usage summary of Symfony2, symfony2cookie_PHP tutorial

Source: Internet
Author: User
Tags set cookie
Symfony2 session and cookie usage summary, symfony2cookie. Symfony2 session and cookie usage summary. symfony2cookie this article describes the session and cookie usage of Symfony2. For your reference, the procedure is as follows: session operation: session and cookie usage summary of Symfony2, symfony2cookie

This article describes the session and cookie usage of Symfony2. We will share this with you for your reference. The details are as follows:

Session operation:

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 (); // clear session $ session-> clear ();}

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 (); // includes 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. The twig template calls the cookie:

{{ app.request.cookies.get('cookie_name') }}

I hope this article will help you design PHP programs based on the Symfony framework.

Articles you may be interested in:
  • Symfony2 joint query implementation method
  • Symfony2 create page instance details
  • Analysis on date usage in twig of symfony2.4
  • Summary of how Symfony2 can obtain data from the database
  • Symfony2 framework learning Notes
  • Symfony2 study notes plugin format analysis
  • System route details for Symfony2 study notes
  • A detailed explanation of the controller usage of Symfony2 learning Notes
  • Template usage of Symfony2 learning Notes
  • How to install a third-party Bundles instance in Symfony2
  • Symfony2 function usage example analysis

Listen This article describes the session and cookie usage of Symfony2. For your reference, the procedure is as follows :...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.