php not redirecting and logging in

118
header("Location: workspace/index.php");

An Ajax request passes data to another page (a script) which processes this data and usually returns some data. You should just echo the data that needs to be returned, not attempt a re-location. The page making the Ajax request should re-direct the current page if necessary, using JavaScript.

Also, you echo data after the header() call, so this echo'd data is probably not sent.

Share:
118

Related videos on Youtube

core16
Author by

core16

Updated on November 24, 2022

Comments

  • core16
    core16 12 months

    So when i log in with correct details my system gives an error message with a blank message and does not redirect? Why is this happening, i don't see how returning a blank error is possible in my code?

    calling it:

    $("#log").click(function () {
        username = $("#user").val();
        password = $("#password").val();
        submit = $("#log").val();
        $.ajax({
            type: "POST",
            url: "",
            data: "submit=" + submit + "&username=" + username + "&password=" + password,
            success: function (response) {
                if (response == 'success') {
                    //should never be called?
                } else {
                    $("#error-log").remove();
                    var error_msg = response;
                    $("#s-log").append('<div id="error-log" class="err welcome dismissible">' + error_msg + '</div>');
                }
            }
        });
        return false;
    });  
    

    checking:

    if($_POST['submit']=='Login')
    {
        // Checking whether the Login form has been submitted
    
        $err = array();
        // Will hold our errors
    
    
        if(!$_POST['username'] || !$_POST['password'])
            $err[] = 'All the fields must be filled in!';
    
        if(!count($err))
        {
            $_POST['username'] = mysql_real_escape_string($_POST['username']);
            $_POST['password'] = mysql_real_escape_string($_POST['password']);
            $_POST['rememberMe'] = (int)$_POST['rememberMe'];
    
            // Escaping all input data
    
            $row = mysql_fetch_assoc(mysql_query("SELECT id,usr FROM members WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));
    
            if($row['usr'])
            {
                // If everything is OK login
    
                $_SESSION['usr']=$row['usr'];
                $_SESSION['id'] = $row['id'];
                $_SESSION['rememberMe'] = $_POST['rememberMe'];
    
                // Store some data in the session
    
                setcookie('FRCteam3482Remember',$_POST['rememberMe']);
                header("Location: workspace/index.php");
                echo 'success';
            }
            else $err[]='Wrong username and/or password!';
        }
    
        if(count($err)) {
            $_SESSION['msg']['login-err'] = implode('<br />',$err);
            // Save the error messages in the session
    
            echo $_SESSION['msg']['login-err'];
            //php unset($_SESSION['msg']['login-err']);
            //header("Location: index.php");
        }
        exit;
    }
    
  • core16
    core16 over 10 years
    I want to change pages from the login/register page (current) to the actual members area?
  • Daedalus
    Daedalus over 10 years
    @user1361852 Then why are you using ajax?
  • core16
    core16 over 10 years
    To update/show the error messages and show/hide the registration form.
  • Daedalus
    Daedalus over 10 years
    @user1361852 You can't redirect the page in an ajax response. Since you've resigned to using javascript, just use javascript to conduct the redirect.
  • core16
    core16 over 10 years
    how could i use something like window.location = "http://www.yoururl.com"; but only by adding onto the current address. So if i ever change domains or something it would still work with having to update it.
  • core16
    core16 over 10 years
    how could i do that without using the absolute address, is there a way to do it by adding on to the current page's address?
  • Daedalus
    Daedalus over 10 years
    window.location supports relative addresses.
  • Diogo Baracho
    Diogo Baracho over 10 years
    I not sure if I understood your comment, but if i undertood you can put PHP $_SERVER['HTTP_REFERER'] in a html element and use it in your Ajax function