How to keep session informations using curl in php? - php

Access to fictitious page www.randomDomain.com/onlyForRegisteredUsers is possible only when I will log in first. So
I did this:
...
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curl, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->currentUseragent;
curl_setopt($this->curl, CURLOPT_PROXY, $this->currentIP);
curl_setopt($this->curl, CURLOPT_URL, $this->currentUrl); //www.randomDomain.com/login/
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->currentData);
//with login RandomUserName and pass *******
$page = curl_exec($this->curl);
And successfully in $page I have got result something like this: ...<p>Welcome back, RandomUserName!</p>.
How to keep informations about session that I am already logged in, and then read content from www.randomDomain.com/onlyForRegisteredUsers ??

You can do this :
$cookieFile = 'cookie.txt';
curl_setopt($this->curl, CURLOPT_COOKIESESSION, true);
curl_setopt($this->curl, CURLOPT_COOKIEJAR, realpath($cookieFile));
curl_setopt($this->curl, CURLOPT_COOKIEFILE, realpath($cookieFile));
Note : the cookie file must exist, you can create the file if not exist with :
if (!file_exists(realpath($cookieFile)))
{
touch($cookieFile);
}

Related

Reusing cookie in curl not working

first time here, and I need help, please!
I have two functions that do different things. The first, only do a login in an external website, and its working fine. In the second fuction, I need to do a POST with the login saved from the first. I use the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE with the same path in both functions, but when I print what I'm sending in each function with var_dump(curl_getinfo($ch)), the cookie only shows in the first one, and not in the second, causing a 401 Unauthorized Server as a result of the second function.
<?php
$cookie_path = dirname(__FILE__).'/cookie.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $login_conf['login_url']);
curl_setopt($ch, CURLOPT_REFERER, $login_conf['referer_header']);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE );
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
$html = curl_exec($ch);
?>
Thats basically the content of the login function, and this:
<?php
$cookie_path = dirname(__FILE__).'/cookie.txt';
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $purge_conf['request_url']);
curl_setopt($ch2, CURLOPT_REFERER, $refheader_submit);
curl_setopt($ch2, CURLOPT_POST, 1 );
curl_setopt($ch2, CURLOPT_POSTFIELDS, $query_purge);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_HEADER, TRUE );
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '.strlen($query_purge))
);
curl_setopt($ch2, CURLINFO_HEADER_OUT, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_path);
$html_purge = curl_exec($ch2);
?>
Is the other one, that I need to sent a json post to other page, for witch I need to keep the login and cookie.
I dont know if I am doing something wrong, or if I am missing something.

curl request to facebook redirects me to https://facebook.com/common/invalid_request.php

This is a part of the code of a function of a class that I did in early 2015. Today it is no longer working. The variables defined in my code, so that is not the problem...
Now that we're clear, let's get to the point:
The Problems:
Do not generate cookies
In the login page , I get a red sign with "
security policy " ...
I get redirected to
https://facebook.com/common/invalid_request.php when I 'm logged (
below I explain as I did for logging )
Poorly made solutions :
To generate cookies , all you did was use an extension for Chrome that shows you cookies.txt called cookies in Netscape format and copy the file with the facebook and log in because I did not allow log in with cookies without Log .
I could not fix it
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->CONFIG['cookie']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $this->CONFIG['useragent']);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.facebook.com');
curl_exec($ch) or die(curl_error($ch));
curl_close($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($email).'&pass='.urlencode($pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->CONFIG['cookie']);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->CONFIG['cookie']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $this->CONFIG['useragent']);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.facebook.com');
$page = curl_exec($ch) or die(curl_error($ch));
file_put_contents(getcwd().'/debug.html', $page);
preg_match('/<body class="([^<]+)" dir="ltr">/', $page, $web);
if(file_exists($this->CONFIG['token'])) unlink($this->CONFIG['token']);
if(preg_match('/home/i', $web[1])) {
$this->login = true;
return true;
} else {
return false;
}
curl_close($ch);
You can´t login with CURL, you need to implement a proper login process and you NEVER need the password for this: https://developers.facebook.com/docs/facebook-login
I suggest using the JavaScript SDK for this, it´s very easy to handle. Here are some links to get you started:
https://developers.facebook.com/docs/facebook-login/web
http://www.devils-heaven.com/facebook-javascript-sdk-login/

php curl file upload without real file , want to write to body a string

Hi i am uploading files by this little code
<?php
$file="oks.html";
$cookie="*;";
$csrf="*";
$parent="*";
function up($d)
{
global $cookie,$csrf,$parent;
$ch = curl_init("https://files.one.ubuntu.com/upload/");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8888");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"csrfmiddlewaretoken"=>$csrf,
"file"=>"#".$d.";filename=r2fas1.html;type=text/html",
"base"=>"/files/",
"path"=>"/",
"parent_key"=>$parent,
"redirct"=>"False",
"public"=>"on",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
return $response;
}
echo up($file);
but i want to send different file content is there a way to send string than real file ?

PHP CURL - Session expired

I'm trying to make an voip call with PHP CURL and MEGAVOIP.
The problem is i can't manage the session to access the page protected by a password.
I looked which variables are posted to the login page to post it with Curl.
But my code doesn't work.
Following Colin Morelli and Waygood's advices, I just added those lines in both commands:
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
but it's still the same:
Megavoip returns: SESSION EXPIRED
So here is my full code:
<?php
ini_set("display_errors", 1);
$username="***";
$password="***";
$url="https://www.megavoip.com/login";
$url2="https://www.megavoip.com/phone_to_phone/";
$timeout = 10;
$cookies_file = 'cookies.txt';
// HERE I GET THE TOKEN
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
preg_match_all('/<input[^>]+>/i',$content, $result);
preg_match_all('/(id|value)=("[^"]*")/i',$result[0][5], $img);
$img1=str_replace('"', '', $img[0][0]);
$img2=str_replace('"', '', $img[0][1]);
$img1=substr($img1,3);
$img2=substr($img1,6);
$postdata = "login%5Busername%5D=".$username."&login%5Bpassword%5D=".$password."&page_referrer=login&".$img1."=".$img2;
// HERE I SEND THE VARIABLES
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$content = curl_exec($ch);
// IF LOGGED HERE I'LL MAKE THE CALL
curl_close($ch);
echo $content;
exit;
?>
Any ideas to help me?
This is a test account so feel free to use my login and password if you want to have a look on this and help me!
Thank you a lot in advance.
Ok you need to forward the cookie/session after login,
you need to first extract the cookie from Header after login like following
// HERE I GET THE TOKEN
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
.... ....
.... ....
$content = curl_exec($ch);
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $content, $m);
parse_str($m[1], $cookies);
$cookie = $cookies['NAMEOFCOOKIEUNEEDHERE'];
After that need to use $cookie variable in curl options like that
// HERE I SEND THE VARIABLES
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, 'NAMEOFCOOKIEUNEEDHERE='.$cookie);
I hope your problem will be resolved.
Thanks
Moin

curl script just filling up form not submitting it

I have written a script in PHP cURL to fill form.
but strangely the form is only getting filled and there is no submission of form
here is the code
please help
$cookies = 'C:\wamp\www\costie\cookies.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url1); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 0); // times out after Ns
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_POSTFIELDS, "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=$value1&__PREVIOUSPAGE=$value2&__EVENTVALIDATION=$value3&$name1=2008&$name2=02-33-255-009&$name3=&$name4=&$name5=");//ADD post fields
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
$result = curl_exec($ch); // run the whole process
curl_close($ch);
echo $result;

Categories