gpt4 book ai didi

php - 使用 PHP 和 Curl 登录我的网站表单

转载 作者:IT王子 更新时间:2023-10-29 00:07:43 26 4
gpt4 key购买 nike

我试图在我用来下载文件的网站上登录我的用户帐户,这样我就可以自动获取文件而无需访问该网站。

这是表格:

 <form method='post' action='/news.php'>
<div>
Username: <input class='tbox' type='text' name='username' size='15' value='' maxlength='20' />&nbsp;&nbsp;
Password: <input class='tbox' type='password' name='userpass' size='15' value='' maxlength='20' />&nbsp;&nbsp;
<input type='hidden' name='autologin' value='1' />
<input class='button' type='submit' name='userlogin' value='Login' />
</div>
</form>

这是我目前为止得到的 PHP。

<?php
$username="my_user";
$password="my_passs";
$url="the_url";
$cookie="cookie.txt";

$postdata = "username=".$username."&userpass=".$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);
?>

我做错了什么吗?它目前只显示网站,但不会让我登录。我以前从未使用过 Curl。

谢谢

最佳答案

您可能需要设置 COOKIESESSION 和 COOKIEJAR 选项来保留 session 并执行另一个请求:

//initial request with login data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}

//another request preserving the session

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}

关于php - 使用 PHP 和 Curl 登录我的网站表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20049393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com