0

I have a few questions about cURL:

  1. Do I need to use session_start() with cURL?

  2. If not, how do I get the session_id() from cURL?

  3. When is that session_id() generated?

  4. What's the difference between initializing a cURL session (curl_init) and execute a cURL session (curl_exec)?

hakre
  • 193,403
  • 52
  • 435
  • 836
user765368
  • 19,590
  • 27
  • 96
  • 167
  • 2
    What are you trying to accomplish? PHP sessions and cURL don't have much to do with each other. – gen_Eric Dec 14 '11 at 18:47
  • 2
    could you explain what you want to accomplish? Sessions and cURL do not have anything in common in the sense you seem to imply in the question. – Lars Dec 14 '11 at 18:47
  • I'm trying to share session_id between two sites. For example, send the session id of site A to site B (to be logged on site B as well) – user765368 Dec 14 '11 at 18:48

4 Answers4

0

cURL is a library for establishing connections to servers (just like your web browser does) and has no session_id to speak of (as it's just retrieving what the client sees and session_id() would be a server-side method).

With that said, usually the session ID can be found in the headers of the request, such as the Set-Cookie header (or sometimes in the URL itself). So, if you need to determine the session I would look in to using the Cookie Jar functionality of cURL or make sure to retrieve the contents with headers so you can view the Set-Cookie value(s).

Follow-Up:

  • curl_init is establishing a new cURL request. Basically, it creates a new cURL instance which you can then configure with any of the numerous curl_setopt settings.
  • curl_exec is, based on your configuration, executing the cURL request and returning the result. Depending on your settings, this may be the actual content you're going for, a set of headers, a single status result, or some other value.

<?php
  // create a new instance of curl we can configure
  $curl = curl_init('http://google.com');

  // begin configuring that instance
  curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=12345ABCDE67890');

  // now fire off the result, with the specified cookie attached
  // (in this case, it's fetching google.com with a cookie named PHPSESSID
  // and a value of 12345ABCDE67890)
  $result = curl_exec($curl);
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • Question, I used the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE options to save cookie data to a file. I saw that the PHPSESSID is saved in the file as well, how do I read that data back (without using fopen or something like that) – user765368 Dec 14 '11 at 19:05
  • @user765368: Unless you're making everal consecutive requests, cookiejar may not be the best option (and if you want the value immediately, it probably isn't the best method either, but I didn't know your intent either). That said, look in to `CURLOPT_HEADER` where you can then parse `Set-Cookie` from the response and get what would essentially be stored in the cookiejar file. – Brad Christie Dec 14 '11 at 19:09
  • ok, I used CURLOPT_HEADER. But how do I get the PHPSESSID cookie sent in the header? – user765368 Dec 14 '11 at 19:34
  • @user: paste the entire line in here and I'll create a method for you. – Brad Christie Dec 14 '11 at 19:41
0

php session is transmited in cookies by default, if you get the web by cURL, and trying to access another section of the same web, then you must add session which you got from cURL

jmp
  • 2,456
  • 3
  • 30
  • 47
0

You may want to consider an alternative solution to your problem of handing off a user from one site to another and still maintain their 'logged in' status.

I posted a response to a similar topic you may want to check out: Session lost when switching from HTTP to HTTPS in PHP

In a nutshell, you can create a hash key, redirect the user to siteB, look up the hash key and if they line-up, log the user into siteB.

Community
  • 1
  • 1
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
0

First, sessions and cURL are completely different and unrelated things. The session acts like a server side cache for the current user. cURL is a way of sending HTTP requests in PHP such as GET and POST. Some APIs require the use of cURL to interact with them from within your own PHP scripts/code.

The session_id() function is used to either get or set the current session id of the current user. If you want to set the session id to 7 for example, you would call

session_id(7);
session_start();

If you wanted to get the session id, you would call

session_start();
$CurrentSessID = session_id();

The session id is generated when you call session_start() unless you've set it yourself before session_start() is called.

David Myers
  • 799
  • 1
  • 9
  • 18