1
votes

I build a simple PHP script to login into a webpage and receive some data on a second page. For now I used one php file with two separate curl connection using the same cookie. The first curl connection did the login, the second received the data.

Now I want to split these two parts in two separate php files to call each independent via Ajax.

The Login is is successful but retrieving the data doesn't work, it seems due to missing cookie.

I am not quite sure if it is possible to make it work this way, and what's the best practice to realize what I am tryin, maybe someone can brighten my thoughts.

        // SET CONSTANTS
        define('COOKIE_NAME', 'c.txt');
        define('url_login', 'https://website.com/login.cgi');
        $postString = 'user='.$user.'&passwd='.$passwd.'&otp='.$otp;

        // CREATE CURL CONNECTION FOR LOGIN
        $curl_connection = curl_init(url_login);

        curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($curl_connection, CURLOPT_USERAGENT, 
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

        curl_setopt($curl_connection, CURLOPT_HEADER, false);
        curl_setopt($curl_connection, CURLOPT_COOKIEJAR, COOKIE_NAME);
        curl_setopt($curl_connection, CURLOPT_COOKIEFILE, COOKIE_NAME);

        curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $postString);
        $loginpage = curl_exec($curl_connection);



        // CREATE CURL CONNECTION TO GET DATA

        $data_url = 'https://website.com/getData.cgi';

        $curl_connection = curl_init($data_url);

        curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($curl_connection, CURLOPT_USERAGENT, 
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

        curl_setopt($curl_connection, CURLOPT_HEADER, false);
        curl_setopt($curl_connection, CURLOPT_COOKIEJAR, COOKIE_NAME);
        curl_setopt($curl_connection, CURLOPT_COOKIEFILE, COOKIE_NAME);


        $data = curl_exec($curl_connection);

Thats how it is working...

As soon as I split the curl connections into to separate php files the second part doesn't work giving this output:

HTTP/1.1 200 OK Server: nginx Date: Fri, 20 Mar 2020 12:49:12 GMT Content-Type: text/html Content-Length: 85 Connection: close Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Expires: 0 Set-Cookie: ws-sid-10-147-g1e32=deleted; path=/; max-age=0 Strict-Transport-Security: max-age=63072000; includeSubDomains; preload X-Frame-Options: DENY X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block no cookie found

1
Please add your code and explain what you've triedCristiano Casciotti
Also, please explain how your code "doesn't work". Are you getting a specific error message? Does the code quit without splitting the file? Does it merge the file instead?Robert Columbia
Edited the post, hope its clear nowConstantin

1 Answers

0
votes

I think I found a solution, at least it's working.

In the first cURL connection I read out the Cookie from the Header and in the Second I added the Cookie manually with the CURLOPT_COOKIE option:

First PHP file and cUrl connection:

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, 
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($curl_connection, CURLOPT_HEADER, true);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, COOKIE_NAME);
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, COOKIE_NAME);

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $postString);
$page = curl_exec($curl_connection);

preg_match("/Set-Cookie: (.{18,20})\=(.{32});/", $page, $cookie);

$cookie = $cookie[2];

In the second file and cURL connection:

$curl_connection = curl_init($url);

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, 
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($curl_connection, CURLOPT_HEADER, true);

curl_setopt($curl_connection, CURLOPT_COOKIE, $cookie);