Cannot use cookies in PHP cURL

I use cURL to analyze the website.

http://www.hcmiu.edu.vn/bookforsale/showbooks.php

Viewing requires a session if you do not have a session and then redirects the page to:

http://www.hcmiu.edu.vn/bookforsale/perInfo.php

I use this code to get a session cookie, but I don’t know why I don’t see any changes in the cookie.txt

$urltopost = "http://www.hcmiu.edu.vn/bookforsale/perInfo.php"; $datatopost = array ( "name" => "abc", "tel" => "99999999", "email" => " abc@gmail.com ", "profession" => "abc", "employer" => "abc", "tel_work" => "99999999", ); $ch = curl_init($urltopost); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $datatopost); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookies.txt"); $returnData = curl_exec($ch); $file = fopen("example.txt", "w"); fwrite($file, $returnData); fclose($file); curl_close($ch); 

However, I see:

 Set-Cookie: PHPSESSID=1egb993objkrdp3gi5mpvs02g0; path=/ 

in the title. Thanks for any help

-Edit: I use the tricky way: I use the http viewer to view the PHPSESSID in the browser. And then I use it to create a cookie to read cURL. Then I can go through a web server session check to view the showbooks.php file.

+3
source share
3 answers

I used this piece of code to extract the cookie from the server response:

 $returnData = curl_exec($ch); $cookie = ''; $pattern = '/Set-Cookie:(.*?)\n/'; if (preg_match($pattern, $returnData, $result)) $cookie = $result[1]; 

I described my experience in this post . This approach does not use a cookie.


Regarding the cookie , consider the following http://php.net/manual/en/book.curl.php tip:

Use the absolute path to set the variables CURLOPT_COOKIEFILE & CURLOPT_COOKIEJAR. To make life easier, use the realpath ("file.txt") function to get the absolute path.

+3
source

Is the file writable? does it even exist?

+2
source

Try creating a file and assigning it a global write privilege.

Also try with a relative path (e.g.. / Cookies.txt) instead of the absolute path for the cookie jar.

+2
source

Source: https://habr.com/ru/post/955667/


All Articles