N
N
Netscout2015-10-27 14:03:07
PHP
Netscout, 2015-10-27 14:03:07

How CURL works in PHP - how to send and receive cookies correctly?

Hello uv. forum users!
I've come across a problem that I can't solve yet and I hope for your tips.
I'll start over.
Wrote a script:

<?php
            
      $url_first='http://www.sitename.ru/1.html';
      $url_second = 'http://www.sitename.ru/2.html';
      
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, $url_first); // 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, 3); // times out after 4s 
      curl_setopt($ch, CURLOPT_POST, 1); // set POST method 
      curl_setopt($ch, CURLOPT_COOKIEJAR, '1.txt');     // сохранять информацию Cookie в файл
        
        
      $result = curl_exec($ch); // run the whole process 
      
      //echo $result; 
    
        
      curl_setopt ($ch, CURLOPT_REFERER, $url_first); // Напоминаем серверу откуда пришли
      curl_setopt ($ch, CURLOPT_URL, $url_second);    // set url to post to 
      curl_setopt ($ch, CURLOPT_COOKIEFILE, '1.txt'); //Отправляем серверу куки
      
      $result = curl_exec($ch); // run the whole process 
      
      echo $result; 
      
    
      curl_close($ch);  

?>

This script first visits the page " www.sitename.ru/1.html " and receives the cookie provided by the server, then it saves this cookie to the file "1.txt".
To safely get to the next page " www.sitename.ru/2.html " the script gives the cookie back to the server, and the server, realizing that everything is OK, should let it go to the requested page.
---------------------------------
So, the contents of file 1.txt are:

# Netscape HTTP Cookie File
# curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
.sitename.com TRUE / FALSE 0 PHPSESSID plpidr5mv0bbltphrp015s7214

Everything would be fine, but if I go to the site with a browser (for example, Opera), then looking at the cookies received from the server, I see that there are 10 more variables that are not in the 1.txt file.
Here are some of what Opera receives, but does not receive CURL:

Name: __auc
Content: ff023206150a8c346db68bc768b
Domain: .sitename.ru
Path: /
Send to: Any connections
Available for script: Yes
Created: Tuesday, October 27, 2015 12:06:21 PM
Expires: Thursday, October 27, 2016, 13:06:21
----------------------
Name: __asc
Content: ff023206150a8c346db68bc768b
Domain: .sitename.ru
Path: /
Send to: Any connections
Available to script: Yes
Created: Tue, Oct 27, 2015 12:06:21 PM
Expires: Tue, Oct 27, 2015 12:36:21 PM
etc.


Why aren't all these variables in the 1.txt file?
Can the server not allow other pages of the site due to the absence of these variables (after all, the Opera browser allows) and if so, how to overcome it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2015-10-27
@dimonchik2013

www.telerik.com/fiddler set, take a closer look
cookies can be set and JS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question