I have got this code:
public function get_thead_page($cookie=null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_COOKIEFILE,'');
if($cookie) curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Now I dont want to use my cookie value, but I want the browser to handle it for me. I wan tthe request to behave as if it was given by the browser.
So I want to the cookie to be loaded with the request instead of providing a value...
There is that value..
curl_setopt($ch, CURLOPT_COOKIEFILE,'');
which asks for the cookie file location...but I dont want to specify the location, I want the request to be sent with a cookie being loaded somehow without specifying the path on the system..
Is there any solution?
The browser can't do that. CURLOPT_COOKIEFILE refers to a server-side file which the browser have no access.
You're the one who made this app. It's to you to choose the cookie's location when you create it.
Related
Im trying to set a cookie through PHP CURL for more than twenty four hour for no avail.
Before i have been setting cookies in my browser by adding them as parameters in a url as shown below
http://localhost/setc.php?userid=123&panelid=1
but now i need to set the cookie when i run a script(setcookie.php)
below is the latest of various types of code that i tried.
setcookie.php
$c = curl_init('http://localhost/atst.php?userid=628929&panelid=1');
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_COOKIE, 'userid=123; panelid=1');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);
it still does not create the cookie, can anybody help out
P.S : if you guys too cant figure this out at least give me a hint/guide on how to set a simple cookie without any complications
The cookiejar is only saved when you close the curl handle using curl_close($ch).
From the manual:
CURLOPT_COOKIEFILE The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file. If the name is an empty string, no cookies are loaded, but cookie handling is still enabled.
CURLOPT_COOKIEJAR The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.
http://www.php.net/manual/en/function.curl-setopt.php
$ckfile = tempnam ("/tmp/", "CURLCOOKIE");
$BASEURL='http://localhost/openx/www/api/json/index.php/main/authenticate/';
$POSTFIELDS='username='.$username.'&password='.$password.'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,'http://localhost/openx/www/api/json/index.php/main/authenticate/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
ob_start(); // prevent any output
$result=curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
$result = curl_exec($ch);
curl_close($ch);
I have a php proxy server to which email and password are posted on the beginning (login). I then use
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
to post forward to some API that expects certain parameters. As you can see I save a cookie in a cookie jar file.
I can then use this cookie file to call any other requests to proxy -> API and successfully get the response. Everything works just fine. I use
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieJar);
to make other requests after user successfully signed in.
The problem is that only one user can login(and call other requests) at the time, because there is only one cookie jar file. I could probably generate unique cookie files on proxy and access them somehow with each new request by each user. But that is a load on the server and definitely not a good idea.
So what I would like to do is to save a cookie that is received into variable instead of a file and then send this to user...
This didn't work for me unfortunately; I can probably manage to write my own regex but I am wondering if there is a possibility to directly save a cookie into variable with curl or do I have to parse headers manually? (I want to be able to feed CURLOPT_COOKIEFILE with cookie in variable rathen than cookie in file)
Lets try this with a single curl handle($ch):
Making my first request:
$url= "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, '-'); // <-- see here
$result = curl_exec($ch);
// remember i didn't close the curl yet!
Now make another curl request with the same handle:
$url= "http://www.google.com";
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
// if you are done, you can close it.
curl_close($ch);
In this example, I have used the - for the cookiejar. Which means it will not use any file. So during second curl request, it will use the cookiejar of previous call.
One problem: It will print the cookie jar values into std-output.
I have a cURL script that is sending login info to a script.
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//open connection
The script has a setcookie function.
setcookie("cookie_email",$email,time()+(3600*24*$i),"/");
setcookie("cookie_password",$password,time()+(3600*24*$i),"/");
When I login to the form using the form everything works as expected. For some reason when you run the cURL it's skipping the setcookies function.
I've been all over the net and I can't find a solution. I'm not sure why it's failing to set the cookies.
Any step in the right direction would be much appreciated.
Thanks,
Phil
UPDATE! - Getting Closer
Okay I have made some changes that grab cookies and put them into a cookie file. Two Issues I set.
1. The cookied password in the file reads: deleted
2. The cookies aren't being set in the browser.
How do I get the md5($password) into the file and how does:
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
set the cookies in the browser?
You must set the CURL_COOKIEJAR and CURL_COOKIEFILE options for curl to set where cookies should be stored and loaded from respectively.
EDIT: Your example rewritten:
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
This assumes you have created a directory 'cookies/' and will save the cookies in a file called 'cookies.txt' (as long as your webserver can write to that directory, it will create the file itself)
Subsequent requests will then use any cookies stored in cookies.txt when sending their request (assuming you set the cookiefile for that request as well)
So I have a script that gets passed a URL as a get param, and consequently tries to write the contents of that page to a file. On non-authenticated pages, it works fine, but it breaks when I try to use it on pages that require authentication.
Specifically, these are pages that I have already logged in to, so I assumed making a standard cURL request with all my currently set cookies would work, but it doesn't. Initially, I receive a 302 response, but if I set 'FOLLOWLOCATION' to true, I end up at the site's log-in page instead of the page I wanted.
$client = curl_init();
//curl_setopt($client, CURLOPT_HEADER, true);
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
curl_setopt($client, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($client, CURLOPT_URL, $pageToLoad);
/* Supposedly libcurl loads all cookies auto-magically if you don't set CURL_COOKIESESSION
if (isset($_SERVER['HTTP_COOKIE'])) {
curl_setopt($client, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE']);
}
*/
$response = curl_exec($client);
curl_close($client);
I have the sinking feeling that I'll need to set a cookiejar file for this to work ('sinking', because my script doesn't have write permissions on the dev server), but what are your thoughts?
EDIT:
So, being sort of an idiot, turns out I do have some write access... anyway, made a temp folder in my home directory, where I've verified my script can write:
$fname = '/home/temp/cookies.txt';
if( $save = fopen($fname, 'w') ) {
fwrite($save, 'testing');
}
Run the script, and low and behold, 'testing' shows up on line one.
I've added the following 2 lines to my cURL request:
curl_setopt($ch, CURLOPT_COOKIEJAR, '/home/temp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/home/temp/cookies.txt');
And nothing has changed in my scenario. The cookies.txt file still just says 'testing' and doesn't have any cookies stored in it, nor does the 'last access' time change, which to me means these lines aren't doing anything.
Any further thoughts? I'm kinda stumped :\
Yes, you'll need both the CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR options set. the 'file' version is where to load cookies from. the jar is where cookies should be saved when the curl object is terminated. They can be the same file.
If you don't have write permissions on the server to create an actual file (very odd if you didn't have at least write perms on /tmp or equivalent), you can use a memory file using the php://memory wrapper.
Here's my beloved pattern for tasks like this:
$loginUrl = 'http://www.remote_site.de/login.php';
$loginFields = array('username' => 'username', 'password' => 'password');
getUrl($loginUrl, 'post', $loginFields);
//now you're logged in and a session cookie was generated
$remote_page_content = getUrl('http://www.remote_site.de/some_page.php');
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
$buffer = curl_exec($ch);
curl_close($ch);
return $buffer;
}
So I took out the raw cURL code I had and copied in some Zend specific code from a different project, and that seemed to work for the most part (not really sure why...). The only other problems I've run into deal with CoSign (http://cosign.sourceforge.net/) and using proxy cookies. Thanks to those who offered answers!
Im using cURL to post data to a php file (setcookie.php) on another domain.
The file, setcookie.php is supposed to set a cookie on that domain with the data posted to it.
The problem is the cookie doesn't want to be set when im doing it with cURL, because cURL returns to the calling file/domain, i guess.
So how can I make cURL not come back to the calling file?
Or is there an easier way to do this?
Here's my code :
$ch = curl_init ("http://<other domain>/setnewcookie.php");
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, false);
$returndata = curl_exec ($ch);
Here's what you need to do:
$ch = curl_init('http://example.org/setnewcookie.php');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
For cookies to work with cURL, you need to define both CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE. ALso, if you don't want the content of "http://example.org/setnewcookie.php" to be outputted to the browser, you need to set CURLOPT_RETURNTRANSFER to TRUE.
This will create a cookie on your server that cURL can use for subsequent requests but it won't allow the user of your website for instance to use that cookie. If the intent is for the user to be logged in on both sites, this will not work as-is.
For cross sub-domains (as in between www1.example.org and www2.example.org), have a look at PHP authentication with multiple domains and subdomains.
If you want the cookie to get sent from domain2 to browser, browser needs to make request directly.
So if you must get the information from domain1 and user must not get it directly, I'd somehow encrypt the data and redirect browser to send the request to domain2 like this:
domain1/script.php
$return_url = 'http://domain1/script2.php';
$request_url = 'http://domain2/setnewcookie.php';
$request = $request_url . '?data=' . url_encode($encrypted_data) . '&return_url=' . urlencode($return_url);
header('Location: ' . $request);
exit;
And then in domain2/setnewcookie.php just decrypt the data, set the cookie and once that is done, redirect user back to domain1 with help of the $return_url.
Still not sure if this was what you were trying to accomplish, HTH.