2

// script.js

$(document).ready(function(){
    $(".loginProzor").hide();
    $(".login").click(function(){
        $(".loginProzor").fadeToggle(300);
    });

    $("#prijavi").click(function(){
        if($("#user").val() == "" || $("#pass").val() == ""){
            $("#labelGreska").html("Unesite korisničke podatke");
        }
        else{
            $.post($("#forma").attr("action"), $("#forma :input").serialize(), 
            function(data){
                $("#labelGreska").html(data);
            });}

        $("#forma").submit(function(){
            return false;
        });

    });
});

// form

<form id="forma" action="login.php" method="post">
                            <label>Korisničko ime</label><br>
                            <input type="text" name="user" id="user"><br>
                            <label>Lozinka</label><br>
                            <input type="password" name="pass" id="pass"><br>
                            <input type="submit" value="Prijavi se" id="prijavi">   
                            <label id="labelGreska"></label>                        
                        </form>

//login.php

<?php

    include 'funkcije.php';
    include 'spojiBazu.php';

    $user = $_POST['user'];
    $pass = $_POST['pass'];

    if(!$user){
        $greske[] = 'Unesite korisničko ime';
    }
    $pass = md5($pass);
    $ucitaj = mysql_query("select * from login where username = '$user' and password = '$pass'");
    session_start();
    if(mysql_num_rows($ucitaj) === 0){
        echo 'Korisnički podaci nisu validni, molimo pokušajte ponovo.';
    }else{
        $korisnik = mysql_query("select username from login where username = '$user'");
        $podatak = mysql_result($korisnik, 0);

        $_SESSION['user'] = $podatak;
        header("Location: index.php");
    }


?>

Hello

I'm learning web development and I ran into a problem. I created simple login form. It evaluates some errors using jQuery and the rest of errors are evaluated using PHP. Everything works except Header command in PHP. When user succesfully logs in, header command should redirect to index.php so user can verify it is logged in, but in this case header tag don't work. Before applying jQuery (all errors were handled by PHP) header command worked with no problems. Can you tell what's wrong here?

10
  • " but in this case header tag don't work." What exactly do you mean? Is there an error message? Is the screen blank? Are there unintended side effects? Commented Jul 30, 2013 at 17:17
  • 1
    Your code is vulnerable to sql-injections, use prepared statements. Also show us the error if you want answers :) And don't use md5, it doesn't add any security at all. Commented Jul 30, 2013 at 17:18
  • 5
    You're doing the ajax call - the php header will redirect the ajax response... not the page that the user is currently sitting on. You will have to modify your javascript code in the client to change the location. Commented Jul 30, 2013 at 17:18
  • you are also missing session_start() before assigning the values in session variable Commented Jul 30, 2013 at 17:20
  • 1
    you might get some data, but whether jquery will honor that data is another matter - since it was told by the server to go elsewhere. Commented Jul 30, 2013 at 18:06

3 Answers 3

1

Details,

Since AJAX happens "behind the scenes" (so to speak) your redirect will just interrupt the response to your javascript handler. So PHP cannot redirect your browser now, jQuery can. So use jQuery to redirect the user.

You'll need to return the URL and have your callback kick the browser to a new location.

On this note, since you have to return data to the front end, you'll want to add a status or similar variable so that you can switch your front end behavior based on whether the call "failed" or not.

Exactly what Marc B pointed,

"You're doing the ajax call - the php header will redirect the ajax response... not the page that the user is currently sitting on. You will have to modify your javascript code in the client to change the location."

A javascript redirect is as simple as window.location.href = "http://mylocation";.

Solution to your problem,

JQUERY

// script.js

$(document).ready(function(){
    $(".loginProzor").hide();
    $(".login").click(function(){
        $(".loginProzor").fadeToggle(300);
    });

    $("#prijavi").click(function(){
        if($("#user").val() == "" || $("#pass").val() == ""){
            $("#labelGreska").html("Unesite korisničke podatke");
        }
        else{
            $.post($("#forma").attr("action"), $("#forma :input").serialize(), 
            function(data){
                if(data=="success"){
                                 window.location.href = "index.php";
                            } else{
                                 alert("login failed");
                            }
            });
                }
        $("#forma").submit(function(){
            return false;
        });

    });
});

PHP

<?php

    include 'funkcije.php';
    include 'spojiBazu.php';

    $user = $_POST['user'];
    $pass = $_POST['pass'];

    if(!$user){
        $greske[] = 'Unesite korisničko ime';
    }
    $pass = md5($pass);
    $ucitaj = mysql_query("select * from login where username = '$user' and password = '$pass'");
    session_start();
    if(mysql_num_rows($ucitaj) === 0){
        echo 'failed';
            exit;
    }else{
        $korisnik = mysql_query("select username from login where username = '$user'");
        $podatak = mysql_result($korisnik, 0);

        $_SESSION['user'] = $podatak;
        echo "success";
    }


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

1 Comment

I get error message (alert) when I input invalid data, but when I input valid data my page is still not redirected, I still have to reload page to get log in notification
0

from http://www.php.net/manual/en/function.header.php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called.

You can try with a javascript redirection or remake your source code with the header at the begining

Comments

0

header() will not work after output has been echoed to the screen.

Check funkcije.php and spojiBazu.php to see if any echo happening. If they are you need to find a way to remove the echos from those to included files before you call header() in login.php.

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.