Jump to content

[SOLVED] Is there any way to persist objects between php pages?


dead_elves

Recommended Posts

I've tried adding variables to the $_SERVER variable, to no avail. It accepts an assignment just fine, but doesn't recognise the key on the following page.

 

I know web servers are stateless, but there are mechanisms in other languages that perform this kind of function (the SERVER variable in ASP, for example, if I remember correctly) so I'm hoping I just missed something in the manual.

 

test01.php:

-----------

<?php

$_SERVER["myobject"]="steve";

?>

 

test02.php:

-----------

<?php

echo $_SERVER["myobject"];

?>

Link to comment
Share on other sites

sessions/cookies won't work (I think...), since what I want to store should be persistent across all sessions. Basically I want to dynamically build a menu structure from a flat file and directory structure. I could do this on every page call, but that just seems like a huge waste.

Link to comment
Share on other sites

Create a config file and include it on all pages, and define those variables or constants there.

 

includes would only cut down on the amount of typing and maintenance of the site - it would still have to be reprocessed on every call.

 

global array

 

Wouldn't that just affect the scoping on the page itself?

 

Basically I want to build the content menu dynamically from a directory-and-file structure. This structure won't change often though, so it seems extremely ineffecient to me to rebuild it on every page call. I've toyed with the idea of storing the structure as an xml file and rereading that, but as I understand it, xml processing is almost as top-heavy as stepping through my directory structure would be.

Link to comment
Share on other sites

Edit: If you're saying this data will be changed by one user, and all users should then see the changes, use a database.

 

At this point a database call does seem to be the best choice out of a bad bunch as far as efficiency goes, but I need to retain the directory structure because other applications will be accessing the data directly. Normally I would use servlets to build this type of site, but client constraints don't allow that this time, and I'm kinda stuck.

Link to comment
Share on other sites

Actually the config file is probably the fastest method.  But the config file need not re-scan the structure every time it's called.  The config file can be the product of the structure scanning, and can simple contain php variable definitions.

 

Re-creation of the config file can be triggered any time the structure changes.

 

The only way I can imagine that would improve on that is some kind of shared memory cache, which would avoid the overhead of parsing the php variable definition and constructing it each time.  But I don't know of anything like that for php.

Link to comment
Share on other sites

The config file can be the product of the structure scanning, and can simple contain php variable definitions.

 

That's not a bad idea. As I understand the PHP session tracking subsystem, session variables are stored in a similar manner. One would still have to come up with a mechanism to keep the data synchronised, but I suppose you could do that by adding a synchronisation counter of some sort that just polls the directory every now and again for changes.

 

Obviously the ideal would be the ability to pass objects around from page to page, but within the limitations php, asp et al. have, this rocks. Thanks!

Link to comment
Share on other sites

Create a global include file where you can create the object. Serialize the object and add it to users session.

Now on in the same include file check if the object is not present then create the object otherwise just use it by unserliaze() the object.

This way you will create this object once per session.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.