duanjia7607 2009-11-28 20:09
浏览 119
已采纳

无法在php curl中启用cookie

Here is my code to use curl:

class cURL {
    var $headers;
    var $user_agent;
    var $compression;
    var $cookie_file;
    var $proxy;
    var $process;

    function cURL($cookies = TRUE, $cookie = 'cookies.txt', $compression = 'gzip', $proxy = '') {
        $this->headers [] = 'Connection: Keep-Alive';
        $this->headers [] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
        $this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';
        $this->compression = $compression;
        $this->proxy = $proxy;      

        $this->cookies = $cookies;
        if ($this->cookies == TRUE)
            $this->cookie ( "c:/wamp/www/googlegroups/cookies/".$cookie );

        $this->process = curl_init ();
        curl_setopt ( $this->process, CURLOPT_HTTPHEADER, $this->headers );
        curl_setopt ( $this->process, CURLOPT_USERAGENT, $this->user_agent );

        if ($this->cookies == TRUE)
            curl_setopt ( $this->process, CURLOPT_COOKIEFILE, $this->cookie_file );
        if ($this->cookies == TRUE)
            curl_setopt ( $this->process, CURLOPT_COOKIEJAR, $this->cookie_file );

        curl_setopt ( $this->process, CURLOPT_ENCODING, $this->compression );
        curl_setopt ( $this->process, CURLOPT_TIMEOUT, 30 );
        if ($this->proxy)
            curl_setopt ( $this->process, CURLOPT_PROXY, $this->proxy );
        curl_setopt ( $this->process, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $this->process, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $this->process, CURLOPT_SSL_VERIFYHOST, 0 );
        curl_setopt ( $this->process, CURLOPT_SSL_VERIFYPEER, 0 );
    }

    function __destruct(){
        curl_close ( $this->process );
        unlink($this->cookie_file);
    }

    function cookie($cookie_file) {
        if (file_exists ( $cookie_file )) {
            $this->cookie_file = $cookie_file;
        } else {
            if($fp = fopen ( $cookie_file, 'w' ))
            {
                fclose($fp);
            }
            else $this->error ( 'The cookie file could not be opened. Make sure this directory has the correct permissions' );
            $this->cookie_file = $cookie_file;
        }
    }
    function get($url) {
        curl_setopt ( $this->process, CURLOPT_POST, 0);
        curl_setopt ( $this->process, CURLOPT_URL, $url);
        $return = curl_exec ( $this->process );
        return $return;
    }

 }

$cc = new cURL();
echo $cc->get("http://127.0.0.1/googlegroups/testcookie.php");

This is a very strange type of curl's malfunctioning that I've never experienced before. and this is the output of the testcookie.php :

Cookies are NOT enabled on your browser

Here is the funny thing if you remove unlink($this->cookie_file); from the destruction function the cookie file will not get deleted and next time you run this script you will get

Cookies are enabled on your browser

testcookie.php source code:

setcookie("test","test");
if (isset ($_COOKIE['test']))
{
    echo "Cookies are enabled on your browser";
}
else
{
    echo "Cookies are <b>NOT</b> enabled on your browser";
}
  • 写回答

1条回答 默认 最新

  • donglu5000 2009-11-28 20:22
    关注

    I don't see where is the problem. The CURL is doing exactly what it supposes to do.

    setcookie() sets cookie in response header to the client. _COOKIE contains value in request header. If you remove cookie file, CURL has no way to persist the cookie value you set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案