PHP可以';t使用cUrl登录

PHP可以';t使用cUrl登录,php,forms,curl,login,Php,Forms,Curl,Login,我在一页上有这个表格: <form name="logForm" method="post" action="j_security_check" onsubmit="return validateLogin(this);" _lpchecked="1"> <input type="hidden" name="j_username"> <input type="hidden" name="catId" value="10000"> <

我在一页上有这个表格:

<form name="logForm" method="post" action="j_security_check" onsubmit="return validateLogin(this);" _lpchecked="1">
    <input type="hidden" name="j_username">
    <input type="hidden" name="catId" value="10000">
    <input type="hidden" name="redURL" value="/loggedIn.do">    
        <table border="0" bordercolor="pink" id="login-table">
            <tbody><tr>
                <td><input id="userId" type="text" name="username" class="login-fields phfield" value="username"></td>
                <td><input id="pwd" type="password" name="j_password" class="login-fields phfield" value="password"></td>
                <td rowspan="2" style="width: 105px;">
                    <input type="submit" value="" class="login_btn" id="login-submit">
                </td>
            </tr>
        </tbody></table>
    </form>
我无法登录,它会在登录失败页面重定向

请问,哪里不对?谢谢

冗长的:

* About to connect() to example.com port 443 (#0)
*   Trying xxx.xxx.xxx.xxx...
* Connected to example.come (xxx.xxx.xxx.xxx) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*   subject: CN=*.example.com
*   start date: Aug 10 00:00:00 2016 GMT
*   expire date: Aug 10 23:59:59 2017 GMT
*   common name: *.example.com
*   issuer: CN=RapidSSL SHA256 CA,O=GeoTrust Inc.,C=US
> POST /folder/j_security_check HTTP/1.1
Host: example.com
Accept: */*
Content-Length: 370
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------e6df4811bbdc

< HTTP/1.1 100 Continue
< HTTP/1.1 302 Found
< Date: Wed, 03 May 2017 14:38:21 GMT
< X-Powered-By: Servlet/3.0
< Location: https://example.com/folder/loginFailure.do
< Content-Length: 0
* Added cookie logged-user="""" for domain example.com, path /, expire 786297600
< Set-Cookie: logged-user=""; Expires=Thu, 01-Dec-94 16:00:00 GMT; Path=/; Domain=example.com
* Added cookie JSESSIONID="0000CKKhQSS79AIRXhTP-REhHSI:18hmtvfl9" for domain example.com, path /, expire 0
< Set-Cookie: JSESSIONID=0000CKKhQSS79AIRXhTP-REhHSI:18hmtvfl9; Path=/; HttpOnly
< Expires: Thu, 01 Dec 1994 16:00:00 GMT
< Cache-Control: no-cache="set-cookie, set-cookie2"
< Content-Type: text/plain
< Content-Language: en-US
* HTTP error before end of send, stop sending
< 
* Closing connection 0

您需要提供Javascript方法validateLogin()的代码

j_用户名是隐藏的,用户无法访问。可见输入,…
name=“username”
…,将包含用户提供的用户名。将此消息传递给PHP代码,以便成功登录

编辑:评论中提供的新信息 您没有使用$\u POST数据。您试图在curl代码中传递尚未设置的数据(我们可以看到)。尝试以下替换:

$cookiedata = array(
   "username" => $_POST['j_username'],
   "j_username" => $_POST['j_username'],
   "j_password" => $_POST['j_password']
);
编辑:添加了两个用户名字段,因为您的chrome curl更新看起来同时使用了这两个字段

答案是:

最好通过浏览器检查“请求头”,以便模拟准确的请求

在这种情况下,内容类型必须是应用程序/x-www-form-urlencoded

所以,
curl\u setopt($ch,CURLOPT\u POSTFIELDS,$cookiedata)不好

您需要:

curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($cookiedata));

您已将详细信息输出到文件,但未包含任何输出。将verbose.txt的内容添加到您的问题中。@Luke添加了verbose…谢谢发送结束前的HTTP错误,停止发送
行表明响应被目标服务器终止显然我可以通过浏览器使用相同的用户名和密码登录…确实我通过了…但失败仍然需要您的javascript代码。这会将数据传递到PHP,对吗?你的确切意思是什么?那不是我的服务器…什么javascript代码?看起来j_用户名值是通过javascript输入的用户名设置的。不过我也没办法,因为你提供的所有代码都没有提供完整的信息。代码中未使用$\u post数据,登录脚本正在查找未在您提供的任何代码中设置的集cookie。“代码中未使用$\u post数据…”?如何设置饼干?$cookiefile不够吗?我不明白,对不起。。。
$cookiedata = array(
   "username" => $_POST['j_username'],
   "j_username" => $_POST['j_username'],
   "j_password" => $_POST['j_password']
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($cookiedata));