<?php
$arr=array();
function application($key,$value="")
{
global $arr;
if($value=="")
{
$shm_id = @shmop_open(12345, "a", 0644,100);
}else{
@$shm_id = shmop_open(12345, "c",0,0);
if(!$shm_id)
{
$shm_id=shmop_open(12345,"c",0644,100);
}
}
@$byte=shmop_read($shm_id,0,1024*1024);
if(!$byte){
return "";
}else{
if($byte!=""){
$arr=unserialize($byte);
}else{
return "";
}
}
if($value=="")//取值
{
shmop_close($shm_id);
if(array_key_exists($key,$arr))
{
return $arr[$key];
}
return "";
}else{
$arr[$key]=$value;
shmop_write($shm_id,serialize($arr),0);
shmop_close($shm_id);
}
}
?>
我在php中实现asp中的application对象的功能
目前的问题是在同一个http请求之中,执行了application("a","1"),那么application("a")会返回1,上面这个函数是正常的
但如果开始第二个请求了,application("a")就取不到值了
问题出在哪儿啊?
表示最大可能的空间(字节数)
而你 shmop_read($shm_id,0,1024*1024);
读取时就越界了,这就是所谓的内存溢出
//$p->a = 'abcd';
//$p->b = 1234;
print_r($p->_all);
echo $p->b;class shared {
private $shm_id;
private $shm_key = 0xff3;
private $shm_size = 1024;
function __construct() {
$this->shm_id = shmop_open($this->shm_key, "c", 0644, $this->shm_size) or die('申请失败');
}
function __get($name) {
$buf = shmop_read($this->shm_id, 0, $this->shm_size);
$buf = unserialize(trim($buf));
if($name == '_all') return $buf;
return isset($buf[$name]) ? $buf[$name] : false;
}
function __set($name, $value) {
$buf = shmop_read($this->shm_id, 0, $this->shm_size);
$buf = unserialize(trim($buf));
$buf[$name] = $value;
$buf = serialize($buf);
if(strlen($buf) >= $this->shm_size) die('空间不足');
shmop_write($this->shm_id, $buf, 0) or die('写入失败');
}
}
php_shmop 也可以在 linux 中编译,但他却是为 window 设计的