Organizational Research By

Surprising Reserch Topic

zf2 read http cookie


zf2 read http cookie  using -'php,zend-framework,cookies,http-headers,httpresponse'

I have a file (caller.php) which create a cookie in HTTP response and then redirect to a controller in a ZF2 application (LoginController).

caller.php

setcookie("_ga", "GA1.2.1622977711.1433494392");
setcookie("_gat", "1");

header("Location: http://gnsys.local/publico/login");


LoginController

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Http\Response;

class LoginController extends BasePublicController{

public function indexAction(){

    $response = $this->getResponse();

    foreach ($response->getHeaders() as $header) {
        echo "Campo: " . $header->getFieldName() . ' with value ' . $header->getFieldValue() . "<br />";
    }


    return new ViewModel();
}
}


When LoginController is called, I haven't got any header from the HTTP response :((

What am I doing wrong? Why I can't read any value from my http response headers? How can I read all the headers from HTTP Response?

If I do what I want but just only using PHP, LoginController is changed by a file called login.php whith this code:

foreach (getallheaders() as $name => $value) {
    echo "$name: $value</br>";
}


And this code works fine and give me what I want!!! How can I get the same in Zend Framework 2?

Please, help me!!!

Thanks in advance,

José Carlos
    

asked Oct 23, 2015 by kotmus2002
0 votes
6 views



Related Hot Questions

3 Answers

0 votes

You should extract headers from request, but not response:

foreach ($this->getRequest()->getHeaders() as $header) {
   echo 'Campo: ' . $header->getFieldName() . ' with value ' . $header->getFieldValue() . '
'; }

To get cookies only use next:

foreach ($this->getRequest()->getCookie() as $name => $value) {
    echo 'Campo: ' . $name . ' with value ' . $value . '
'; }

Set cookie from action:

public function callerAction()
{
    $cookie = new \Zend\Http\Header\SetCookie();
    $cookie->setName('foo')
        ->setValue('bar')
        ->setDomain('gnsys.local')
        ->setPath('/')
        ->setHttponly(true);

    /** @var \Zend\Http\Response $response */
    $response = $this->getResponse();
    $response->getHeaders()->addHeader($cookie);

    return $this->redirect()->toUrl('/publico/login');
}
answered Oct 23, 2015 by akasati02
0 votes

Thank you to Stafox!!! I can read HTTP Headers but ... I can't read Cookie!!! I read the next headers:

Host: gnsys.local
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8
Accept-Language: es-ES, es;q=0.8, en-US;q=0.5, en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive

But ... where is my cookie? Why I can't read it?

answered Oct 23, 2015 by nimisha.jagtap
0 votes

I have made changes in my code and it still doesn't work :(

The file named caller.php is on http://localhost/caller.php and his code is ...

caller.php

setcookie("_ga", "GA1.2.1622977711.1433494392", 0, "/", "http://gnsys.local");
setcookie("_gat", "1", 0, "/", "http://gnsys.local");
header("Location: http://gnsys.local/publico/login");

And the controller (http://gnsys.local/publico/local) where I'm going to read the HTTP Headers has this code.

LoginController

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Http\Request;

class LoginController extends BasePublicController{

public function indexAction(){

    foreach ($this->getRequest()->getCookie() as $name => $value) {
        echo 'Campo: ' . $name . ' with value ' . $value . '
'; } //return new ViewModel(); } }

And now the result that I have is ...

Campo: zdt-hidden with value 0

I don't understand anything :( What am I doing wrong right now?

answered Oct 23, 2015 by deepak07.s

...