Jump to content

cURL setopt cookies


53329

Recommended Posts

I can't seem to get CURLOPT_COOKIE or CURLOPT_COOKIEJAR to work.  So the first is self explanatory: I can't get it to send cookies because I can't find the syntax anywhere.  However cookiejar confuses me.  I get no error messages and no result in the file I assigned.  I even gave the file and directory write permission and had that php script used fopen to create it to see if that was the problem.

 

Also do either of these work with session cookies as well?

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/101057-curl-setopt-cookies/
Share on other sites

  • 3 months later...

Is it better to post a question in similar but older topic, or start a new one?

In any event, I could not figure out how to post a new topic, so here goes...

 

I have a function "Redirect", as shown below (comments and error-checking removed for brevity):

 

From the little research I've done, I thought that setting the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE options as I have done would make the current session cookie available in the redirected page. But it is not, and nothing appears to be written to curl_cookies.txt.  It then tried creating the file in case cURL could/would not do it, and I set the file has permissions 666 so that it can be read and written to. It is under the home directory so it cannot be browsed to. Did I not specify the path correctly?

 

function Redirect($url, $postinfo) 
{ 
    $curlh = curl_init(); 
    curl_setopt($curlh, CURLOPT_URL, $url); 

    if(isset($postinfo)) 
    { 
        curl_setopt($curlh, CURLOPT_POST, 1); 
        curl_setopt($curlh, CURLOPT_POSTFIELDS, $postinfo); 
    }        

    curl_setopt($curlh, CURLOPT_COOKIEJAR, "/curl_cookies/curl_cookies.txt"); 

     curl_setopt($curlh, CURLOPT_COOKIEFILE, "/curl_cookies/curl_cookies.txt"); 
        
    curl_exec($curlh); 
    curl_close($curlh); 
  
    return 1; 
} 

 

This is how it is being used:

 

The flow follows the "Dispatch" method suggested in this article http://shiflett.org/articles/secure-design.

 

All dynamic pages (excluding Gallery) are entered through http://www.hrsms.org/run.php with an option set as a GET parameter, ex. http://www.hrsms.org/run.php?script=resources

 

If a vistor requests a private page ex. http://www.hrsms.org/run.php?script=members, they are redirected to the login page, if not already logged in.  When the form is submitted and the user validated the form posts back to the originally requested page, the script see the user is logged in, and the content is displayed.  Again, in a little more detail:

 

 

 

The login form tries to match a token (hidden field of form) to the current session token.  The problem appears to be that the session cookie is not available after redirecting using cURL, so the form creates a new token, which will not match the current session token.  The current session is available when keying in a new web address, clicking a link <a href="...> or posting the form.  It is the cURL redirect that is not passing along the cookie (or whatever the correct term may be).

 

It's the first time I've attempted to use cURL.

Can anyone spot my error above?

 

Thanks!!

Link to comment
https://forums.phpfreaks.com/topic/101057-curl-setopt-cookies/#findComment-607619
Share on other sites

I've tried a number of other things, including moving the cookie file (temporarily) to public_html.  There was no difference.

 

I'm still stuck on this and would appreciate any help I recieve.  Soon I'll have to give up on the automatic redirects and add a couple of "click to continue" links.  I sure would like to avoid that though...

Link to comment
https://forums.phpfreaks.com/topic/101057-curl-setopt-cookies/#findComment-609020
Share on other sites

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.