By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

PHPs version of ASPs application scope variable

P: 1
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
Share this question for a faster answer!
Share on Google+

Post your reply

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