Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl not passing phpsessid

Tags:

php

curl

Try as I might, I am unable to get curl to pass the PHPSESSID cookie. I have a similar setup to what several others have described, but I haven't been able to get any of the proposed solutions working.

I have a page that sends a get request to pageA.php. pageA.php needs some information from pageB.php, which is on another server, so I am using curl. pageB maintains the session state that I need to get to and from my page.

I am able to pass other cookies from pageA to pageB, just not the PHPSESSID cookie.

The following works (a cookie arrives on pageB):
$options[CURLOPT_COOKIE] = "myPHPSESSID=" . $sessionId;

The following does not:

$options[CURLOPT_COOKIE] =  "PHPSESSID=" . $sessionId;

(I build an $options array and then pass it to curl_set_opt_array)

In fact, the latter causes some sort of error that I am unable to discern, as my call to curl_exec never returns (and pageB is never reached).

I've tried setting the header instead of using CURLOPT_COOKIE, but also with no success:

$options[CURLOPT_HTTPHEADER][] = "Cookie: myPHPSESSID=" . $sessionId;

the above works just fine but

$options[CURLOPT_HTTPHEADER][] = "Cookie: PHPSESSID=" . $sessionId;

does not.

PHP evidently does not want me to manually set PHPSESSID. I'm not sure if it's relevant, but at no time on pageA do I call start_session() (though I tried doing so and had the same results).

I realize I could (probably) just pass the session id as a differently-named cookie and have pageB call set_session_id() or something similar. But I really want to know why what I'm doing is not working, as I'd rather fix what I'm doing wrong than create a workaround. I can provide some dumps of the various headers that are pinging around if that's helpful, but I figure this question is long enough for now.

Thanks...

like image 793
susie derkins Avatar asked Mar 26 '13 00:03

susie derkins


1 Answers

Call session_write_close(); before your curl_exec.

This saved my life: http://kmak.1funkybit.com/?p=126 (read for explanation).

Example:

<?php
session_start();
var_dump($_SESSION); //See what's in session
echo "<br>";



$c = curl_init('http://192.168.100.204/logintest/index.php?r=AccessTest/CheckAcess');
$parametros_post = 'action=verChau';
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $parametros_post);
curl_setopt($c, CURLOPT_VERBOSE, TRUE);
curl_setopt($c, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
session_write_close();
$page = curl_exec ($c);
echo "<br>";
echo "<br>";
echo $page;
curl_close ($c);


?>
like image 130
JorgeeFG Avatar answered Sep 22 '22 20:09

JorgeeFG