I have some problem with PHP Curl and Cookies Authentication.
I have a file Connector.php which authenticates users on another server and returns cookie with current user.
The Problem is that I want to authenticate thousands of users with curl but it authenticates and saves COOKIES only for one user at a time.
the code for connector.php is this:
<?php
if(!count($_REQUEST)) {
die("No Access!");
}
//Core Url For Services
define ('ServiceCore', 'http://example.com/core/');
//Which Internal Service Should Be Called
$path = $_GET['service'];
//Service To Be Queried
$url = ServiceCore.$path;
//Open the Curl session
$session = curl_init($url);
// If it's a GET, put the GET data in the body
if ($_GET['service']) {
//Iterate Over GET Vars
$postvars = '';
foreach($_GET as $key=>$val) {
if($key!='service') {
$postvars.="$key=$val&";
}
}
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
}
//Create And Save Cookies
$tmpfname = dirname(__FILE__).'/cookie.txt';
curl_setopt($session, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($session, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
// EXECUTE
$json = curl_exec($session);
echo $json;
curl_close($session);
?>
Here is the process of authentication:
- User enters username and password: Connector.php?service=logon&user_name=user32&user_pass=123
- Connector.php?service=logosessionInfo this returns info about user based on cookies saved earlier with logon service.
The problem is that this code saves Cookie in one file for one user and cant handle multiple user authentication.
In working with a similar problem I created the following function after combining a lot of resources I ran into on the web, and adding my own cookie handling. Hopefully this is useful to someone else.
Here you can find some useful info about cURL & cookies http://docstore.mik.ua/orelly/webprog/pcook/ch11_04.htm .
You can also use this well done method https://github.com/alixaxel/phunction/blob/master/phunction/Net.php#L89 like a function:
And pass this as
$cookie
parameter:You can specify the cookie file with a curl opt. You could use a unique file for each user.
The best way to handle it would be to stick your request logic into a curl function and just pass the unique file name in as a parameter.
I use this for quick grabs. It takes the url and an array of options.
First create temporary cookie using tempnam() function:
Than execute curl init witch cookie saves as a temporary file:
Or visit cookie data where is store of temporary data:
This will loads page of cookie initialization:
You can define different cookies for every user with
CURLOPT_COOKIEFILE
andCURLOPT_COOKIEJAR
. Make different file for every user so each one would have it's own cookie-based session on remote server.Solutions which are described above, even with unique CookieFile names, can cause a lot of problems on scale.
We had to serve a lot of authentications with this solution and our server went down because of high file read write actions.
The solution for this was to use Apache Reverse Proxy and omit CURL requests at all.
Details how to use Proxy on Apache can be found here: https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html