CUrl cookie存储在本地而不是远程


CUrl cookies stored in local but not on remote

我做了一个简单的PHP browser来导航远程网站。当我在本地服务器上运行脚本时,它可以完美地工作(它存储cookie,我可以稍后使用sid检索会话)。但是当我将文件推送到远程服务器时,它不会保存cookie。它看起来每次都创建一个新的会话。

是我做错了什么还是我必须在我的远程服务器上启用一个特殊的选项?

下面是我的代码:
<?php
class Browser
{
  private $session_id = '';
  private $user_agent = '';
  private $curl = null;
  function __construct($session_id, $user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2')
  {
    $this->session_id = $session_id;
    $this->user_agent = $user_agent;
    // Create a new instance of Curl
    $this->curl = curl_init();
    if (empty($this->curl)) throw new Exception('CURL doesn''t look to be available');
  }
  function __destruct()
  {
    // Close session
    curl_close($this->curl);
    unset($this->curl);
  }
  private function request($url, $reset_cookies, $post_data)
  {    
    $options = array(
      CURLOPT_URL               => $url,
      CURLOPT_RETURNTRANSFER    => 1,
      CURLOPT_HEADER            => 0,
      CURLOPT_FAILONERROR       => 1,
      CURLOPT_USERAGENT         => $this->user_agent,
      CURLOPT_CONNECTTIMEOUT    => 30,
      CURLOPT_TIMEOUT           => 30,
      CURLOPT_SSL_VERIFYPEER    => 0,   
      CURLOPT_FOLLOWLOCATION    => 1,
      CURLOPT_MAXREDIRS         => 10,
      CURLOPT_AUTOREFERER       => 1,
      CURLOPT_COOKIESESSION     => $reset_cookies ? 1 : 0,
      CURLOPT_COOKIEJAR         => $this->session_id,
      CURLOPT_COOKIEFILE        => $this->session_id,
    );
    // Add POST data
    if (isset($post_data))
    {
      $options[CURLOPT_POST]          = 1;
      $options[CURLOPT_POSTFIELDS]    = http_build_query($post_data);
    }
    // Attach options
    curl_setopt_array($this->curl, $options);
    // Execute the request and read the response
    $content = curl_exec($this->curl);
    // Handle any error
    if (curl_errno($this->curl)) throw new Exception(curl_error($this->curl));
    return $content;
  }
  public function get($url, $reset_cookies = false)
  {
    return $this->request($url, $reset_cookies, null);
  }
  public function post($url, $post_data, $reset_cookies = false)
  {
    return $this->request($url, $reset_cookies, $post_data);
  }
}

谢谢你的帮助。

您需要确保您的cookies文件夹在public_html文件夹之外,并且运行PHP的用户有权限写入该文件夹

将文件夹权限设置为755,这就足够了。任何额外的权限都是一个安全漏洞,可以让其他人写入共享主机上的文件夹。

示例路径…

/home/my_awesome_website/cookies