0

I made sure that I was not sending any client sided data before hand and I turned on error reporting but nothing is happening.

It's a simple login script with a redirection on validation.

    <?php
include_once "database-handler2.php";
require "password.php";

$UserName = $_POST["name"];
$Password = $_POST["pass"];

if(isset($UserName))
{
    $query = "SELECT Password FROM users WHERE Username='$UserName'";
    $hashed_password = mysqli_query($connection, $query);

     while ($row = mysqli_fetch_row($hashed_password))
    {
       $res = $row[0];
    } 


    if(password_verify($Password, $res)) {
       // Show Errors
      error_reporting(E_ALL);
      ini_set('display_errors','On');
      // Redirect
      header('Location:http://example.com/ex/index.php', true);
      die('redirect');
    }
}

I have made sure that the other included php's don't have an extra whitespace or anything else. Using AJAX the success function does nothing, and if I turn on alert() or debug.log() the success message of the PHP's response (which there shouldn't be one) then I get a print out of the page I'm trying to redirect to's HTML without any error or other information under or above it.

Javascript code:

    $('#submitButton').click(function()
{
        var userName = document.getElementById("user").value;
        var pass = document.getElementById("pass").value;
    $.ajax({
        url: '/handlers/login-handler.php',
        type:'POST',
        data:
        {
            name: userName,
            pass: pass
        },
        success: function(msg)
        {
                    alert(msg);
        }               
    });
});

Please note that if I take the alert(msg); out than if I click the button nothing happens.

enter image description here

6
  • 2
    The redirect is redirecting where AJAX reads the response from. What did you expect to happen instead? Commented Sep 20, 2017 at 5:44
  • AJAX never reloads the page by itself, that's the whole point. If you want to redirect after an AJAX request, you have to do that in the Javascript code. Commented Sep 20, 2017 at 5:45
  • 1
    Why are you using AJAX if you want to reload the page from the place where PHP redirects to? Use an ordinary form submission. Commented Sep 20, 2017 at 5:45
  • 1
    You should redirect your page onSuccess using javascript; not by php because its an ajax request. Commented Sep 20, 2017 at 5:46
  • @everyone thank you for the comments however when I try to redirect with Javascript my sessions are lost. How would I go about redirecting with the header via php without submitting a form? Commented Sep 20, 2017 at 5:49

3 Answers 3

1

remove this line of your php code

header('Location:http://example.com/ex/index.php', true);

and add below line of code on the success response of your ajax.

window.location.href="http://example.com/ex/index.php"

If you are AJAX then server side redirection will not work, you have to redirect with jquery/javascript.

Sign up to request clarification or add additional context in comments.

Comments

0

You should return success type response from your php code to ajax response and then you should use window.location JS code to get your page redirected after successful process.

1 Comment

@Brandon If you are storing information in php sessions, then they won't go if you redirect with JS.
0

Don't use javascript/jQuery to submit the form, but use a standard html form instead. You don't need any javascript to submit a form.

<form method="post" action="/handlers/login-handler.php">
    <label for="user">Username:</label>
    <input type="text" id="user" name="user">
    <label for="pass">Password:</label>
    <input type="text" id="pass" name="pass">
    <input type="submit" value="Submit">
</form>
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.