Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Variable

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How can i set a variable in PHP to work on many php pages without loosing its value ?
i tried the require methode but i'm loosing the variable value each time i moove to another php page.
thanks for your help
 
Well, there are a number of ways, but a common way, is to use sessions. (
session_start();

$my_name = "Chad";
session_register("my_name");

and then in every other page where you want to use this, you just place

session_start();

at the very top of the script and then whenever you want to use that variable, you just call it by $my_name.


You can also auto prepend a script to every script that is executed. This you should read about as well, but is less common and can add overhead to your applications.

Chad. ICQ: 54380631
 
It can also be done with cookies. However, the structure from inlandpac is much better.
Session management also works with cookies, but u can register more variables. mcvdmvs
-- "It never hurts to help" -- Eek the Cat
 
Keep in mind that sessions utilize "server" cookies in such a way that not only is a cookie stored (in memory) on the client side which ONLY contains the name and session id, a session file is stored on the server which contains the actual data matching the name and session id.

Cookies still have to be enabled in the client in order to use sessions, but there are even ways around this.

Chad. ICQ: 54380631
 
Is this variable an application-level variable, such that you need the same variable to be available to multiple users at the same time?
 
The variable is not available for all users, unless u give the same var with the session stuff. U cannot change the var at that time for all users at the same time.
(Back to you Chad) mcvdmvs
-- "It never hurts to help" -- Eek the Cat
 
very true, mcvdmvs.

session vars have an identical scope to setcookie() vars. They are user specific.

It is impracticle for session and cookie vars to be available to all users because we can do this just by setting the variable within a global include script or something.

Example (obvious, but good for newbies):

Let's say that a user has registered on our site and in that registration, the told us that they wanted to see the date on their start page to be in m-d-Y format. By default, we have set the date format to be Y-m-d.

As a result of the users registration and subsequent login, a session variable was set called 'my_date' using:

$my_date = date("m-d-Y");
session_register("my_date");

filename: global.inc.php
<?php
/*
initiate the session so we can
access any registered session variables.
*/
session_start();

/*
an application-level variable
(one that is accessible to everyone
since we are setting it within the script
as a generalized variable (no special
parameters, access, etc.). Basically, it
is global in the scope of this script and all
scripts that include this one.
*/
$date = date(&quot;Y-m-d&quot;);

/*
if session registered variable 'my_date'
exists, reassign $date to $my_date
*/
if($my_date) {
$date = $my_date;
}
?>

Now, whenever we have a script that we want to access $date, we can use

(for PHP >= PHP 4.0.1pl2):
include_once($DOCUMENT_ROOT.&quot;/globals/php/global.inc.php&quot;);
(for others):
include($DOCUMENT_ROOT.&quot;/globals/php/global.inc.php&quot;);

A session now has already been initiated, we have determined that the user-level variable $my_date is set, and we have reassigned $date with $my_date.

So, in conclusion, session variable and cookie variable data are strictly user-level accessible because to do otherwise would be redundant and pointless; we can just set a variable within our code somewhere and access it globally (if done right).

Hopefully, I have made sense here and someone has gotten something out of this. ;-)

Chad. ICQ: 54380631
online.dll
 
aldumil,

This is surely another method...BUT, it is extremely insecure and inefficient.

For users who only have access to older versions of PHP and have very limited access to functionality, your method is really the best one.

Heck, if you have access to MySQL and are on one of these older servers, you can just pass a unique session identifier in the url, on each page, query a MySQL database->table for the existence of that identifier, grab data (if it exists) and use the data, etc.

Chad. ICQ: 54380631
online.dll
 
I agree with inlandpac.

My question about whether it was an application-level variable was meant for the orginal poster. I was just trying ot understand whether he or she needed an application-level variable.

Are yuo still reading this thread, Abouelmourouk?

I know there are several ways to set application-level variables. However, if you want to be able to change that variable quickly, but still make it available to multiple users, there are a couple of interesting ways to do this. If interested, please reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top