1

我有一个登录网站的代码。它可以在 wordpress 之类的网站上运行,但在我想登录的网站上什么也没有发生。哪里没有错误信息或任何东西。这是我的代码:

<?php
print" <head>
        <base href='http://www.example.com/'/>
    </head>";

$url = 'http://www.example.com/login.php?';                        
$postdata = 'username=dasds&password=frkm'; 
$referer = $url; 
$cookie = "cookie.txt" ;
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
curl_setopt($ch,CURLOPT_COOKIESESSION,false); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch);
print $result;
curl_close($ch);
?>

和他们的html表单:

<form name="form" method="post" action="/login.php">
<label class="registration" for="username">
<span>Username</span>
<input name="username" type="text" id="username" size="30"/>
</label>
<label class="registration" for="password">
<span>Password</span>
<input name="password" type="password" id="password"/>
</label>
<input type="submit" name="Login" value="Login"/>
</form>
4

1 回答 1

0

试试这个

  • 删除尾随 ? 在网址中。
  • 同时指定COOKIEJARandCOOKIEFILE并设置 cookie 文件的绝对路径:

代码

curl_setopt ($ch, CURLOPT_COOKIEJAR, __DIR__.'/'.$cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, __DIR__.'/'.$cookie);

和:

  • 在尝试登录之前加载主页一次以获取他们可能给您的任何 cookie。
  • 确保不涉及验证码。在浏览器中跟踪 POST 过程 (FireFox + HttpFox)。

如果您在浏览器中跟踪登录请求 + 响应,则不会失败。

于 2012-10-28T20:24:08.007 回答