We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Cookie-> set not working when calling a route using file_get_contents?

Hi,

Setting cookie functionality is not working when I'm calling a route fro PHP file_get_contents method? and It's working with jQuery $.ajax. What is the mistake i've done? Please help me to solve this.

Route:

$api->addGet('/setmycookie/{cookievalue}', array('controller' => 'myController', 'action' => 'index'));

Controller:

public function indexAction($cookievalue) { session_start();
$this->cookies->set('myCookie', $cookievalue, time() + 15 * 86400); $this->cookies->send(); $this->response->setResponse(array("status" => "success")); return $this->response; }

Front End Trigger:

$data = file_get_contents("https://domain.com/api/setmycookie/mycookievalue");

edited Jul '16

So, you would expect that file_get_contents will accept data from Set-Cookie HTTP header? Where it would store it? That is not client but one PHP process initiating remote call to another HTTP server / PHP. That's why your AJAX works, since that's client side (i.e. cookie is stored in a browser of a client).