代码之家  ›  专栏  ›  技术社区  ›  Nick Woodhams

php curl登录论坛,但不会保持登录?

php
  •  0
  • Nick Woodhams  · 技术社区  · 15 年前

    我正试图从一个私人论坛中提取一些数据。我创建了一个使用curl登录的PHP脚本,以及使用domdocument提取页面数据。

    我已经使用脚本成功登录,但它的作用就像我尝试使用loadhtmlfile()加载网页时从未登录过一样。

    有人告诉我可能需要发送cookie头?但我不知道该怎么做,甚至不知道是否有必要。

    有人有什么想法吗?

        <?
    function vBulletinLogin($user, $pass)
    {
           $md5Pass = md5($pass);
           $data = "do=login&url=index.php&vb_login_md5password=$md5Pass&vb_login_username=$user&cookieuser=1";
           $ch = curl_init();
    
            curl_setopt ($ch, CURLOPT_URL, "****"); // replace ** with tt
            curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
            curl_setopt ($ch, CURLOPT_TIMEOUT, '10');
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
            curl_setopt($ch, CURLOPT_COOKIEJAR, "/public_html/phpcrawl/cookies.txt");
            curl_setopt($ch, CURLOPT_COOKIEFILE, "/public_html/phpcrawl/cookies.txt");
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_REFERRER, "****"); 
            $store = curl_exec ($ch);
            echo $store; <- **this shows that I have successully logged in, it gives me a welcome message**
            print_r($_COOKIE);
    
            curl_close($ch);
    
           $pos = strpos($store, "Thank you for logging in");
           if($pos === FALSE) RETURN 0;
           else RETURN 1;
    
    }
    if(vBulletinLogin("****","****")) echo "Logged In";
    else echo "Failed to Login check User / Pass";
    
    $url="http://texturl.com";
    echo $url."<br>";
    
    //get new HTML document
    $html = new DOMDocument(); 
    
    $html->loadHTMLFile($url);
    print $html->saveHTML(); <- shows a login and password box saying I am not logged in. 
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   superfro    15 年前

    推荐文章