douyong5825 2018-04-25 21:17
浏览 48

标准正确的ajax响应登录

I'm developing my own PHP framework (which is working quite well, by the way), and now I'm developing a login system. This is no problem, I've got it already working perfectly, I have a database, a registration, login form, logout, etc.

But right now, when you fill up the login form and click "login", ajax is sent, the server (php) verifies the user (username and password) and sends a response back. Now the response is just a number:

0 - Login correct.

1 - Username incorrect.

2 - Username correct, but password isn't.

This works perfectly for its purpose, but I'm sure this is not the correct/professional way, I just invented this. So I'd like to know which would be the right response from a login verification.

For example, many webs use a json response with the "error code", and message, but I don't really know how it works. Also, will this really affect? Or it doesn't matter? What's the real use of this? Security? Mixing with other libraries and services? Or is my current system already fine?

/****** UPDATE ******/

You guys are telling me I shouldn't say that the username/email is right but password isn't. Okay, you are right, I agree and I will correct it, and thanks for your participation and help.

Nevertheless, that's not the question, and the answers should help someone else with my same doubt: the response.

I found this, very useful: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

And this function (PHP >5.4): http_response_code( $code_number);

Technically it would be:

function my_ajax_function( )
{
    //try login

    if (login_correct)
        http_response_code(200);  // 200 = OK
    else
        http_response_code(401);   // 401 = Unauthorized

    // ajax response is independent of the http response
    die("whatever I want");
}
  • 写回答

2条回答 默认 最新

  • dqxhit3376 2018-04-25 21:23
    关注

    The ajax reply await to be 200 if all ok, you can return from. Your server a message, and in the success ajax reply your read the reply of server and then if there are something that is wrong you show the details from. Errors, by example you can return 1 for login ok, - 1 for mail. Not exist, and - 2 for password not ok, i edit, you can create a token when you load your form, that will be verified in the ajax call on server later, in this way you add a point of security, you can too in server directly is all is ok redirect to the main page qbere you user is logged

    评论

报告相同问题?