Entry
I want to create an array that I can access from a various number of PHP scripts, is there any way I can do this without using the post and get form methods?
May 19th, 2000 03:31
Scott Weese, http://www.php.net
There is a way that you can do this.. But it uses Cookies..
So it REALLY requires that you do not print/echo any HTML code or HTML
headers until you set the cookie.
Here is the sample code for it..
When you get ready to set the cookie for the array all you have
to do is this..
setcookie ($cookiename, serialize(urlencode($arraytosave)));
Also Remember, When you use this method of saving a cookie, if you
want ALL your pages in your website to be able to access it,
you MUST set the path of the cookie to the $DOCROOT of the site.
The reason for this is because if you have a script in a subdirectory,
when you set cookie information in that script, if the cookie exists,
the value will only be active for that script and time. It will NOT
overwrite the final resulting value for the rest of the scripts on the
site. You can Just simply remedy this by using the following code:
setcookie ($cookiename, serialize(urlencode($arraytosave)), "/");
or
setcookie ($cookiename, serialize(urlencode($arraytosave)), $DOCROOT);
When you want to retrieve the array just use the following line..
$cookiename=unserialize(urldecode($cookiename));
You can also use this method with Classes and Objects..
Remember, you can only use this method with Classes in Php4.. I forgot
to mention that..
There is also one more way of performing this global array definition.
You can do it using and include or require statement.
just make an include file to hold array definition and make all of your
scripts that you want to be able to access this variable include or
require that script file.. But you can't change a value and expect
another script to see that change unless you have included that script
into the currently processing one also.. Thats why the cookie example
is sitting above this one.. =)