Sending cookie in cURL request

Please take a look at the code below:

$method = $request[ $i ][ 'Method' ];
$url = $request[ $i ][ 'URI' ];

$curl = curl_init( 'http://' . $_SERVER[ 'SERVER_NAME' ] . $url );
curl_setopt( $curl, CURLOPT_HEADER, 2 );
//When I uncomment this line, I am not getting response, request timed out.
//[B]curl_setopt( $curl, CURLOPT_COOKIE, session_name() . '=' .
$_COOKIE[session_name()] . '; domain=192.168.0.1; path=/' );[/B]
curl_setopt( $curl, CURLOPT_HTTPHEADER, $request[ $i ][ 'Headers' ] );

if ( $method == 'POST' )
{
    curl_setopt( $curl, CURLOPT_POST, 1 );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $request[ $i ][ 'Content' ] );
}
else if ( $method != 'GET' )
{
    curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $method );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $request[ $i ][ 'Content' ] );
}

curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$strResponse = curl_exec( $curl );

When I uncomment the line

curl_setopt( $curl, CURLOPT_COOKIE, session_name() . '=' .
$_COOKIE[session_name()] . '; domain=192.168.0.1; path=/' );

I am not getting response, Timed out.

When I observed normal Ajax requests, a header is sending as:

Cookie PHPSESSID=goio5r15cqost5hvu64u7p28k0

So, I tried adding

header("Cookie: " . session_name() . '=' . $_COOKIE[session_name()]);

Again, timed out.

don’t know but might this help?:


# Location of your cookie file. (Must be fully resolved local address)
define("COOKIE_FILE", "c:\\cookie.txt");

...

	curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);   // Cookie management.
	curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);

...


taken from the “LIB_http.php” file available from here: http://www.schrenk.com/nostarch/webbots/DSP_download.php

My doubt is, how can I get the cookie file from client?

How can I create cookie file on the server to send in cURL requests?

I tried this also. Created a file with content

PHPSESSID=q42abapevipjtttnfom10va5a2; path=/; domain=localhost

and I sent it in the cURL request. But the same result, timed out.

When I try:


$strCookie = session_name() . '=' . $_COOKIE[ session_name() ] . '; path=/';
curl_setopt( $curl, CURLOPT_COOKIE, 'PHPSESSID=7b8kv5u75rg8io3mm3lcenljd1; path:/' );//Current session ID

I am able to get data.

But when I use:


$strCookie = session_name() . '=' . $_COOKIE[ session_name() ] . '; path=/';
curl_setopt( $curl, CURLOPT_COOKIE, $strCookie );

it is not working. In both the cases, I am sending a string. But why it is not working?

Pretty sure you should be using…

$strCookie = 'PHPSESSID=' . session_name() . '; path=/';
curl_setopt( $curl, CURLOPT_COOKIE, $strCookie ); 

SilverB.

No.

I am using:

$strCookie = session_name() . '=' . $_COOKIE[ session_name() ] . '; path=/';

curl_setopt( $curl, CURLOPT_COOKIE, $strCookie );

Value in $strCookie is:

PHPSESSID=jpo09e37uikmqqth3rkd38v3s2; path=/

Echo’ing $strCoookie will output ‘PHPSESSID=CURRENT_SESS_ID; path=/’.

Isn’t this what you want passed to cURL?

SilverB.

Exactly.

So why doesn’t my code work? Are you ensuring you’re starting the session too with session_start() ?

I don’t understand while you’re using the $_COOKIE super global.

SilverB.

If I have not started the session, how can I get the $strCookie value???

Before you can access any session vars you needs to have a valid session, a session can be started with session_start();


session_start();
$strCookie = 'PHPSESSID=' . session_id() . '; path=/';
curl_setopt( $curl, CURLOPT_COOKIE, $strCookie ); 

Sorry, but if this doesn’t help, I’m going to need you to explain what exactly you are trying to do here…

It’s just not sinking in.

SilverB.

Have a look at:

http://www.sitepoint.com/forums/showthread.php?t=571935

Well, if that works what about…

<?php
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
curl_setopt( $curl, CURLOPT_COOKIE, $strCookie );
?>

SilverB.

That is also not working.

Only a single script may access session data at a time. php puts an exclusive lock on the session file. Since your curl script is waiting on the script it’s calling via url to access session data, it waits forever because the script called via the url is waiting for the curl script to close the session, so that it may open it. This obviously never happens.

call session_write_close() before sending the request.

Hey!

That’s working fine…

Thank you very much.