简体   繁体   中英

How to keep session informations using curl in 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM