473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PHP + cURL + session id

Hello all,
I have simple PHP application that on one page strarts session and
write some information to it. On another page program tryes to fetch
information from a third page using cURL. On that third page program
needs to retrive information that were written to the session and use
them. Problem is that when I call the third page and call
session_start() it starts a new session where there are no infromation
I need. Code below.

<code>
<?
/**
* file: t1.php
*/
session_start();
$_SESSION['user'] = 'lukas';
echo '<html>
<body>
<a href="t2.php">'.$_SESSION['user'].' 2 >> </a>
</body>
</html>';
?>

<?
/**
* file: t2.php
*/
session_start();
$_SESSION['user'].=" add some info.";
$url="http://www.myserver.com/t3.php"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
//curl_setopt($ch, CURLOPT_COOKIE,
session_name()."=".session_id().";"); //when I uncomment that line
operation times out
$output = curl_exec($ch);
echo "output: ".$output."<br/>";
echo "error no: ".curl_errno($ch)."<br/>";
echo "error: ".curl_error($ch)."<br/>";
curl_close($ch);
?>

<?
/**
* file: t3.php
*/
session_start();
if(isset($_SESSION['user']))
echo "yeah<br/>";
else
echo "nope<br/>";
?>
</code>

My PHP Version 4.4.2-1

My php.ini session settings:
Session Support enabled
Registered save handlers files user
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /var/lib/php4 /var/lib/php4
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off

My php.ini cURL settings
CURL support enabled
CURL Information libcurl/7.15.3 OpenSSL/0.9.8a zlib/1.2.1.1
libidn/0.5.18

Any help would be appreciated.

Kind regards,
Luke

Apr 21 '06 #1
2 14658
Problem is in session identification.
When you start a session, it gets unique identifier, which is passed to
your browser (in cookie or, if cookies are off, in GET parameter). Value
stored in the session is saved on the server and next time you call
session_start, PHP finds it on the server according to this identifier.

For the third script the calling client is not your browser, but cURL
client on the server. However, it doesn't know the session identifier,
so the session is empty.

What you need is to pass session identifier to the URL called by cURL in
second script as a GET parameter.

lookee wrote:
Hello all,
I have simple PHP application that on one page strarts session and
write some information to it. On another page program tryes to fetch
information from a third page using cURL. On that third page program
needs to retrive information that were written to the session and use
them. Problem is that when I call the third page and call
session_start() it starts a new session where there are no infromation
I need. Code below.

<code>
<?
/**
* file: t1.php
*/
session_start();
$_SESSION['user'] = 'lukas';
echo '<html>
<body>
<a href="t2.php">'.$_SESSION['user'].' 2 >> </a>
</body>
</html>';
?>

<?
/**
* file: t2.php
*/
session_start();
$_SESSION['user'].=" add some info.";
$url="http://www.myserver.com/t3.php"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
//curl_setopt($ch, CURLOPT_COOKIE,
session_name()."=".session_id().";"); //when I uncomment that line
operation times out
$output = curl_exec($ch);
echo "output: ".$output."<br/>";
echo "error no: ".curl_errno($ch)."<br/>";
echo "error: ".curl_error($ch)."<br/>";
curl_close($ch);
?>

<?
/**
* file: t3.php
*/
session_start();
if(isset($_SESSION['user']))
echo "yeah<br/>";
else
echo "nope<br/>";
?>
</code>

My PHP Version 4.4.2-1

My php.ini session settings:
Session Support enabled
Registered save handlers files user
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /var/lib/php4 /var/lib/php4
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off

My php.ini cURL settings
CURL support enabled
CURL Information libcurl/7.15.3 OpenSSL/0.9.8a zlib/1.2.1.1
libidn/0.5.18

Any help would be appreciated.

Kind regards,
Luke

Apr 21 '06 #2
Thanks for your reply.
I did what you had suggested and still no luck. I appended
session_name()."=".session_id() to the URL I wanted to fetch and the
results were the same as when I set this line in the second script:
curl_setopt($ch, CURLOPT_COOKIE, session_name()."=".session_id().";");
"Operation timed out with 0 out of -1 bytes received". If you have any
other suggestions let me know.

Thanks.

Best regards,
Luke

Apr 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
What is the most standardized method of utilizing the CURL functions in PHP (version 4.3.2) to be able to retrieve the contents of a remote URL that happens to be dependent upon $_SESSION for its...
9
by: Dave Martin | last post by:
I've successfully used CURL to maneuver around and through sites but a new site I've been trying to work with has got me stumped. To retrieve the data I'm trying to get a login is required. If...
3
by: Leo | last post by:
Can anyone help or offer some explanation with this problem: I'm trying to do a POST from one of my PHP pages, to another page on my site using curl. To maintain the session I'm sending the...
1
by: mickey | last post by:
I am trying to post an array using cURL from one PHP page to another. I can't figure out how to correctly post and read the array on the page that actually receives it. Here is what I have... ...
9
by: devranger | last post by:
I am using the below CURL Function and can not figure out why it is not retruning the results from the post. Can anyone take a look and tell me what I may be doing wrong? I am just not seeing...
3
by: JDS | last post by:
So, I'd like to create the following scenario: 1) Use cURL library within PHP (cURL + "Cookie Jar", et.al) to create a virtual browser session that "logs in" to a remote site. (For example: ...
1
LeoTD
by: LeoTD | last post by:
Dear all, I'm a newbiew in Curl functions. I want to transfer Session data from this page to another in difference server. It means: page1 have a link to page 2. When i click to link in page 1,...
11
by: Flexor | last post by:
I have a php script that runs from command line and makes an https request to paypal, using curl. It works fine if I run it from a web page. It fails if I run it from CLI. The error I get from...
3
by: buzz2050 | last post by:
Hi all, I am using cURL for the first time. I need to login to a site and my cURL code to do the same is as follows: //curlScript.php <?php function getContent($url, $referer,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.