Jump to content

curl and cookies


kireol

Recommended Posts

So I have a script that I wrote 2 years ago that uses cookies just fine with cURL.

 

I started writing a new script, so I copied and started editing the new script.

 

Now, the old file works with cookies and the new one doesnt.

 

I stripped the new file down to basics and found a simple site that uses cookies.  I verified with firefox that the site does produce cookies.  but they are not saved in the cookie file this generates.

 

Please help me figure out why.

 

<?php
function getpage($url)
{
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch = curl_init(); 
      $cookies = "cookies/cookiejar.txt"; // Please set your Cookie File path
      if ($cookies != '')
      {
	if (substr(PHP_OS, 0, 3) == 'WIN')
            	{$cookies = str_replace('\\','/', getcwd().'/'.$cookies);}
	$fp = fopen("$cookies","w") or die("<BR><B>Unable to open cookie file $cookies for write!<BR>");
	fclose($fp); 
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
}
else
{
	echo "COOKIES NEED TO BE SET UP\n";exit;
}
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec ($ch);
return($result);
}
$result = getpage("http://www.echoecho.com/samplecookie2.htm");
?>

Link to comment
Share on other sites

I did a bit more testing and still stuck.  here's some more results.

 

 

If i change to stdout on the cookie file

 

curl_setopt($ch, CURLOPT_COOKIEJAR, '-');

 

it DOES print out a cookie to the screen.

 

This cookie, however, is not saving to the cookie file.

 

 

If I copy and paste that cookie info, into the cookie file, then re-run the script, php clears out my cookie file back to null.

 

 

arggggggg!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.