使页面刷新时,php响应仍然可见

时间:2012-05-15 07:52:24

标签: php jquery

我的登录脚本一切正常。我在我的div中获得正确的响应(id = login_reply)并且会话开始。但每当我刷新页面时,login_reply就会消失。我怎么能保持login_reply?谢谢!

这是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';  
}

这是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 个答案:

答案 0 :(得分:3)

问题是JS只是客户端脚本语言 - 它只在客户端浏览器上处理。

如果您想使用AJAX登录,好的,但您必须将登录用户的值存储到会话(或cookie)中。然后在每个页面加载您将检查该会话或cookie是否已设置和存在,如果它们存在,您将在登录后写下与jQuery相对应的HTML ...

换句话说,这就是你要做的事情:

  1. 页面加载 - 用户未登录
  2. 用户填写登录凭据并点击“登录”
  3. 您检查该用户是否存在,如果存在,则保存$_SESSION['username'](例如)
  4. 在AJAX中填写$('#login_reply')
  5. 用户点击您网站中的某些链接
  6. 您可以检查$_SESSION['username']
  7. 中是否有值(或设置了索引)
  8. 如果是,请填写#login_reply div,如果不是,您将显示登录表单...
  9. 希望这会有所帮助......

    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';
    }
    

答案 1 :(得分:2)

最简单的解决方案是使用cookies:

在PHP方面,您声明并删除co​​okie:

<?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 :(得分:1)

您需要将登录回复数据存储在服务器上或者会话中; javascript是一种客户端语言,因此在刷新之间不会保留所获得的任何值。

我建议您存储结果,然后在使用php生成页面时检查它是否存在;如果是,请填写数据..等等。