samScripts.com
[Home] [My Scripts] [Contact] [Business Card Designer] [Donate] [$8.95 Domain Names]

Home > Scripts > application object

application object

... feel free to use this script for whatever you like.

application object

This is an experimental script that attempts to replicate (somewhat) the function of ASP's Application Object.

Description:

Application Object? What's all that then?


With Microsoft's ASP, your scripts can access what is called the application object. Basically, this allows you to set variables with values accessible from any script, which keep their
values after the script that sets them terminates.

How does this php implementation work?


At first glance, it seems like a simple problem to solve: just serialize your variables and save them into a database, or a file.

The one problem with this, is that if two scripts modify the information at the same, time, the data loses its integrity.

This simple script gets around this (i hope - tell me if I'm messing up somewhere) by using the flock function, and a simple rule:


Quote from a simple rule:
if you want to change a value, you must read it, modify it, and store it again before any other script can even read it.


Anyway, enough of my ramblings: there's an example of using it below, which you can see in action
here

Example:




<html>
<head>
<title>Application Object Example</title>
</head>
<body>
<div align="center">
<h3>The Application Object Example</h3>
For more information, see <a href="http://www.samscripts.com/scripts/application_object">this page</a>.<br><br>

<?php

include("applicationobject.php");

$app = new applicationobject("/your/path/to/store/datafile/in/appdat.app",false);

if( isset(
$HTTP_POST_VARS["message"]) && isset($HTTP_POST_VARS["name"])
    &&
trim($HTTP_POST_VARS["message"]) != "" && trim($HTTP_POST_VARS["name"]) != "" ){

    
$name = substr($HTTP_POST_VARS["name"], 0, 30);
    
$message = substr($HTTP_POST_VARS["message"], 0, 50);

    
$app->open("w");
    if( !isset(
$app->messages) ){
        
$app->messages = array();
    }
    if( !isset(
$app->totalmessages) ){
        
$app->totalmessages = 1;
    }else{
        
$app->totalmessages++;
    }
    if( !isset(
$app->firstmessagetime ) ) $app->firstmessagetime = date("Y-m-d H:i:s");

    if(
count($app->messages) > 29 ) array_pop($app->messages);
    
array_unshift($app->messages, array("name"=>$name, "message"=>$message, "date" => date("l jS F Y H:i:s")));

    
$app->close();
}else{
    
$app->open("r");
}

if( !isset(
$app->messages) ){
    echo
"No messages set so far...";
}else{
    for(
$i = 0; $i < count($app->messages); $i++){
        echo
'On <i>'.$app->messages[$i]["date"].'</i>, <b>'.htmlentities($app->messages[$i]["name"]).'</b> said:<br> '.htmlentities($app->messages[$i]["message"]).'<br><br>';
    }
    echo
"A total of ".$app->totalmessages." messages have been posted since ".date("l jS F Y H:i:s", strtotime($app->firstmessagetime)).'<br><br>';
}

?>
<form action="applicationexample.php" method="post">
<h4>Enter your name and your message</h4>
Name: <input type="text" name="name" value="">
Message: <input type="text" name="message" size="60">
<input type="submit" name="submit" value="Add Me!">
</form>
</div>
</body>
</html>



All that this example does is store the last 30 names, times, and messages posted on it. It also stores the date and time it first stored a message, and the total stored.

Not much use as is, but can easily be adapted to create a visitors online or other such script.

See what you can come up with...

Reference


Constructor:
$app = new application($appfilepath, $doze)
$appfilepath is the full filename of the file to store the data in. Make sure your script can write to it.
$doze should be set to true if testing on Windows 9x or any other OS that doesn't support flock().

Open the application object
$app->open($mode)
$mode is either "r" if you only want to read the variables, or "w" if you are planning to update them. If "w" mode is used, you need to call the close() function when you have finished. Other scripts will not be able to access the script while you have it open.

Close it
$app->close()

Getting / setting variables
$app->somevariable = $somevalue;
if( isset($app->somevariable) ) $somevalue = $app->somevariable;

Related Links
Files

applicationobject.php (2505 bytes) View Download
Viewed 1589 times. Downloaded 1644 times.

[Back] [Top] [Forward]

Powered by php, mySql.

Request processed in 0.00 seconds on Wednesday 26th September 2012 23:42:37.

©2002 Sam Yapp

What do you think of my resume wizard site? All nicely written in php :)