douyinlai2169 2015-04-25 22:00
浏览 57
已采纳

OKHTTP - 发布用户输入数据,检索JSON响应以确定成功

I've been thinking in how to do this. I've read trough the OKHTTP documentation, I have an idea on how to handle this but I'm not entirely sure.

I'll have the user submit three fields ( Username, Password, Database ). I'll have a PHP file uploaded to my host which accepts post data. ( It's pure PHP, so no html. It just accepts data posted to the page. )

Once the values have been submitted, i'll perform a check. Credentials found? Return/echo JSON array ( Success/ Failure ). I could check if the array key " success " equals either 1 || 0. If 1, show a toast that user has been logged in successfully.

Now, as for the question. Is my logic correct? If not, how would you do it? Do you have any tips for me? Any examples?

  • 写回答

1条回答 默认 最新

  • douyi0902 2015-04-26 09:48
    关注

    To send the "authorization" in HTTP requests you have to set the header to 403.

    header('HTTP/1.0 403 Forbidden');
    
    echo 'You are forbidden!';
    

    But when I send JSON via a PHP page, I am using a 'generator' to make it easy, like Simple JSON for PHP.

    include('includes/json.php');
    
    $json = new json();
    
    $json->add('status', '403');
    $json->add('message', 'YOU ARE FORBIDDEN!');
    
    $json->send();
    

    UPDATE : Headers if you want to send "home made" JSON :

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    

    EDIT :

    There is also the Slim Framework, I didn't try it, but it seems very efficient! Simple JSON for PHP is as the name says, just to create a JSON & send it, it do not use make use of routes at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 鸿业暖通修改详细负荷时闪退
  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体