0

I'm making a DIGEST AUTH using cURL and it's working perfect. The problem is, I need the browser to keep those Digest Credentials made on the login page, in all other pages without having to resend the user:pass again.

Example:

I'm in login.php, I write my user and password, I make a cURL request to authenticate, servers responds with OK, so I redirect to index.php, which requires to be authenticated, but it will ask for username and password again, it didn't keep it from login.php. How do I fix this?

Guj Mil
  • 333
  • 4
  • 16

1 Answers1

1

You need to use Cookie Jar for that, eg:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/* more cURL options */
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');

$output = curl_exec($ch);
curl_close($ch);
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • Still not working, maybe I'm doing something wrong, I added those cookie options, and when redirecting, I make a auth check using curl, calling the http digest auth resource, but without sending credentials since i dont have them anymore, but will get 401 every time – Guj Mil Aug 23 '12 at 14:57
  • The cookie file is writable? Do you see contents in that file? – Mihai Iorga Aug 23 '12 at 15:26
  • I actually can't find this file anywhere – Guj Mil Aug 23 '12 at 15:33
  • `/tmp/cookies.txt` has to be writable by cURL, and you can change it's path in any way you want, you can set it as `cookies.txt` next to the php file. – Mihai Iorga Aug 23 '12 at 16:05
  • But where exactly is this file being created, or where exactly should i create it? supposing my site is in localhost/mysite, where should it be placed? – Guj Mil Aug 23 '12 at 22:52
  • Where you want it to be placed. It's best to make a path outside your website directory, eg: `c:\tmp\cookies.txt`, I guess you are on Windows. – Mihai Iorga Aug 24 '12 at 05:18