To keep the php response visible when the page is refreshed

In my login script, everything works fine. I get the correct answers in my div (id = login_reply) and the session starts. But whenever I refresh the page, login_reply leaves. How can I save login_reply? Thank!

Here is the php:

if (isset($_POST['username'], $_POST['password']))
{
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string(md5($_POST['password']));

    $check = mysql_query("SELECT * FROM `userbase` WHERE `user_name` = '$username'");
    if (mysql_num_rows($check) > 0)
    {
        while ($row = mysql_fetch_assoc($check))
        {
            $user_id = $row['user_id'];
            $user_name = $row['user_name'];
            $user_email = $row['user_email'];
            $user_password = $row['user_password'];

            if ($password == $user_password)
            {
                $_SESSION['user_id'] = $user_id;
                if (isset($_SESSION['user_id']) && $_SESSION['user_id'] != '')  
                    echo "Welcome back '$user_name'!";
                else
                {
                    echo 'no';
                }

            }
            else
                echo 'no';
        }
    }
    else
        echo 'no';  
}

Here is jQuery

$(document).ready(function()
{

$('#login').click(function()
{
    var username = $('#username').val();
    var password = $('#password').val();

    $.ajax(
    {
        type: 'POST',
        url: 'php/login.php',
        data: 'username=' +username + '&password=' + password,
        success: function(data)
        {
            if (data != 'no')
            {
                $('#logform').slideUp(function()
                {
                    $('#login_reply').html(data).css('color', 'white');
                });
            }
            else                    
                $('#login_reply').html('Invalid username or password').css('color', 'red');
        }
    });
    return false;
});
});
+3
source share
3 answers

The problem is that JS is just a client-side scripting language - it is only processed in the client browser.

AJAX, , ( cookie). , cookie , , HTML, jQuery ...

, :

  • -
  • "log in"
  • , , , $_SESSION['username'] ()
  • AJAX $('#login_reply')
  • , ( ) $_SESSION['username']
  • , PHP div #login_reply, , ...

, ...

EDIT1. , () JS, POST ...

EDIT2: ...

:

if (isset($_POST['username'], $_POST['password']))
{ // <-- This is a .NET style of writing the brackets that I don't like much and I guess PHP don't like .NET either :-)
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string(md5($_POST['password']));

    $check = mysql_query("SELECT * FROM `userbase` WHERE `user_name` = '$username'"); // <-- mysql_* method calls should be replaced by PDO or at least mysqli
    // Also the query could be improved
    if (mysql_num_rows($check) > 0)
    {
        while ($row = mysql_fetch_assoc($check)) // <-- why calling while when You expect ONLY one user to be found?
        {
            $user_id = $row['user_id'];
            $user_name = $row['user_name'];
            $user_email = $row['user_email'];
            $user_password = $row['user_password']; // <-- What are these last 4 lines good for? This is useless...

            if ($password == $user_password) // <-- this condition SHOULD be within the SQL query...
            {
                $_SESSION['user_id'] = $user_id;
                if (isset($_SESSION['user_id']) && $_SESSION['user_id'] != '')  // <-- this condition is useless as You have just set the session variable... 
                // ALSO, if You use brackets with else it is good to use brackets also with if
                    echo "Welcome back '$user_name'!";
                else
                {
                    echo 'no';
                }

            }
            else
                echo 'no';
        }
    }
    else
        echo 'no';  
}

(- mysql_ *, ):

if (isset($_POST['username'], $_POST['password'])) {
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string(md5($_POST['password']));

    $check = mysql_query("SELECT * FROM `userbase` WHERE `user_name` = '{$username}' AND `user_password` = '{$password}' LIMIT 1"); // <-- We check whether a user with given username AND password exists and we ONLY want to return ONE record if found...
    if ($check !== false) {
        $row = mysql_fetch_assoc($check);

         $_SESSION['user_id'] = $row['user_id'];

         echo "Welcome back '{$row['user_name']}'!";
    } else {
        echo 'no';
    }
} else
    echo 'no';
}
+3

- cookie:

PHP cookie:

<?php
if (!isset($_COOKIE['new_one']) ) {
  setcookie('new_one', "ole", 0, "/");
  echo "logged in";
}
else {
  setcookie('new_one', null);
  echo "logged out";
}
?>

jQuery:

$(document).ready(function() {
  if ($.cookie("new_one") === "ole") {
    $('#login_reply').html("Show msg only when the server sets the cookie.");
  }
});
+2

- , , ; javascript - , .

, , php; , ..

+1

All Articles