简体   繁体   English

使用 jquery 进行登录重定向

[英]Login redirect using jquery

i'll be glad if anyone can help out am working on a login system using jquery but i encounter a problem如果有人可以帮助我在使用 jquery 的登录系统上工作,我会很高兴,但我遇到了问题

what i want to achieve is user filling out their details on the login page and i'll process it using jQuery at the back end without reloading the page, i have achive that but the problem now is when the details they provide and the details in the database is correct i want to redirect them to another page我想要实现的是用户在登录页面上填写他们的详细信息,我将在后端使用 jQuery 处理它而不重新加载页面,我已经实现了,但现在的问题是他们提供的详细信息和详细信息数据库是正确的我想将它们重定向到另一个页面

here is my login form这是我的登录表格

<form class="form-login" id="loginmyForm" method="post">
<input class="input input_auth" type="text"  name="loginemail" id="loginemail" placeholder="E-mail" required />
<span id="loginError_username" class="error error-opacit"></span>

<input class="input input_auth" type="password" name="loginpassword" id="loginpassword" placeholder="Password" required />
<span id="loginError_password" class="error error-opacit"></span>
<input type="hidden" name="source" value="login" id="source">
    
<button class="btn pulse input_auth" type="button" id="submitFormData" onclick="loginSubmitFormData();" value="Submit">Login</button>

<div class="forgot-password">
    <a id="forgotPass" href="#" class="link-btn open-modal" data-openModal="modal-recovery">Forgot your password?</a>
</div>

Here is jquery code这是 jquery 代码

<script type="text/javascript">
    function loginSubmitFormData() {
    var loginemail = $("#loginemail").val();
    var loginpassword = $("#loginpassword").val();
    var source = $("#source").val();
    $.post("authlogin.php", { loginemail: loginemail, loginpassword: loginpassword },
       function(data) {
         $('#loginresults').html(data);
         $('#loginmyForm')[0].reset();
       });
}
</script>

And here is the login authentication authlogin.php这里是登录验证 authlogin.php

<?php
    session_start();
    include 'config/info.php';

    // get the details from form 
    $email=$_POST['loginemail'];
    $password = stripslashes($_REQUEST['loginpassword']);
    $password = mysqli_real_escape_string($conn,$password);

    $sql="SELECT * FROM user_info WHERE email='".$email."'";
    $result = mysqli_query($conn,$sql);
    $Countrow = mysqli_num_rows($result);
    if ($Countrow == 1) {
        $fetchrow = mysqli_fetch_assoc($result);  
        $loginpassword = $fetchrow['password'];

        // Verify the password here
        if (password_verify($password, $loginpassword)) {
            $_SESSION['email'] = $email;
            //setcookie('username', $adminID, time() + (86400 * 30), "/"); 
            $date = date('Y-m-d H:i:s');
            $ActivityStmt = "INSERT INTO login_activity (`email`, `last_login`, `browser`, `os`, `ip_address`) VALUES('".$email."', '".$date."', '".$gen_userBrowser."', '".$gen_userOS."', '".$gen_userIP."')";
            $ActivityResult = mysqli_query($conn, $ActivityStmt);

            echo 'Login Successfully! <a href="account" style="color: green;">Click to proceed</a>';

            exit();
        }
        else{
            echo 'Incorrect Password';
            exit();
        }
    }
    else{
        echo 'User does not exit';
        exit();
    }
    ?>

I have tried using我试过使用

header('Location: account'); 

and

window.location.href = "account";

after the session is saved but none is working, please who can help me on how to get this done在 session 被保存但没有工作后,请谁能帮我完成这项工作

You should try this jQuery Code and PHP Code by replacing them in your code section, It will definitely work for you:您应该通过在您的代码部分替换它们来尝试此 jQuery 代码和 PHP 代码,它肯定会为您工作:

 <script type="text/javascript"> function loginSubmitFormData() { var loginemail = $("#loginemail").val(); var loginpassword = $("#loginpassword").val(); var source = $("#source").val(); $.post("authlogin.php", { loginemail: loginemail, loginpassword: loginpassword }, function(data) { var data = jQuery.parseJSON( data ); console.log(data); $('#loginresults').html(data.message); if(data.redirect_url){ window.location.href = data.redirect_url; } $('#loginmyForm')[0].reset(); }); } </script> <?php session_start(); include 'config/info.php'; // get the details from form $email=$_POST['loginemail']; $password = stripslashes($_REQUEST['loginpassword']); $password = mysqli_real_escape_string($conn,$password); $sql="SELECT * FROM user_info WHERE email='".$email."'"; $result = mysqli_query($conn,$sql); $Countrow = mysqli_num_rows($result); if ($Countrow == 1) { $fetchrow = mysqli_fetch_assoc($result); $loginpassword = $fetchrow['password']; // Verify the password here if (password_verify($password, $loginpassword)) { $_SESSION['email'] = $email; //setcookie('username', $adminID, time() + (86400 * 30), "/"); $date = date('Ymd H:i:s'); $ActivityStmt = "INSERT INTO login_activity (`email`, `last_login`, `browser`, `os`, `ip_address`) VALUES('".$email."', '".$date."', '".$gen_userBrowser."', '".$gen_userOS."', '".$gen_userIP."')"; $ActivityResult = mysqli_query($conn, $ActivityStmt); $message = 'Login Successfully;', $response = array( 'message' => $message: 'redirect_url' => 'https.//www.example,com'; ); exit(); } else{ $message = 'Incorrect Password'; $response = array( 'message' => $message ); exit(); } } else{ $message = 'User does not exit', $response = array( 'message' => $message; ); exit(); } echo json_encode( $response)? ?>

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

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