PHPcurl认证问题

发布于 2024-12-07 05:38:23 字数 3938 浏览 0 评论 0原文

我有自己的网站,仅在会话中存储登录信息。 我需要使用 CURL 提交表单,但技巧是您需要先登录才能访问带有表单的页面。所以我尝试创建两个请求,但它似乎没有按预期工作,即使我在登录页面上登录,表单仍然显示我已注销。

//add data to be posted
$post_string1 = "username=test&password=1234";
$post_string2 = "value1=555";

//create cURL connection
$curl_connection1 = curl_init("http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/login.php");
$curl_connection2 = curl_init("http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/formpage.php");
 //create the multiple cURL handle
//set options
curl_setopt($curl_connection1, CURLOPT_POST, 1);
curl_setopt($curl_connection1, CURLOPT_POSTFIELDS, $post_string1);
curl_setopt ($curl_connection1, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl_connection1, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_connection1, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection1, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($curl_connection2, CURLOPT_POST, 1);
curl_setopt($curl_connection2, CURLOPT_POSTFIELDS, $post_string2);
curl_setopt ($curl_connection2, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl_connection2, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_connection2, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection2, CURLOPT_FOLLOWLOCATION, 1);


$mh = curl_multi_init();
curl_multi_add_handle($mh,$curl_connection1);
curl_multi_add_handle($mh,$curl_connection2); 

$active = null;

do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

//show information regarding the request
print_r(curl_getinfo($curl_connection1));
echo curl_errno($curl_connection1) . '-' . curl_error($curl_connection1);
//show information regarding the request
print_r(curl_getinfo($curl_connection2));
echo curl_errno($curl_connection2) . '-' . curl_error($curl_connection2);

Cookie.txt 是可写的并且包含会话,以下是响应:

Array
(
    [url] => http://mysite.com/login.php
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 762
    [request_size] => 404
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 0.270866
    [namelookup_time] => 2.9E-05
    [connect_time] => 0.000118
    [pretransfer_time] => 0.000193
    [size_upload] => 0
    [size_download] => 132955
    [speed_download] => 490851
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 0.250872
    [redirect_time] => 0.01623
)
0-Array
(
    [url] => http://mysite.com/formpage.php?login=destroyed
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 786
    [request_size] => 415
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 0.056873
    [namelookup_time] => 2.6E-05
    [connect_time] => 9.9E-05
    [pretransfer_time] => 0.000198
    [size_upload] => 0
    [size_download] => 9790
    [speed_download] => 172137
    [speed_upload] => 0
    [download_content_length] => 1
    [upload_content_length] => 0
    [starttransfer_time] => 0.013818
    [redirect_time] => 0.042138

I have my own site that store login info only in session.
I need to use CURL to submit form, but trick is you need to login first before you can access page with form. So i tried to create two requests but it dont seem to work as intended even though i do get login on login page page with form still shows i am logged out.

//add data to be posted
$post_string1 = "username=test&password=1234";
$post_string2 = "value1=555";

//create cURL connection
$curl_connection1 = curl_init("http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/login.php");
$curl_connection2 = curl_init("http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/formpage.php");
 //create the multiple cURL handle
//set options
curl_setopt($curl_connection1, CURLOPT_POST, 1);
curl_setopt($curl_connection1, CURLOPT_POSTFIELDS, $post_string1);
curl_setopt ($curl_connection1, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl_connection1, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_connection1, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection1, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($curl_connection2, CURLOPT_POST, 1);
curl_setopt($curl_connection2, CURLOPT_POSTFIELDS, $post_string2);
curl_setopt ($curl_connection2, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl_connection2, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_connection2, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection2, CURLOPT_FOLLOWLOCATION, 1);


$mh = curl_multi_init();
curl_multi_add_handle($mh,$curl_connection1);
curl_multi_add_handle($mh,$curl_connection2); 

$active = null;

do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

//show information regarding the request
print_r(curl_getinfo($curl_connection1));
echo curl_errno($curl_connection1) . '-' . curl_error($curl_connection1);
//show information regarding the request
print_r(curl_getinfo($curl_connection2));
echo curl_errno($curl_connection2) . '-' . curl_error($curl_connection2);

Cookie.txt is writable and contains session, here's the response:

Array
(
    [url] => http://mysite.com/login.php
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 762
    [request_size] => 404
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 0.270866
    [namelookup_time] => 2.9E-05
    [connect_time] => 0.000118
    [pretransfer_time] => 0.000193
    [size_upload] => 0
    [size_download] => 132955
    [speed_download] => 490851
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 0.250872
    [redirect_time] => 0.01623
)
0-Array
(
    [url] => http://mysite.com/formpage.php?login=destroyed
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 786
    [request_size] => 415
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 0.056873
    [namelookup_time] => 2.6E-05
    [connect_time] => 9.9E-05
    [pretransfer_time] => 0.000198
    [size_upload] => 0
    [size_download] => 9790
    [speed_download] => 172137
    [speed_upload] => 0
    [download_content_length] => 1
    [upload_content_length] => 0
    [starttransfer_time] => 0.013818
    [redirect_time] => 0.042138

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2024-12-14 05:38:23

为此,请勿使用两个平行的卷曲手柄。根据网络条件,您的第二个curl请求实际上可能在登录请求之前首先到达服务器。

同样,CURL 在执行请求时读取 cookie 文件。在从服务器返回任何登录 cookie 之前,您的两个请求将基本上同时触发。所以这两个请求都会处于“未登录”状态。

使用单个卷曲句柄,并按顺序执行每个调用>

$ch = new CURL();
... set up login ...
... do login ...
... set options for second post
... do second post ...

这样的顺序保证登录请求(和 cookie)在您开始第二个请求之前完成并可用。

Don't use two parallel curl handles for this. Depending on network conditions, your SECOND curl request could actually reach the server first, BEFORE the login request.

As well, CURL reads the cookie files at the time the request is executed. Both of your requests will fire off at basically the same time, BEFORE any login cookies are returned from the server. So both requests will be in a "not logged in" state.

Use a SINGLE curl handle, and do each invocation in sequence>

$ch = new CURL();
... set up login ...
... do login ...
... set options for second post
... do second post ...

Such a sequence guarantees that the login request (and cookie) will be completed and available before you start on the second request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文