jondinham
jondinham

Reputation: 8511

How to create a shared object between different sessions in PHP?

How to create a shared object between different sessions in PHP?

I'm thinking of using file or MySQL memory table. Using file isn't a good option because it doesn't have locking and is slow. Using MySQL memory table is a good option, but how to save class instances (objects) to a table? Serializing an object and put it to table is also slow.

Option 1: MySQL memory table
Option 2: shm_attach,shm_detach,shm_get_var,shm_has_var,shm_put_var,..
Option 3: Memcache

The problem is using MySQL memory table requires querying. Memcache is not included in standard PHP installation. To have shm_* functions on Windows, it's required to get PHP built from source with option "--enable-sysvsem", and this requires setting in php.ini where developer may not be able to access all the time.

Which one of the above is the better? Any other options?

Upvotes: 5

Views: 1129

Answers (4)

HMR
HMR

Reputation: 39280

I don't see apc mentioned:

http://www.php.net/manual/en/book.apc.php

Not sure if it's within one session as the manual lacks any info on what it does but in other posts I see at alternative to memcache.

I am looking for a simular solution to storing page templates that might have to be used a lot and storing authenticated users in an in memory table instead of using php session (the second part I'm not so sure of).

Upvotes: 1

jondinham
jondinham

Reputation: 8511

Memcache is not in standard PHP installation. Semaphore & shared memory functions are not supported on Windows.

Most likely the only solution is using MySQL memory table with object serialization.

Upvotes: 1

hakre
hakre

Reputation: 197832

If you want to share objects (instances of classes) between different processes, you will always be bound to serialize and unserialize regardless which kind of storage layer you use (database, memchace, files, ...).

If you don't want to use serialize and unserialize, then there is not much that you can do.

Upvotes: 1

Shattuck
Shattuck

Reputation: 2780

I remember seeing a similar problem with a solution in the development of eyeOS.

I know you are not exactly to exited about using a file, but what if you were to store the variable(s) you want to share in xml format.

If you want to have it specific to certain sessions you could use unique tokens(a password of sorts) for each set of sessions and set up a controller that directs requests to the correct session xml file based on the token.

For security you could store the xml info in a php file and only allow information to be retrieved by POST using the correct token.

This method would allow you to securely access, edit, and delete(destroy) shared sessions.

Upvotes: 1

Related Questions