/    Sign up×
Community /Pin to ProfileBookmark

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:
[url]http://pighouse.webonlive.com/jason/drawingboard/[/url]

source:
[url]http://pighouse.webonlive.com/jason/drawingboard.zip[/url]

u can use different username or ip, and every user online at the same time can see other users’ drawing.

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @jython spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...