简体   繁体   English

PHP CURL登录问题

[英]PHP CURL Problems with login

I have problem with logging using cURL. 我使用cURL登录时遇到问题。 This is my code: 这是我的代码:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', '1');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://mysite.net/post/login.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/test.cookie'); // replace this with /tmp or something like that
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'accountName' => 'name',
    'accountPassword' => 'password'
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo $output; 
?> 

What's wrong with my code? 我的代码有什么问题? I cant login. 我无法登录。 Sorry for my english. 对不起我的英语不好。

There's FORM form mysite.net 表单来自mysite.net

<form class="form-horizontal" method="post" action="post/login.php">
<input type="hidden" name="securitytoken" value="672385374d4ed88e5ac0d6aaf29ae5f25280ed549a01f1.65943444">
<fieldset>
<h2>Account Login</h2>
<div class="control-group">
<label class="control-label" for="accountName">Account name</label>
<div class="controls">
<input type="text" class="input-large" id="accountName" name="accountName">
</div>
</div>
<div class="control-group">
<label class="control-label" for="accountPassword">Account password</label>
<div class="controls">
<input type="password" class="input-large" id="accountPassword" name="accountPassword">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Submit</button>&nbsp;
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>

Any ideas? 有任何想法吗? What did I do wrong? 我做错了什么? I tried changing the code, used example codes. 我尝试更改代码,并使用示例代码。 Still nothing. 依然没有。

There are several issues. 有几个问题。

  1. You need to supply securitytoken as well as accountName and accountPassword . 您需要提供securitytoken以及accountNameaccountPassword To do that you need to grab the page, extract token and post it with credentials 为此,您需要抓取页面,提取令牌并使用凭证将其发布
  2. What enctype is used for the form normally (by browser)? 正常情况下(通过浏览器)表单使用哪种enctype? You can look that up via any browser traffic addon. 您可以通过任何浏览器流量插件进行查找。 If you provide $data as array, cURL will use multipart/form-data . 如果提供$data作为数组,则cURL将使用multipart/form-data If it is a string - content type will be application/x-www-form-urlencoded 如果是字符串-内容类型将为application/x-www-form-urlencoded

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM