How can I pass a PHP object created during my initial page load, to an ajax call?
create-page.php creates my page and has an object expensiveObject. I need to pass expensiveObject to ajax-some-stuff.php.
Option 1: Should I json_encode() the object, attach it to my URL, grab that part of my URL with javascript, and send it back to a PHP ajax?
Option 2: Can I store it as a $_SESSION variable?
I would save it in $_SESSION as this would prevent you from sending data trough internet which should be only processed on server. There would be security issues as well as performance issues.
You might need to implement __sleep() and __wakeup() depending on your object when saving it in $_SESSION
I would like go with Option 1 but after json_encode function i will wrap it again with base64_encode because the json_encode is still not URL-safe string.
The $_SESSION method is good and easy but it depends on your object. If i'm not mistaken, the $_SESSION has maximum size of data can be stored there.
- if you're server side session your server memory will consumed
- using cookie as session replacement will limit the data size
Related
I'm using CodeIgniter 3.x with database session driver and i want to access data cloumn that BLOB type. Here my blob data:
__ci_last_regenerate|i:1435420891;identity|s:13:"john#doe.com ";username|s:13:"johndoe";email|s:13:"john#doe.com ";user_id|s:1:"5";old_last_login|s:10:"1435412865";
I tried with unserialize($string) but didnt work!
unserialize(): Error at offset 0
How can i access blob data element? For ex: $user['email']
There's no straight-forward way to do that ... You could use session_decode(), but it requires that you already have an active session, so that it can put the decoded data into $_SESSION.
I must tell you however, if you want to do that - you're doing it wrong. You should never access another user's session. If there's some data that's tied to a session that's not explicit to the user who owns the session, you should just add another field to the sessions table and save it in there.
I got the solution here
So I have used session decode http://php.net/session_decode
session_decode('__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example#xyz.com";logged_in|b:1;');
So session decode stored all the encrypted data in normal php session.
Which I can access using:
echo $_SESSION['ci_UserID'];
As Narf says there is no easy way to do this.
ykay;s solution presupposes that you are using the current built in session handler. That can be changed at any time by the application, and there are no guarantees that PHP will keep this format.
Your solution trashes the current session and replaces it with the stored data (but at least it will use the "current" serialization method).
The serialization function and the read/write operations for session_ functions can be overridden at runtime. As long as you read back data encoded using the same mechanism as you use for decoding your mechanism will work - but it is a bad approach for long term storage of data or for use in a context where you cannot guarantee consistency of the PHP installations reading the data.
The right way to fix the problem of reading session data outside of a user's session is to use the serialize/unserialize format:
ini_set("session.serialize_handler", 'php_serialize');
Then use unserialize() to read the data.
I am trying to share session information between our main PHP app and node.js
We currently use a database backend for session storage, because we have several web servers that can handle a request.
Is anyone aware of any solutions for accessing session data from node.js? The only ones I can see use redis or memcache, but we cannot change the method for storing session data.
You would have to use a common format, such as JSON or one of your own invention. Let's assume JSON for convenience.
On the PHP side you will need to register your own session handler.
Set session.name to something less PHP specific, for example SESSID instead of PHPSESSID.
Set session.serialize_handler to php_serialize. In the write() method cast the incoming $data into JSON by first calling unserialize() on it, then calling json_encode() and set the re-encoded data in your database.
Similarly, the read() method should extract the session data from your database (given the value of the ID stored in the SESSID cookie), call json_decode() on it, then serialize it and return that string.
Might I suggest a fast, scalable, and reliable database such as Aerospike. I am, however, biased.
I'm using JS Objects to sort and filter a table but I need to store the original table data in case the user wants to return to it. I'm just wondering if there's an easy way to pass the object's data to a PHP session var, via AJAX, and retain stuff like the key/value relationships without doing a lot of heavy lifting.
Build a JSON-string of the object and store this string.
JSON.stringify
It's not possible.
On the client side, the session is identified by a cookie. The session ID is all you've got.
On the server, the session data is stored in a binary file, one file per session (typical scenario).
So you can't touch the contents from the session from the browser without help from the server.
And second: it's not "light". In an app I've been working on, modern computer and virtually no load yet, loading the session seem to be taking around 250ms, 10 times longer than the response time for the whole page when starting from scratch.
In summary:
you cannot touch session variables without writing a script on the server that handles the data
It's not "light".
OK Ive written this neat javascript 'thing' using jquery and ajax. Its all based on the idea that a div has an attribute that lets you write inside the div. (contenteditable=true). I thought it would be cool to make a chatroom type thing out of it, and holy cow its doing some cool stuff(*), but I have an issue.
Using ajax I post to a php page that takes the posted data (x,y, text, id) and stuffs it into a JSON-like object. Without writing to a database (overkill I think), how can I make this data persist? See the problem? : The variables in a php page are essentially vapor after the page has ran, so my javascript ajax call to retrieveNewJSON() would find nothing.
*using jquery effects and setting colors I have variably placed text that scrolls and evaporates, matrix style, for example. Also, a cursor is placed in the div where the user clicks.
You have to store the data somewhere. If you don't want to use a full blown database you can store them in flat files (ie: txt) and use PHP's file functions to handle the files.
Of course this is not very scalable, and I'd strongly recommend using a database if you are going to be using this a lot.
You could use cookies (client-side) or session variables (server-side), or you could write to a file for longer-term storage.
You could use a the $_SESSION variable to persist data.
// Call at start of PHP script
session_start()
//....
// Store object
$_SESSION['obj'] = json_encode(obj);
in your pull script:
// Call at start of PHP script
session_start()
// Retrieve object
echo $_SESSION['obj'];
Note that when using sessions you have to make sure that you call session_start() at the top of every php script that uses the session.
I would not recommend trying to store this in a file unless you are supporting a very low number of users and have taken proper data sanitation steps to physically write files to the server. If you need this to persist past the length of a session you should be using a database.
It is worth noting that you can't update a users session without some other form of centralized storage. Unless you have some sort of long-polling / comet type setup you will have to have some sort of central storage place. Something I would take a look at would be memcache.
If you want to avoid using a database engine (which would have a lot of overhead for a multiple-read, multiple-write app like a chat room anyway), you might look at a simple object store like memcache, couch, or mongo. Files are also a valid option, provided you store them outside of the Web root with proper permissions. Bottom line is, you'll have to use some sort of storage engine on the back end in order to make the data shareable across multiple user sessions.
If this is simply a tech demo or a proof of concept, I wouldn't worry too much about overhead right away.
Is there any way to read Session value through JQuery?
Edited:
I am calling a .php file using JQuery. The .php file stores some column values in a session.
What would be the right approach to return those column values to the calling JQuery function?
In PHP, something like (from memory as I've not PHP'd for some time...)
<input type="hidden" id="SessionValue" value="<?php echo $MySessionValue ?>">
Then in jQuery
$("#SessionValue").val();
Sure you can set up a service which serves the Session values in JSON, and then use $.getJSON. But to read it directly is impossible.
Session values are stored on the server, JQuery is a client-side library that runs in the browser. Unless you send the session value down to the client, JQuery won't be able to read it.
I assume that you're referring to a server-side session in ASP.Net or PHP.
Not directly.
However, you could make an AJAX call to server-side code that returns something from the session.
If you do, beware of information disclosure.
Session values can only be read on server-side. Still if you are really hard-bent on what you want to do, you can write a Ajax enabled web-method in your code-behind that responds with session value for the given key as argument. You can call this webmethod from JQuery and retrieve the session value!
The ways sessions are associated with a client is by using cookies. That's where maybe the confusion takes place.
But the session data themselves are stored on the server.
Your backend need to send your session values somehow to your page for jQuery to pick it up.