Question

Is there any way to save a php object in the client side, so when I further need it I don't need to make Ajax request to get another new.

Was it helpful?

Solution

Yes -- use an HTML5 localStorage key to save whatever data you need:

localStorage["hi"] = "ho";

// and then later ...
alert(localStorage["hi"]);

Strings saved into the browser's local storage will persist when the user navigates to a new page on your site, or returns to your site later. Its browser support is pretty good -- even IE8+ supports it.

One note: you can only save string data. So, if you have an object, then you'll need to use JSON.stringify / JSON.parse:

var user = { firstName: "foo", lastName: "bar" };
localStorage["name"] = JSON.stringify(user);

// and then later ...
var restoredUser = JSON.parse(localStorage["name"]);
if (restoredUser)
    alert("Hi, " + restoredUser.firstName);

OTHER TIPS

You can use object serialization/deserazlization as explained here storing php objects on html form element and passing php objects through GET method?

  • serialize the object and encrypt the output xml string that goes on client
  • Then when client send request back you can decrypt the xml string and restore the object on server side by using deserialization

About encryption see this one Simplest two-way encryption using PHP

And also you can use HTML 5 client local storage as said above but this may not work in older browsers by default see localStorage supported browsers

And again it is really depends on what you exactly want to achieve and what kind of data you want to store, in some cases you can just simply use server caching so in such case you do not need to store the whole object on the client at all.

Sure, you can certainly store a PHP object at the client side. Eve an object, which contains references to object, arrays as member variables...

But don't trust anything coming back from the client that you forwarded before to the client!

An attacked can easily change or replace the serialized object at the client side. Therefore, it's extremely dangerous to store server-side objects at the client side.

That said, here is how to store a PHP object at the client side

First, serialize the object:

$serializedObject = serialize( $someObject );

Next, save the object using local storage at the client side (base64_encode() 'hides' quotes):

<script type="text/javascript">
  localStorage["soAccessor"] 
      = "<?php echo base64_encode( $serializedObject ); ?>";
</script>

Finally, you might pass it back to the PHP-side using e.g. a hidden field of a form.

In case you need to deserialize something back to its original object state, use deserialize(). Note, that you need to require_once() each object's class definition before doing the deserialize operation.

During deserialization, PHP tries to __wakeup() serialized objects. This won't happen correctly, if the class isn't orderly defined yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top