Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Using PHP/CURL with Cookies

Avatar of blainehilton
blainehilton asked on
PHPWeb Applications
11 Comments1 Solution5057 ViewsLast Modified:
I need to access a web page and pass a PHPSESSID from an existing session.

I have tried it two ways (see code).

Both of these work perfectly in their own script.  However when I put either of these code segments into my main page it dies.  Basicially with #1 using file_get_contents(), I get no error message, it just times out, and after 120 seconds the rest of the page loads.

With #2 CURL I get a CURL error 28, Operation timed out after 5 seconds with 0 bytes received.

I know for sure that the page loads as it loads when these snippits run by itself, as well as it runs when I go into the command line and open it with lynx and/or wget.

The main page seems valid, it works except for pulling in the string from the external page.  The main page is fairly complex in that it does have set a session, it uses a lot of javascript/AJAX type code, as well as PHP/DB type stuff as well.

I need this answered by tomorrow, or I'm going to have to start over from scratch with a different approach.

The solution needs to make this code work in my main page.

Please let me know if I can provide any other info.

Also this is being ran on a PHP5 based system running Red Hat/Apache/Plesk.  Open basedir is turned off, and allow_url_fopen is enabled.  Again the scripts above work in isolation, but not from within the main page.

Thanks in advance for any assistance
$phpsessionID	= 'ahnvr6hr1veh6rru4brjm3aj45' ;
$url		= 'http://website/directory/file.php?ID=XXXX&second=YYYY' ;
$opts = array(
		'http' => array(
			'method' => 'GET',
			'header' => 'Cookie: PHPSESSID='.$phpsessionID
$context	= stream_context_create($opts);
$string		= file_get_contents($url, 0, $context);
$phpsessionID	= 'ahnvr6hr1veh6rru4brjm3aj45' ;
$url		= 'http://website/directory/file.php?ID=XXXX&second=YYYY' ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$outputA = curl_exec($ch);
$string	= $outputA ;
Avatar of lavinpj1

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answers