1
function loginpage($loginurl, $loginset, $agent, $cookie){

    $options = array( 
        CURLOPT_SSL_VERIFYPEER    =>    true, 
        CURLOPT_SSL_VERIFYHOST    =>    0, 
        CURLOPT_SSL_VERIFYPEER    =>    0, 
        CURLOPT_COOKIESESSION    =>    true, 
        CURLOPT_CONNECTTIMEOUT     =>     120, 
        CURLOPT_USERAGENT          =>     $agent, 
        CURLOPT_TIMEOUT            =>    120, 
        CURLOPT_FOLLOWLOCATION    =>    true, 
        CURLOPT_RETURNTRANSFER    =>    true, 
        //CURLOPT_REFERER            =>    1, 
        CURLOPT_POST            =>    1, 
        CURLOPT_HEADER            =>    0, 
        CURLOPT_POSTFIELDS        =>    $loginset, 
        CURLOPT_POST            =>    1, 
        CURLOPT_COOKIEJAR        =>    $cookie, 
        //CURLOPT_COOKIE            =>    $strCookie, 
        //CURLOPT_COOKIEFILE        =>    $cookie, 

    ); 
    $ch        = curl_init( $loginurl ); 
    CURL_SETOPT_ARRAY( $ch, $options );  
    $result     = curl_exec( $ch );  
    $err     = curl_errno( $ch );  
    $errmsg  = curl_error( $ch );  
    $header  = curl_getinfo( $ch );  
    curl_close( $ch ); 
    return $result;  
} 

$username    = "myusername..."; 
$password    = "mypassword..."; 
$loginurl    = "http://up.4share.vn/?control=login"; 
$loginset    = "inputUserName=$username&inputPassword=$password"; 
$cookie        = "/uploads/4shareVN_cookie.txt" ; 
$agent        = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"; 

$loginpage = loginpage($loginurl, $loginset, $agent, $cookie);  
echo $loginpage;

使用此代码,我可以使用我的帐户 cURL 并登录到:http ://up.4share.vn/?control=login

我的问题是如何保持登录并转到该网站的另一个页面

例如:http ://up.4share.vn/?control=account_info

并在php中显示它,请有人帮忙。

4

2 回答 2

1

您可以通过 cookie jar 启用自动 cookie 管理以处理 cUrl 中的会话:

curl_setopt($ch, CURLOPT_COOKIEJAR, '/where/you/want/to/store/cookie.txt');

确保目录可写

于 2013-09-04T12:39:00.663 回答
0

使用 PHP cURL 时,cookie 有两个重要的选项:

CURLOPT_COOKIEJAR

当句柄关闭时保存所有内部 cookie 的文件的名称,例如在调用 curl_close 之后。

所以用它来保存服务器响应设置的cookie

CURLOPT_COOKIEFILE

包含 cookie 数据的文件的名称。cookie 文件可以是 Netscape 格式,或者只是转储到文件中的普通 HTTP 样式的标头。如果名称为空字符串,则不加载任何 cookie,但仍启用 cookie 处理。

所以用它来加载你保存的cookies。

http://up.4share.vn/?control=manager在您的示例中,当我在成功登录后尝试使用 URL 时,此代码适用于我。

function loginpage($loginurl, $loginset, $agent, $cookie){

    $options = array( 
        CURLOPT_SSL_VERIFYPEER    =>    true, 
        CURLOPT_SSL_VERIFYHOST    =>    0, 
        CURLOPT_SSL_VERIFYPEER    =>    0, 
        CURLOPT_COOKIESESSION    =>    true, 
        CURLOPT_CONNECTTIMEOUT     =>     120, 
        CURLOPT_USERAGENT          =>     $agent, 
        CURLOPT_TIMEOUT            =>    120, 
        CURLOPT_FOLLOWLOCATION    =>    true, 
        CURLOPT_RETURNTRANSFER    =>    true, 
        //CURLOPT_REFERER            =>    1, 
        CURLOPT_POST            =>    1, 
        CURLOPT_HEADER            =>    0, 
        CURLOPT_POSTFIELDS        =>    $loginset, 
        CURLOPT_POST            =>    1, 
        CURLOPT_COOKIEJAR        =>    $cookie, 
        //CURLOPT_COOKIE            =>    $strCookie, 
        CURLOPT_COOKIEFILE        =>    $cookie, 

    ); 
    $ch        = curl_init( $loginurl ); 
    CURL_SETOPT_ARRAY( $ch, $options );  
    $result     = curl_exec( $ch );  
    $err     = curl_errno( $ch );  
    $errmsg  = curl_error( $ch );  
    $header  = curl_getinfo( $ch );  
    curl_close( $ch ); 
    return $result;  
} 

$username    = "zerog31"; 
$password    = "vnpy0966"; 
$loginurl    = "http://up.4share.vn/?control=login"; 
#$loginurl  = "http://up.4share.vn/?control=manager";

$loginset    = "inputUserName=$username&inputPassword=$password&rememberlogin=on"; 
#$loginset = "";
$cookie        = dirname(__FILE__) . "/cookiefile.txt" ; 
$agent        = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"; 

$loginpage = loginpage($loginurl, $loginset, $agent, $cookie);  
echo $loginpage;

登录成功后,更改$loginurl$loginset查看管理网页。如果您没有使用正确的 cookie,您将看到登录页面。

于 2013-09-04T13:13:07.267 回答