Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP CURL创建和使用唯一命名的cookie文件_Php_Curl - Fatal编程技术网

PHP CURL创建和使用唯一命名的cookie文件

PHP CURL创建和使用唯一命名的cookie文件,php,curl,Php,Curl,我有一个php脚本,正在登录到一个远程站点,它运行良好。但是,它似乎只在指定的cookie文件名为cookie.txt 我想要的是,每个使用此脚本的人都有一个单独的cookie文件。因此,如果machineA上的人在19日使用它,他们的cookie将位于类似于/tmp/19/someNameCookie.txt的位置,machineB将具有/tmp/19/someOtherNamedCookie.txt,如果machineA在20日使用它,他们将具有/tmp/20/someNamedCookie

我有一个php脚本,正在登录到一个远程站点,它运行良好。但是,它似乎只在指定的cookie文件名为
cookie.txt

我想要的是,每个使用此脚本的人都有一个单独的cookie文件。因此,如果machineA上的人在19日使用它,他们的cookie将位于类似于
/tmp/19/someNameCookie.txt的位置,machineB将具有
/tmp/19/someOtherNamedCookie.txt
,如果machineA在20日使用它,他们将具有
/tmp/20/someNamedCookie.txt

有许多方法可以生成唯一命名的文件,但这里的关键是让它们正常工作。我对php的了解还很年轻,对curl完全陌生

另一方面,我也没有成功地使用
CURLOPT_FOLLOWLOCATION
获取登录后的新页面

谢谢


这是一个密码。这是从另一个渠道得到的

$username=urlencode($usr); 
$password=urlencode($pass); 
$url="http://domain.com/"; 
$cookie="cookie.txt"; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // have tried with just the jar, and just the file and both 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch);

echo $result;  
curl_close($ch);
这是我将调用的代码,用于检查登录是否成功

$ch2 = curl_init();
//curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch2, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch2, CURLOPT_URL, "http://domain.com/");

$r = curl_exec($ch2);
echo $r;
curl_close($ch2);

据我所知,如果cookie文件不存在,curl应该创建cookie文件。我也尝试过使用fopen手动或通过脚本创建其他文件,但仅当我使用cookie.txt时,这些文件仍然有效,而且现在它们都被保存到公共html中以供测试。

使用例如session\u id()作为cookie文件名,它对于连接到应用程序的每台计算机都是唯一的,直到过期,它仍然是一样的

设置
饼干罐

<?php
    $machine = 'PC_A'; // here it's your problem how you define each machine
    $cookie_file = "/tmp/".date('d')."/".$machine."_cookie.txt";
    // and set the cURL cookie jar and file

    if (!file_exists($cookie_file) || !is_writable($cookie_file)){
            echo 'Cookie file missing or not writable.';
            die;
    }

    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
?>

我们能看一些代码吗?特别是当它保存到
cookie.txt
阅读此--->我正在做类似的事情,但仍然没有保存cookie,也没有登录。甚至尝试确保tmp文件夹存在于根目录中,并且尝试了绝对目录。您确定您的脚本具有写入
/tmp
的权限吗?否:P我正在使用cyberduck连接到我的服务器,它提供的权限是744。我需要从我的网络主机设置权限吗?好的,它会显示您创建的错误消息。那么这是否意味着它没有适当的权限呢?请检查是否可写。。。我发布的代码,如果不是。。是的。。创建一个具有权限的文件夹,并将cookie放在那里。