我做了一个简单的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);
}
}
感谢您的帮助。
答案 0 :(得分:1)
您需要确保您的cookie文件夹位于public_html
文件夹之外,并且运行PHP的用户有权写入该文件夹。
将文件夹权限设置为755
,这就足够了。任何额外的权限都是安全漏洞,可以让其他人写入共享主机上的文件夹..
示例路径......
/home/my_awesome_website/cookies