I have a php script that is being used login into a remote site and it is working well. However it seems to only works when the designated cookie file is named cookie.txt
What I want is for each person who uses this script to have a separate cookie file. So if a person on machineA uses it on the 19th they're cookie will be located in something like /tmp/19/someNameCookie.txt, machineB would have /tmp/19/someOtherNamedCookie.txt, and if machineC uses it on the 20th they will have /tmp/20/someNamedCookie.txt.
There are many ways to generate uniquely named files but the key here is getting them to work. I've very young in the php knowledge and completely new to curl.
On a side note, I've also have had no success using CURLOPT_FOLLOWLOCATION for getting the new page that results from logging in.
Thanks.
Here's a code exert. Got to this from another source.
$username=urlencode($usr);
$password=urlencode($pass);
$url="http://domain.com/";
$cookie="cookie.txt";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // have tried with just the jar, and just the file and both
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
And this is the code I would call after to check if the login is successful
$ch2 = curl_init();
//curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch2, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch2, CURLOPT_URL, "http://domain.com/");
$r = curl_exec($ch2);
echo $r;
curl_close($ch2);
From my understanding curl is supposed to create the cookie file if it doesn't exist. I've also tried creating other files manually and through script using fopen but still only works if I'm using cookie.txt, and right now it's all being saved to public_html just for testing.
set the Cookie Jar
<?php
$machine = 'PC_A'; // here it's your problem how you define each machine
$cookie_file = "/tmp/".date('d')."/".$machine."_cookie.txt";
// and set the cURL cookie jar and file
if (!file_exists($cookie_file) || !is_writable($cookie_file)){
echo 'Cookie file missing or not writable.';
die;
}
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
?>
use for example session_id() for cookie file name, it is unique for every single machine connecting to your application, and its still the same till it expires
Related
I am not sure how can or if I can use php multi curl function in the following situation:
I need to curl 3 different links while all of them are dependent to each other.
With the unique id obtained from the first link I will access the second link to retrieve another unique id which will be used in the 3rd link for the final result. See example below for better understanding:
$agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
//open the session with the first link
$url1 = "http://first-link-example.com/execute";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
$results = curl_exec($ch);
//get unique id from the link above
$unique1 = explode("string1", $results);
$unique1 = explode("string2", $unique1[1]);
//new request to access second link while preserving the session
$url2 = "http://second-link-example.com/execute?id=".$unique1[0];
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
$results2 = curl_exec($ch);
//get the second unique id from $results2 page
$unique2 = explode("string1", $results2);
$unique2 = explode("string2", $unique2[1]);
//final request to retrieve the desired string while preserving the session
$url3 = "http://third-link-example.com/execute?id=".$unique2[0]."/response";
curl_setopt($ch, CURLOPT_URL, $url3);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
$results3 = curl_exec($ch);
//get the desired string from $results3 page
$success = explode("string1", $results3);
$success = explode("string2", $success[1]);
echo $success[0];
curl_close($ch);
I did a lot of research on the web but couldn't find something appropriate and I wasn't able to understand how can I use multi curl in my situation or if it's possible.
Hopefully I made myself clear enough regarding what I'm looking for.
Thanks in advance to anyone who will be able to help me.
Short answer - NO.
Long answer - cURL multi handles are intended for asynchronous requests, i.e requests that don't block each other. In your case, each request blocks the one beforehand, as it is dependent on its response, so they must only be ran synchronously.
I am trying to write an "auto-login" script, that uses curl to log in to a specific site and then echoing the response, so that the user can "bypass" the login.
(For example, to grant one-time logins etc)
Obviously, using the following method, the cookie is stored on the server, not on the client, in cookie.txt.
I've tried setting $_COOKIE, but that won't work, since the URLs (actually subdomains) of the auto-login script and the service to be logged into, are different.
Login works fine. You see the screen as if you were logged in, but clicking any link requires you to login again.
Any ideas? Thanks
The code I've used to auto-login:
<?php
$username="email#domain.com";
$password="password";
$url="http://sub.domain-to-login.com/index.php/sessions/login";
$cookie="cookie.txt";
$postdata = "email=".$username."&password=".$password.'&btn_login=Login';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
?>
I guess that you need to replace all links (in $result) from http://sub.domain-to-login.com/index.php/stuff to something like http://mysite.com?url=http://sub.domain-to-login.com/index.php/stuff and then using curl download this site again.
You just cannot set cookie for diffrent site.
I'm trying to login to a remote site using CURL but I just keep getting the error message your session has expired, please re-login.
$username="xxxxxx";
$password="yyyyyyyy";
$url="https://remotesite.com/my.activation.php3";
$cookie="cookie.txt";
$postdata = "username=".$username."&password=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
// check for errors before close
$result = curl_exec($ch);
echo $result;
curl_close($ch);
The cookie file is writeable and something is getting written to it. Any ideas what I can try to keep the session going?
Make sure you're using the correct option for CURLOPT_COOKIE*.
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.
CURLOPT_COOKIE - The contents of the "Cookie: " header to be used in the HTTP request. Note that multiple cookies are separated with a semicolon followed by a space (e.g., "fruit=apple; colour=red")
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.
Additionally, enabling CURLOPT_VERBOSE may be of assistance when debugging cURL.
use curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
My code is doing the login to a remote site using CURL. But it is not keeping the session alive there. In my case I am logging into the site using the following code.
$username=$options['session[login]'];
$password=$decryptedPassword;
$cookie = tempnam("/tmp", "cookies");
$postdata = "session[login]=".$username."&session[password]=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
$result = str_replace("href=\"","href=\"$onappurl\\",$result);
$result = str_replace("href=\"$onappurl\\","href=\"$onappurl",$result);
echo $result;
curl_close($ch);
Now the result is echoed in a popup and it is showing logged in interface. But if I click on any link of the site, I loose the session and it is redirected to the login page.
I think this is because your server is sending the request, and the remote site is creating a session for your server, not for you. So, if you click a link that redirects to that remote site, and you do not have an active session there, you will be prompted to log in.
I am trying to log into a website, then go to a link and then submit a form to get the desired data. I want to do it using cURL. I have achieved success in loggin into the website. The login redirects me to the profile page.
Now I need to follow a link and then submit a form! but when i do that using CURL the session is invalidated. I get the JSESSIONID in a cookie.txt file i used to store the cookie created. All the examples i have seen are just about loggin into a website or just a registration form submission.that is just one single POST request!
How do I go to another link and then submit another form using curl after I have successfully logged in and stored the cookie?
I am using WAMP as my local server.
<?php
$username="myusername";
$password="mypassword";
$url="http://onlinelic.in/LICEPS/Login/webLogin.do";
$referer = "http://onlinelic.in/epslogin.htm";
$postdata = "portlet_5_6{actionForm.userName}=".$username."&portlet_5_6{actionForm.password}=".$password;
$cookie = "cookie.txt" ;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
curl_setopt($ch,CURLOPT_COOKIESESSION,false);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, $referer);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
logIntoLocator();
curl_close($ch);
function logIntoLocator()
{
$pincode = "731234";
$locatorType = "P";
$url = "http://onlinelic.in/LICEPS/appmanager/Customer/CustomerHome?_nfpb=true&_windowLabel=Cust_Agent_Locator_portlet_25_2&Cust_Agent_Locator_portlet_25_2_actionOverride=%2Fportlets%2Fvisitor%2FAgentLocator%2Flocating";
$referer = "https://customer.onlinelic.in/LICEPS/appmanager/Customer/CustomerHome?_nfpb=true&_windowLabel=CustomerLocatorsPortlet_1&_cuid=RC_t_832059&_pagechange=AgentLocator";
$postData = "Cust_Agent_Locator_portlet_25_2wlw-radio_button_group_key:{actionForm.agentRadioOption}=".$locatorType."&Cust_Agent_Locator_portlet_25_2{actionForm.agentOption}=".$pincode;
$cookie = "cookie.txt" ;
$agentCurl = curl_init();
curl_setopt ($agentCurl, CURLOPT_URL, $referer);
curl_setopt ($agentCurl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt ($agentCurl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
curl_setopt($agentCurl,CURLOPT_COOKIESESSION,true);
curl_setopt ($agentCurl, CURLOPT_TIMEOUT, 60);
curl_setopt ($agentCurl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($agentCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($agentCurl, CURLOPT_REFERER, $referer);
curl_setopt ($agentCurl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($agentCurl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($agentCurl, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($agentCurl, CURLOPT_POST, 1);
$result = curl_exec ($agentCurl);
echo $result;
}
If you like to give it a try the username is "manashch1" and the password is "nokia1105*". login there and go the AgentLocator and there u can enter pincode as 731234 and get the data needed.
You need to attach the cookie(s) to CURL. There are two options:
You can do this all manually by reading the cookie, and attaching manually using
$ch = curl_init();
$sCookie = 'JSESSIONID=' . $sessionIDSavedEarlier . '; path=/';
curl_setopt ( $ch , CURLOPT_COOKIE, sCookie );
... rest of POST
You can use a "cookie jar" (probably easiest)
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
... rest of POST
(Making sure you can read/write to a file called "cookie.txt" - if you have multiple users using hte script, then make that file unique for each user through a PHP session!)
So you want to login, get the page and then goto a page which is only accessible once logged in?
If so, then ensure that the cookie.txt is writeable by your script and check the url doesn't contain any special parameters (like your session ID). Are you extracting it from the profile page.
If you know the content of the form, you should just be able to post direct to that forms destination, but you may need to set the referring page to the site your logging into.
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
Use this in your cUrl script .
Where
$postfields = 'form1=value1&form2=value2&form3=value3';
where form1,form2.. are the 'name' attributes of your inputs .