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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application variables?!

Status
Not open for further replies.

Kendo

Programmer
Jun 23, 2000
28
GB
PHP Newbie. Sorry.

Application variables are available in ColdFusion and I think ASP too...is there an equivalent in PHP?

In case you're not familiar with these types of variables, or possibly just not familiar with 'application' as a scope name for them, I'll describe what I need:

I need a variable that is setable (a word?!) and readable by all users across a server, but acts like a session variable so that it times out after non-use for, say 30mins.

Anyone know what I'm harping on about?

Thanks!
 
I understand what you are looking for since I came to PHP from ASP. I haven't found anything yet like the Application object along with its properties and methods. Also, in ASP the Application objects do not ever time out, only the Session objects have that.

The only thing that I can think of that might work for you are global variables, but that brings up security risks.
 
Okay then, well in that case, how fast is PHP at reading from a text file? Perhaps I could just drop what I need in a text file and have it read on every page? Not ideal perhaps, but a possibility. I would assume for something simple that storing it in a text file would be quicker than storing it in a database, but perhaps I'm wrong about that too.

Suggestions welcome.
 
I would think that would depend upon whether or not your making any other hits to a database. If you are, then another query should not have that much impact.

PHP is very fast with text files but then again, that is related to just how big the file is.

What are you trying to do and maybe we can come up with something better than reading a file in for every page.
 
If you are just trying to store a few variables then an include file would probably work as well.
 
Here is a simple starter class that you can use to approximate the functionality of ASP's setup. At the moment, I use temp files as the storage, but the storage can be changed easily, without affecting the functionality. Thus, if you want real speed, you could save the values in shared memory, or in MySQL HEAP tables, which stay in RAM.

Code:
--------- start class.Application.php ----------
<?php

/* Application class, includes methods to *
 * get set application variables by       *
 * storing serialized values to tempfiles */

class Application
{
  function setVar($varname,$value)
  {
    //$temp_path = 'C:\TEMP\\'; // for Windows server
    $temp_path = '/tmp/';      // for Unix server
    $strvalue = serialize($value);
    // generate a storeable representation of
    // the value, with it's type and structure

    $file_pointer = fopen($temp_path . $varname, 'w');

    if(!(fwrite($file_pointer,$strvalue)))
    {
      return false;  //success
    }

    else
    {
      return true;   //not able to write file
    }

  } //end function setVar


  function getVar($varname)
  {

    //$temp_path = 'C:\TEMP\\'; // for Windows server
    $temp_path = '/tmp/';      // for Unix server

    // file(path) returns an array, so we implode it

    if(!($strvalue = implode(&quot;&quot;,file($temp_path . $varname))))
    {
      return false; //could not get file
    }

    else
    {
      $value =  unserialize($strvalue);
      return $value;  //here's the value of the variable
    }

  } //end function getVar

} //end class Application

?>
----------- end class.Application.php ------

Now, all you have to do is make this file part of your standard include set, or define it's path in the auto_prepend_file directive in php.ini. Then, anywhere you want, you can simply do something like &quot;Application::setVar(&quot;varname&quot;,$value_or_object);&quot;. Then, on any other page, just do

$my_var_or_obj = Application::getVar(&quot;varname&quot;);

Or, for error checking:

if(!Application::setVar(&quot;varname&quot;,$value_or_object)
{
echo &quot;Could not save application var!&quot;;
exit;
}
else
{
echo &quot;Saved \$varname as application variable.&quot;;
}

One way to get a nice speed increase with this approach is to mount a RAMdisk, where you store these temp files.

On Unix systems, though, the filesystem approach is not too bad. Most Unixes have great methods for caching read/writes, so that you are not limited to physical disk speed. Also, in a server cluster, you could save the value to a shared network directory, or a shared database, so that other servers can read the value. Also, you can incorporate timeouts into this method simply by reading file &quot;last-access&quot; times.

Obviously, the functionality can be expanded in many ways. Give me your ideas, and I will see what I can do ;-).

I should point out that this method allows you to save scalar variables, arrays, and even objects. However, it doesn't save objects *together* with their class definitions, so in order to unserialize, and work with the object in another script, you need to also have the class code avalailable.

I suppose with a little more work, it might be possible to save the object AND the class, but that seems like unnecessary overload, and will involve on-the-fly eval()-ing of your class code. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-unknown F student
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top