0

我正在尝试使用 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">&nbsp;</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">&nbsp;</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": "登录成功"}}

但是登录后它没有重定向到新页面。

4

3 回答 3

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

于 2013-09-28T05:37:42.883 回答
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 提交并在成功时重定向.. 或引发警报。

于 2013-09-28T03:04:54.347 回答
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');
            }
        }
    });
});
于 2013-09-28T03:09:00.210 回答