Develop mobile app and server-side session state management and interaction

Source: Internet
Author: User

When we are doing web development, we usually use cookies or session to save the user's login status, verify whether the user has access to some pages that need to log in by checking the cookie or session data, this is done through the browser, this is the B/s architecture, But what if the client is a mobile app? Because this is the C/s architecture, it is not possible to use a cookie or session to verify the user's status, as if the browser has disabled cookies.

Fortunately, this is a workaround, in the case of disabling cookies, you can pass the session_id through Query_string, that is, after the app sends a login request, the server can pass session_id to the app, then the app saves the session _ID on mobile devices, in those functions that require login access, each interaction request is accompanied by a parameter session_id, routed to the server side, and the server-side checks the legitimacy of the session_id to determine if the user is logged in.

Here's a simple mobile development example that doesn't use native, but uses Appcan to build the app:

1. App Login Request:

  1. var url = ' http://127.0.0.1:8080/index.php?act=login&[email protected]&pwd=123456 ';
  2. $. getjson(url,function(res) {
  3. if(res. Ok = = ' yes ') {
  4. var storage = window. Localstorage;
  5. if(storage) storage. SetItem(' Sid ',res. session_id);
  6. }Else{
  7. Uexwindow. Toast(0, 5, ' login failed! ', 4000);
  8. return;
  9. }
  10. }, ' json ',null, ' POST ', ', ' );

2. App Request user information:

  1. var sid = ';
  2. var storage = window. Localstorage;
  3. If(storage) sid = storage. GetItem(' Sid ');
  4. var url = ' http://127.0.0.1:8080/index.php?act=uinfo&session_id= '+sid;
  5. $. getjson(url,function(res) {
  6. if(res. Ok = = ' yes ') {
  7. var uname = res. Username;
  8. Uexwindow. Toast(0, 5, ' username: '+uname, 4000);
  9. return;
  10. }Else{
  11. Uexwindow. Toast(0, 5, ' please login first! ', 4000);
  12. return;
  13. }
  14. }, ' json ',null, ' POST ', ', ' );

3. Server-side PHP response request [index.php]:

  1. <? PHP
  2. /**
  3. * User:wudiweb.com
  4. * App and server-side Simple example
  5. */
  6. Header("content-type:text/html; charset= ' Utf-8 ');
  7. Session_Start();
  8. $act = $_request[' act '];
  9. $result = array(' OK ' = ' yes ');
  10. If($act = = ' login ') {
  11. $email = $_request[' email '];
  12. $pwd = $_request[' pwd '];
  13. if($email = = ' [email protected] ' && $pwd = = ' 123456 ') {
  14. $result[' session_id '] = session_id();
  15. }Else{
  16. $result[' OK '] = ' no ';
  17. }
  18. }ElseIf($act = = ' uinfo ') {
  19. $session _id = $_request[' session_id '];
  20. if($session _id = = session_id()) {
  21. $result[' username '] = ' wudiweb ';
  22. }Else{
  23. $result[' OK '] = ' no ';
  24. }
  25. }
  26. echo Json_encode($result);
  27. Exit;

Note that this is only a simple usage, and if you think it is not perfect, you can extend it on this basis, such as encryption session_id.

Develop mobile app and server-side session state management and interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.