Symfony2 session and Cookie Usage summary, symfony2cookie_php Tutorial

Source: Internet
Author: User
Tags set cookie setcookie

Symfony2 session and Cookie usage summary, Symfony2cookie


This article describes the session and cookie usage of Symfony2. Share to everyone for your reference, 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 (); including 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 Invoke Cookie:

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

It is hoped that this article is helpful to the PHP program design based on Symfony framework.

Articles you may be interested in:

    • Implementation method of Symfony2 Federated query
    • Symfony2 Creating a Page instance
    • Analysis of date usage in twig of symfony2.4
    • Symfony2 a summary of how to achieve data acquisition from a database
    • Symfony2 Framework Learning Notes Form usage
    • Analysis of plugin format of Symfony2 learning notes
    • Symfony2 Study Notes System routing
    • Symfony2 study notes of the controller usage detailed
    • Symfony2 Study Notes Template usage detailed
    • Symfony2 installing a third-party bundles instance
    • An example analysis of Symfony2 function usage

http://www.bkjia.com/PHPjc/1111905.html www.bkjia.com true http://www.bkjia.com/PHPjc/1111905.html techarticle Symfony2 session and Cookie Usage Summary, Symfony2cookie This example describes the Symfony2 session and cookie usage. Share to everyone for reference, as follows: Session operation: ...

  • 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.