NameSilo

Cookies on file_get_contents or cURL?

Spaceship Spaceship
Watch

tm

Established Member
Impact
25
Hi,

I need to load up a page, but to view the page you need to be logged in (ie have cookies set).

How do I set cookies in a php script so when I use file_get_contents() or cURL (don't know much about cURL) it loads the logged-in page rather than the login page?

Hope I made myself clear.. please say if you don't understand.

Thanks

Colin
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
0
•••
You think that'd work like that, would it store the cookies on the server?
 
0
•••
I would assume cookies are stored in "cookies.txt", seems logical.

But like i say i have never used cURL.

Matt
 
0
•••
So something like this should work?
PHP:
$url="http://url.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "fieldname=fieldvalue&fieldname=fieldvalue&");
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
$content = curl_exec ($ch); # This returns HTML
curl_close ($ch);
 
0
•••
tm said:
How do I set cookies in a php script so when I use file_get_contents() or cURL (don't know much about cURL) it loads the logged-in page rather than the login page?
You definitely need curl. Curl can handle cookies for you.
 
0
•••
CURLOPT_COOKIEJAR is used when you fetch a page that sets cookies. It declares a name of a file in which the cookies will be stored.

Then you use that filename with CURLOPT_COOKIEFILE on subsequent requests to tell cURL to send the cookies in that file along with your request(s).

For security reasons, be really careful where you keep the cookiejar file - unless there's a reason to keep it, delete it when you're done!

If you just need to send a couple of cookies and you know what they are, you can use CURLOPT_COOKIE followed by a comma and a string (in single quotes) containing comma-delimited cookies and values. For example:

curl_setopt($c, CURLOPT_COOKIE, 'cookie1=value1, cookie2=value2, cookie3=value3');​
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back