使用PHP和CURL登录网站


Login to a site using PHP and CURL

对我来说,唯一的问题是它没有用于表单提交的html按钮——它使用的是没有name属性的css样式的自定义按钮。这是表格

<form name="login_form" method="post" action="index.php" onsubmit="loginFormSubmit(this)">
        <div style="position:absolute;top:-1500px;"><input type="submit" value="sub"></div>
        Username:<input name="username" size="20" maxlength="32"><br><br>
        Password:<input type="password" name="passwd" size="20" maxlength="32">
    </form>

按钮的HTML代码,如果在标签之外

<div style="position: absolute; width: 54px; height: 20px; top: 382px; margin-left: 312px; cursor: pointer; color: rgb(255, 255, 255); font-style: normal; font-weight: normal;" onmouseover="fFOB(this)" onmouseout="fFNN(this)" onclick="loginFormSubmit(document.login_form)">Login</div>

这是我的PHP代码

    <?php
$username="yyyyy"; 
$password="xxxxx"; 
$url="http://example.com/login.php"; 
$cookie="cookie.txt"; 
$postdata = "username=".$username."&passwd=".$password; 
$postdata = "username=$username&passwd=$password";
$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_COOKIEJAR, $cookie); 
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);
?>

更改类型,如

 method = 'get'

在表单标签和php代码中使用

GET['username'],GET['password']