dty3416 2016-10-15 14:51
浏览 48
已采纳

phonegap:将数据库值存储在本地存储中

This is first time i am using phonegap and local storage. So, if i asked something silly then i apologise.

I am trying to create a login/signup page for mobile app. I have created a front end page in html/js/jquery and backend is in php.

In php we use session to store some data but in html there is no session. However i got to know that we can use local storage to store these values. Here is my php code which is used for authentication.

<?php
session_start();
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
require_once('include/db.php');
require_once('functions.php');
$res=array();
$res['errors']=array();
if(isset($_POST)) {
    if(empty($_POST['email'])) {
        array_push($res['errors'], "Email could not be empty");
    }
    if(empty($_POST['password'])) {
        array_push($res['errors'], "Password could not be empty");
    }
    if(!empty($_POST['email']) && !isValidEmail($_POST['email'])) {
        array_push($res['errors'], "Invalid Email Format");
    }

        $email = $_POST['email'];
        $password = $_POST['password'];
        $now = date('Y-m-d H:i:s'); 

    if(empty($res['errors'])) { 
        $result = $mysqli->prepare("SELECT uid,pic,fname,last_seen FROM users where email= ? and password = ? and status = '1'");
        $result ->bind_param("ss", $email, $password);
        $result->execute();
        $result->store_result();

        $result->bind_result($uid, $pic, $fname, $last_seen);
        if($result->num_rows == 1){

             while ($result->fetch()) {
                        $_SESSION['uid'] = $uid;
                        $_SESSION['pic'] = $pic;
                        $_SESSION['name'] = $fname;
                        $_SESSION['last_login'] = $last_seen;
            }

            $res['success'] = true;
        }
        else{
            array_push($res['errors'], 'Invalid login details');
            $res['success'] = false;

        }
    }else{
        $res['success'] = false;        
    }
    session_write_close();
     echo json_encode($res);
}
?>

here is the ajax code.

<script type="text/javascript">
$('document').ready(function()
{
            $("#login").on("submit", function(e) {              
                e.preventDefault;
                var btn = $('#btn-login');
                btn.button('loading');
                $.ajax({
                    type: 'post',
                    url: 'loginexec.php',
                    cache: false,
                    dataType: 'json',
                    data: $('form#login').serialize(),
                    beforeSend: function() { 
                        $("#validation-errors").hide().empty(); 
                    },
                    success: function(data) {
                        if(data.success == false)
                            {
                                var arr = data.errors;
                                $.each(arr, function(index, value)
                                {
                                    if (value.length != 0)
                                    {
                                        $("#validation-errors").append('<div class="alert alert-danger"><strong>'+ value +'</strong><div>');
                                    }
                                });
                                $("#validation-errors").show(); 
                                btn.button('reset');                            
                            } else {
                                localStorage.login="true";
                                localStorage.email=email;
                                localStorage.id=id;

                                 window.location.href = 'user_profile.php';
                            }
                    },
                    error: function(xhr, textStatus, thrownError) {
                        alert('Something went to wrong.Please Try again later...');
                        btn.button('reset');
                    }
                });             
                return false;
            });
        });

    </script>

I want to store these values in local storage but not sure how to do that.

$uid;
$pic;
$fname;
$last_seen;

Please advise.

  • 写回答

2条回答 默认 最新

  • douxunchen3498 2016-10-15 15:24
    关注

    First mistake here I see is usage of localStorage is wrong:

    use window.localStorage.setItem("login",1); rather than localStorage.login ="true";

    then to access it use window.localStorage.getItem("login");

    However, in my opinion it is also not a good idea too store something like sessions, (and even also not email addresses) in localStorage as any other malicious app can access all localStorage values,- why not rather do a single ajax request on each page first to check if valid session is establisshed and if not redirect to login. In this case there would also be no need to store last_seen time as well, sessions and good authentification should always be done server side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥30 itest不允许查看成绩怎么办
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏