Jump to content

Alternative To cURL That Allows Access To $_SESSION Values


johnsmith153

Recommended Posts

If you use cURL, then you can't access session variables - it doesn't work - something to do with starting a new http request/session.

 

Test it.

 

Is there an alternative, so I can create a http request from within a PHP page (ie like cURL) - but allows me to access the session variables already stored?

If you use cURL, then you can't access session variables - it doesn't work - something to do with starting a new http request/session.

 

Test it.

 

Is there an alternative, so I can create a http request from within a PHP page (ie like cURL) - but allows me to access the session variables already stored?

 

 

Set up cURL to use cookies and your sessions will persist.

How would I do this?

 

If I had:

 

$post_fields="var1=1&var2=5&var3=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.site.com/page.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response_string = curl_exec($ch);
curl_close($ch);

 

What would I change?

Is there anybody who can help with this?

 

How would I change this to maintain the $_SESSION values when using cURL?

 

Xtopolis, you can't know as you wanted to see some code when I originally asked why cURL did not maintain the session values. Luckily someone else educated us.

 

$post_fields="var1=1&var2=5&var3=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.site.com/page.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response_string = curl_exec($ch);
curl_close($ch);

That's a very big assumption about me.

 

You STILL have not edited your code to account for COOKIEs that are usually used for Sessions.

Your answers can STILL be found on the comments I posted, to get you started.

 

Doesn't mean it will work either, but corbin told you what to try, so why not?

I am afraid you are all wrong.

 

Darkwater. Thanks for coming in on this. You have given the nearest answer.

 

I eventually got the answer from http://www.sitepoint.com/forums/showthread.php?t=572402 who told me that you need to first close the session on the main page (session_write_close();)

 

Not one single link anybody posted mentioned session_write_close.

 

Quote From Xtopolis - "If you read the comments I posted a link to in your other thread, you would already know."

Xtopolis, you where adament how the answer was so obvious and how I should have read your earlier links (which was when we were talking about SSL). You were wrong, and you wasted a lot of my time. You never mentioned session_write_close. Whilst all help is free and I appreciate it, you mocked me here, when clearly you had no right.

 

Page 1:

session_start();

set sessions etc.

session_write_close();

Do cURL

 

Page 2:

session_start()

echo $_SESSION

 

 

The session writing closes automatically at the end of a request; I've never felt a need to use that function.  Try using your code without the function and see if it still works with the proper COOKIEFILE parameter.

It does close automatically at end of request.

 

Whilst using cURL you are basically using 2 http requests/sessions. (so the inital request hasn't closed.)

 

Tried without and it just locks up, resulting in 500 error.

Yes, same server. My previous post was initially about why cURL would not work - where I didnt even consider session problems.

 

In that I posted:

"Is there an alternative to cURL where I can get the session values?

Everything is all on my same server. "

 

which everyone else would have seen (including Xtopolis).

 

I suppose you didn't see all that DarkWater as you have just joined things.

 

Also, everyone kept mentioning various things to add, but the only thing on top of a normal cURL response is:

curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());

 

(As well as session_write_close)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.