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!

newbie question; global.asa equivalant 2

Status
Not open for further replies.

Niv3k

Programmer
Jul 11, 2001
350
US
I was wondering if there was some sort of file or something that would be the equivelant of an ASP "global.asa" file.

For those non-ASP people, that is just a global function file for things like checking session id's or special formatting functions, That can be called from any other pages in you web application.

Thanks, Kevin
 
So you're asking for some sort a default file in the configuration of the interpreter that contains modifications and additions to language defaults?

Ummmmm... I dont think there is anything like this in PHP, but I dunno why you would need something like this anyways.

What I usually do for each of my programs is include a vars.php or a secure.php or a functions.php in my index file, and call all subsequent files based on query strings passed to the index file... confused?

Code:
-[index.php]-
Code:
<?
include('vars.php'); 
  /***************
   all variables in vars.php are now available 
   for the duration of this scripts processing
                               ****************/
include('functions.php'); 
  /***************
   all functions in functions.php are now 
   available for the duration of this scripts 
   processing. these functions could even user 
   vars from vars.php if they were accessed 
   globally
                               ****************/


include('secure.php'); 
  /***************
   initializes session, checks for valid user, 
   throws error without valid user.
                               ****************/

switch($action)
{
 case 'foo':
  include('foo.php');
 break;

 case 'bar':
  include('bar.php');
 break;
}
?>

I always wanted to give one of the foo/bar examples ;-) !

Hope this kinda answered yuor question! -gerrygerry
Go To
 
Yeah, that's what I needed to know... I was hoping not to have to include all the files, but hey, as long as I don't need the user validation functions in every friggin page.

Thanks for the foo/bar example.

Kevin
 
I might have posted this code snippet before, so if I have, please forgive me.

I have been hearing for over a year now from frustrated former ASP developers who wished there was a simple way to set global variables, such as ASP's global Application object. This is not the same as a session variable , which is only availabe to a single user, but variables available anywhere, which PHP can get/set depending on certain events or values.

Of course experienced PHP users know there are several ways to accomplish the same thing, such as shared memory, parsing text files, or even a database.

But PHP does not wrap this up into a nice simple class with a couple of methods. This seems like a reasonable thing to want, so I have made a small &quot;starter&quot; class that allows for this, and could be extended in a few ways. For now it only writes to temp files, but anyone with a little work could get it to work with a database or shared memory. I have made this very simple, so it can be called with full object instantiation, or simply with a &quot;class::method()&quot; call:

Code:
<?php

/* filename: class.Application.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
?>

Example 1 -- setting an application variable:
Code:
<?php

require_once(&quot;class.Application.php&quot;);

$myvariable = &quot;Here is a test application variable.&quot;

if(!(Application::setVar(&quot;myvariable&quot;,$myvariable))){
 echo &quot;Couldn't set variable!!&quot;;
}

else{
echo &quot;Variable has been set!!&quot;;
}

?>

Example 2 -- retrieving an application variable:
Code:
<?php

require_once(&quot;class.Application.php&quot;);
if(!($appvariable = Application::getVar(&quot;myvariable&quot;))){
 echo &quot;Could not retrieve variable&quot;;
}

else{
 echo $appvariable;
}

?>

NOTE: since this at a very basic level at the moment, it does not take into account a few things to consider, such as where you decide to save your tempfiles. I recommend you set a non-standard directory, to avoid getting your files accidentally overwritten by other processes. Also, there is a security issue, if this is on a shared server, because others could read your tempfiles (unless this is on a system running PHP as a CGI, or PHP module in &quot;safe mode&quot;, with your tempfiles in your own directory tree). Of course, you could use some sort of encryption scheme if security is really important.

Also, with a little work, this class could be extended to check for all existing set application variables in a directory, getting and setting expiry times for these variables, etc... For ideas on how to do that, just check out PHP's filesystem functions:
Anyone, please feel free to post suggestions to add to this.

For those of you PHP'ers who are wondering what we are talking about, here is an example:

Let's say we have members-oriented website, where people are continually logging in, and performing whatever functions. Now, we want to set the site up so that anytime someone logs in, they receive a message announcing who was the last person to log in.

Normally, in PHP we would do that by saving logins to a table, and every time there is a new login, we read the login table, using date/time criteria to select the previous login information. For a large website, this can be an unnecessary overhead.

Instead, we could just set a certain variable every time someone logs in. User &quot;Fred&quot; logs in, so in PHP, we first run my class method Application::getVar(&quot;lastlogin&quot;); This tells us who the most recently logged in visitor is. Then, werun Application:setVar(&quot;lastlogin&quot;, &quot;Fred&quot;), to update that variable to &quot;Fred&quot; who is now the most recent. Meanwhile, other users browsing the website, will see the most recent login at the top of the page. &quot;User Fred just logged in&quot;, because we are retrieving that variable at the top of every page.

Using this method, we can get or set as many variables as we want, which can be seen accross the whole application, and we can do this without the fuss of inserting and querying a database.

Now, to expand on the above concept, we could actually set up the variable to be an array of users, and -- using the array_push method, we could just keep adding each new user to the end of the array. Now we have an array of all the currently logged-in users, with the most recent being at the end of the array. This could provide a nice way to manipulate interactions between users in a system.

class.Application is simply an easy syntax for setting values on an application level, without having to think about the mechanics. The next step is to set this up so it works with shared memory. Stay tuned ;-). -------------------------------------------

&quot;Now, this might cause some discomfort...&quot;
(
 
Well... err... umm... it kinda makes me wish I had better understood what a global.asa file was! Thanks for your thorough response rycamor! You are always good for a strong response! Everytime you post I give you a star!

I think I am gonna try and make a similar class for mysql... you never know, maybe I'll actualy make something worthy of a FAQ! LOL Meanwhile, rycamor, do you mind making a FAQ?

BTW, thanks for the signature article! -gerrygerry
Go To
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top