Symfony2 session and Cookie Usage Summary _php instance

Source: Internet
Author: User
Tags php programming set cookie setcookie

This example describes the session of Symfony2 and the use of cookies. Share to everyone for your reference, specific as follows:

Session Action:

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 action:

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. Twig Template Invocation Cookie:

{{App.request.cookies.get (' Cookie_name ')}}

I hope this article will help you with your PHP programming based on the Symfony framework.

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.