467,134 Members | 968 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,134 developers. It's quick & easy.

PHPs version of ASPs application scope variable

I found a good way to do that:
<?php
function application_start (){
global $_APP;
global $shm_id;
$shm_id = shmop_open(0xff3, "c", 0644, 10000);
$shm_size = shmop_size($shm_id);
$data = shmop_read($shm_id, 0, $shm_size);
$_APP=$_APP = unserialize($data);
}

function application_end (){
global $_APP;
global $shm_id;
$data = serialize($_APP);
shmop_write($shm_id, $data, 0);
shmop_close($shm_id);
}
?>

Then in the program:
<?php
application_start();
..
.. use $_APP to store the application scope variables
..
application_end();
?>


for a larger room to store the variables: (but slower)
<?php

define("APP_DATA_FILE",
"/tmp/application.data");

function application_start (){
global $_APP;
// if data file exists, load application
// variables
if (file_exists(APP_DATA_FILE)){
// read data file
$file = fopen(APP_DATA_FILE, "r");
if ($file){
$data = fread($file,
filesize(APP_DATA_FILE));
fclose($file);
}
// build application variables from
// data file
$_APP = unserialize($data);
}
}

function application_end (){
global $_APP;
// write application data to file
$data = serialize($_APP);
$file = fopen(APP_DATA_FILE, "w");
if ($file){
fwrite($file, $data);
fclose($file);
}
}
?>

Here is an example using Application variables to communicate with different users.
A drawing board online:
http://pighouse.webonlive.com/jason/drawingboard/

source:
http://pighouse.webonlive.com/jason/drawingboard.zip

u can use different username or ip, and every user online at the same time can see other users' drawing.
Feb 11 '07 #1
  • viewed: 1505
Share:

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

33 posts views Thread by aa | last post: by
4 posts views Thread by Keith Chadwick | last post: by
3 posts views Thread by mydejamail@yahoo.co.uk | last post: by
3 posts views Thread by Disco Octopus | last post: by
3 posts views Thread by Lucanos | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.