0

我目前正在编写用户必须填写用户名和密码的脚本。然后,当用户登录时,脚本会检查他/她的用户名是否已在我的数据库中注册。如果是这种情况,用户登录我的外部网站(使用 cURL),如果没有,用户登录我无权访问数据库的其他网站。

if($count==1){
    $curl = curl_init('http://www.myownwebsite.com/');
    curl_setopt ($curl, CURLOPT_POSTFIELDS, "gebruikersnaam=$myusername&wachtwoord=$mypassword");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($curl);
}
else {
    $curl = curl_init('http://www.differentwebsite.com/');
    curl_setopt ($curl, CURLOPT_POSTFIELDS, "username=$myusername&password=$mypassword");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($curl);
}

如您所见,我的脚本将行数存储在一个计数变量中,如果查询结果为 1 行,它会登录我的站点,如果不是,它会登录另一个站点。用户名和密码检查是在用户登录的实际网站上完成的。

现在我的问题是,我希望它“跟随位置”,或者可以这么说。就像现在一样,该脚本将(?)重定向到例如http://www.myownwebsite.com/checklogin.php(checklogin.php 是我在其中使用 cURL 的脚本)。

我尝试通过使用 followlocation cURL 函数来解决这个问题,但这样做会给我一个警告:

警告:curl_setopt(): CURLOPT_FOLLOWLOCATION 在启用安全模式或在 ----- 中设置 open_basedir 时无法激活

我使用 phpinfo(); 检查了我的 php.ini function 和 safemode 被关闭,open_basedir 没有值,所以我认为这不是问题。我查找了一些其他可能的解决方案,但到目前为止没有任何帮助我解决这个问题。

如果有任何不清楚的地方,请随时询问。

4

1 回答 1

2

您无法通过使用服务器向登录页面发送 POST 请求来登录用户。发生的情况是您登录了您的服务器,而不是用户。

此外,您不能使用 PHP 使用 POST 数据重定向用户。您所能做的就是制作一个带有隐藏字段method="POST"和的表单,该表单将在页面加载时通过 JavaScript 提交。这是您可以输出的页面的简单示例。action="http://www.differentwebsite.com/"usernamepassword

<html>
<head>
<script type="text/javascript">
function submit_form()
{
    document.myform.submit();
}
</script>
</head>
<body onload="submit_form();">
    <form method="POST" name="myform" action="http://www.google.com/">
        <input type="hidden" name="username" value="someusername"/>
        <input type="hidden" name="password" value="somepassword"/>
    </form>
</body>
</html>

至于为什么您会重定向用户,这是因为您跳过了:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

所以输出直接发送给用户。

于 2013-03-13T09:01:51.703 回答