2016-10-15 14:51
浏览 48


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.

header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
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->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;
            array_push($res['errors'], 'Invalid login details');
            $res['success'] = false;

        $res['success'] = false;        
     echo json_encode($res);

here is the ajax code.

<script type="text/javascript">
            $("#login").on("submit", function(e) {              
                var btn = $('#btn-login');
                    type: 'post',
                    url: 'loginexec.php',
                    cache: false,
                    dataType: 'json',
                    data: $('form#login').serialize(),
                    beforeSend: function() { 
                    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>');
                            } else {

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


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


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.

    点赞 打赏 评论
  • douxianglu4370 2016-10-15 15:44

    we use local storage for store some value for future use, it can overwrite the value based on key. Here are the example for store value localStorage.setItem("login",1); For retrieve the value of login use localStorage.getItem("login"); For remove the login key use localStorage.removeItem("login");


    点赞 打赏 评论

相关推荐 更多相似问题