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

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度