douyou3619 2013-03-13 08:33
浏览 48
已采纳

cURL关注位置无法正常工作

I am currently working on a script where the user has to fill in a username and a password. Then, when the user logs in, the script checks if his/her username is registered in my database. If that is the case, the user logs in on my external website (using cURL), and if not, the user logs in on a different website of which I do not have access to the database.

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);
}

as you can see, my script stores the row count in a count variable, and if the query results 1 row, it logs in on my site, and if not it logs in on the other site. The username and password checking is done on the actual websites the user logs in to.

Now my problem is, that I want it to "follow the location", or so to speak. The script, as it is right now, redirects(?) to e.g. http://www.myownwebsite.com/checklogin.php (checklogin.php being the script I'm using cURL in).

I tried solving this by using the followlocation cURL function, but doing that gives me a warning:

Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in -----

I checked my php.ini by using the phpinfo(); function and safemode is turned off, open_basedir has no value, so I don't think this is the problem. I looked up some other possible solutions, but nothing so far helped me solve this issue.

If anything is unclear, feel free to ask.

  • 写回答

1条回答 默认 最新

  • drzbc6003 2013-03-13 09:01
    关注

    You can't log in user by sending POST request to login page with your server. What happens is you get your server logged in, not the user.

    Also, you can't redirect the user with POST data with PHP. All you can do is you can make a form with method="POST" and action="http://www.differentwebsite.com/" with hidden fields username and password, which will be submitted via JavaScript on page load. Here's simple example of the page you could output.

    <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>
    

    As for why you get user redirected, it is due to the fact that you skipped:

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    

    so the output gets sent to the user directly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目