我正在尝试使用POST表单登录外部页面 -
<form id="login-form" action="https://www.mydomain.in/login/" method="post">
<ul>
<li><label for="id_username">Login ID</label><input id="id_username" class="text_box_small" type="text" name="username" maxlength="75" />
<div class="error_mssg"> </div>
</li>
<li><label for="id_password">Password</label><input id="id_password" class="text_box_small" type="password" name="password" />
<div id="error_msg" class="error_mssg"> </div>
</li>
<li><input type="submit" name="submit" value="Login" /><span id="email_wait_loader_id" class="email_wait_loader" style="display: none;"> </span>
</li>
</ul>
</form>
当我尝试输入用户名和密码时,我收到了一条消息 -
{“success”:{“message”:“登录成功”}}
但登录后它没有重定向到新页面。
答案 0 :(得分:2)
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$post = array(
'username' =>$Username,
'password' =>$Password
// Add As many parameters as you need
);
$post_data = http_build_query($post);
$ch = curl_init('https://www.mydomain/login/');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);// This stores the cookie , you can use it later if you have to make more request which requires user to be loged in
curl_exec($ch);
curl_close($ch);
一些有用的网址
http://www.php.net/manual/en/function.curl-init.php
http://www.php.net/manual/en/function.curl-setopt.php
http://www.php.net/manual/en/intro.curl.php
答案 1 :(得分:0)
您必须执行以下操作:
<form id="login-form" action="https://www.mydomain.in/login/" method="post" onsubmit="javascript:var self=this;$.ajax({ url:$(self).attr('action'), type:'json', success:function(response){ if(response.success != undefined){ document.location.href='http://www.sometarget.com/page/after/login';}else{alert('Login failed');}}});return false;">
这需要'$'
的jQuery基本上,表单将通过ajax提交并在成功时重定向..或抛出警报。
答案 2 :(得分:0)
仅限javascript:
$('#login-form').submit(function(e){
e.stopPropagation();
var self=this;
$.ajax({
url : $(self).attr('action'),
type : 'json',
success : function(response){
if(response.success != undefined){
document.location.href = 'http://www.sometarget.com/page/after/login';
}else{
alert('Login failed');
}
}
});
});