Skip to content Skip to sidebar Skip to footer

How Can I Get Data In Local Storage Every Login On Laravel?

I use laravel 5.3 I set my data on the local storage javascript like this : localStorage.setItem('storedData', JSON.stringify(data)) Every time user login, I want to add a conditi

Solution 1:

Local storage is accessible on client side using javascript only so if you want to fetch data on server side use session or cookies(if data size is less than 4kb).

If you still want to use local storage than you have to write ajax call to send data to server

$( document ).ready(function() {
   var myData = localStorage.getItem('storedData');
   if(myData == undefined || myData == ""){
    } 
   else{
      $.ajax({url: "dostuff.php",
         data:{data:myData },
         success: function(result){
             localStorage.clear();
        }});
    }
  });

Solution 2:

$( document ).ready(function() {
    var checkStorage = storage.getItem('storedData');
    if (checkStorage) { 
        $.ajax({url: "dostuff.php", success: function(result){
            //DO STUFF
        }});
    }
});

Solution 3:

  1. Set Your value in localStorage while login.
  2. Create a common js file and include in the header (for all the pages after login).
  3. Inside that,

    $( document ).ready(function() {
     var myData = localStorage.getItem('storedData');
     if(myData == undefined || myData == ""){
      window.location = "/logout";
     } 
     else{
     //Do your ajax and send the data to controller
     }
     });
    
  4. While loading the login page clear the localStorage data.

    localStorage.clear();
    

Suggestion:

Better use session for these type of activities.

Post a Comment for "How Can I Get Data In Local Storage Every Login On Laravel?"