redirect ajax call after checking username & password

时间:2015-07-28 22:38:24

标签: php jquery html ajax

I am trying to get my login request redirected to a session "logged_in" with php header location, if username and password is correct, my problem is that header("Location:secret.php"); won't do anything the only thing that will work is echo.. I am not sure why :/

Here is my code:

HTML

<html>
<head>
</head>
<body>
<div class="login_box">
    <div class="user_logo"></div>
    <div class="login_form">
        <form>
            <input id="username" type="text" placeholder="Username">
            <input id="password" type="password" placeholder="Password">
            <input id="login" type="button" value="Login">
        </form>
    </div>
    <p id="result"></p>
</div>

<script src="script/jquery-2.1.4.min.js"></script>
<script src="script/ajax_script.js"></script>
</body>
</html>

Ajax script

$(document).ready(
function(){
    $('#login').click(function(){
        $.post("ajax.php",
        {
            username : $('#username').val(),
            password : $('#password').val()
        },
            function(data){
                if (data=="0") {
                    $('.login_box').addClass("overflow");
                    var $login_form =   $('.login_form').addClass("error_shake");
                    $('.login_form input').addClass("error_border");
                    setTimeout(function() {
                        $login_form.removeClass("error_shake");
                    }, 300);
                    $('#result').html('Forkert brugernavn eller adgangskode!');
                }
            }
        );
    });
}
);

PHP

require('includes/db.php');

if ( isset($_POST['username']) && isset($_POST['password']) ) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

    $result = $db->query($query); // udfører databaseopkald og sætter det ind i vores query

    $rows = $result->num_rows; // hent antal rækker og sæt det ind i resultat

    if($rows == 1){
        $_SESSION['logged_in'] = TRUE;
        while ($row = $result->fetch_assoc()) {
            $_SESSION['user_id'] = $row['ID'];
        }
        header("Location:secret.php"); //THIS WON'T WORK
    }
    else{
        echo "0";
    }
}

Secret site to redirect to

<?php
session_start();
if ($_SESSION['logged_in'] != TRUE) {
    header("Location: index");
    exit();
}

?>


Top secret!!!

I hope that someone knows how to do this, or have some ideas. Thanks!

2 个答案:

答案 0 :(得分:2)

Since you're doing an Ajax call, you cannot redirect this way. If you would want to, you could return a string from your php in the place where you redirect, with the path you want to redirect to and then in your javascript do something like location.href = "your_secret_url" to redirect.

(untested) Example:

PHP:

if($rows == 1){
    $_SESSION['logged_in'] = TRUE;
    while ($row = $result->fetch_assoc()) {
        $_SESSION['user_id'] = $row['ID'];
    }
    // Yeah, we're logged in!
    $result = array(
        'result' => 1,
        'location' => '/secret.php',
    );
}
else{
    // Nope.
    $result = array(
        'result' => 0,
    );
}

// return the result
echo json_encode($result);
die();

JS:

$.post("ajax.php",
{
    username : $('#username').val(),
    password : $('#password').val()
},
    function(data){
        if (data.result == 0) {
            $('.login_box').addClass("overflow");
            var $login_form =   $('.login_form').addClass("error_shake");
            $('.login_form input').addClass("error_border");
            setTimeout(function() {
                $login_form.removeClass("error_shake");
            }, 300);
            $('#result').html('Forkert brugernavn eller adgangskode!');
        } else if (data.result == 1) {
            location.href = data.location;
        }
    }, "json" // get content which has been returned in json format
);

Edit possible duplicate: Call PHP function with AJAX, but redirect doesn't work

答案 1 :(得分:0)

You are redirecting the ajax called page to secret.php. That won't affect the calling page from which you are running the ajax request.