如何知道“会话变量存储多少”在php中占用

时间:2013-06-07 17:14:38

标签: php session

我的项目几乎包含10-15个会话变量。 所以现在我想知道它们占用了多少尺寸,但我从未找到方法...... 那么如何知道他们占用了多少空间呢? 要不然 影响的因素有哪些? 在最好的情况下,平均最坏的情况..? 如果是会话多维数组呢?

如果没有可能知道这占用多少空间..?如何减少它?

<?php

session_start();

/*
if(isset($_SESSION['processing'])){
    if($_SESSION['processing']==1)
    {
    $_SESSION['processing']=0;
    }
    else
        {
            header('Location: search1.php');
        }
}
else
{
header('Location: search1.php');
}
*/





if($_SESSION['error']==0)
{
    if(isset($_SESSION['tresults']))
    {
        $text_results=$_SESSION['tresults'];
        if ($text_results<100)
        {   
            $_SESSION['error']=5;
        }
        else 
        {

            //sufficient results
        }
     } //      if no result was found
}
//$_SESSION['tresults']=0;






$bannedkeywords =  array("long list of keywords");

if(isset($_SESSION['searchedquery']))
{
$string=$_SESSION['searchedquery'];
}   
else
{
$string="something";
}   

$encode = array(
    '/(\d+?)\.(\d+?)/' => '\\1DOT\\2',
    '/(\d+?),(\d+?)/' => '\\1COMMA\\2',
    '/(\d+?)-(\d+?)-(\d+?) (\d+?):(\d+?):(\d+?)/' => '\\1DASH\\2DASH\\3SPACE\\4COLON\\5COLON\\6'
);


foreach ($encode as $regex => $repl) {
    $string = preg_replace($regex, $repl, $string);
}
preg_match_all('/\w+/', $string, $matches);
$decode = array(
    'search' =>  array('DOT', 'COMMA', 'DASH', 'SPACE', 'COLON'),
    'replace' => array('.',   ',',     '-',    ' ',     ':'    )
);
foreach ($matches as $k => $v) {
    $matches[$k] = str_replace($decode['search'], $decode['replace'], $v);
}
$bquery=count($matches, COUNT_RECURSIVE);
$bquery=$bquery-1;

$result = array_udiff($matches[0], $bannedkeywords, 'strcasecmp');

print_r($result);


$aquery=count($result, COUNT_RECURSIVE);


if($aquery==$bquery)
{
//query is clean 

}
else
{

$_SESSION['error']=6;
}



if(!isset($_SESSION['searched']['one']))
{
$_SESSION['searchednumber']=0;
$_SESSION['searched']['one']=Array();
$_SESSION['searched']['two']=Array();
$_SESSION['searched']['three']=Array();
$_SESSION['searched']['four']=Array();
$_SESSION['searched']['five']=Array();

$_SESSION['searched']['six']=Array();
$_SESSION['searched']['seven']=Array();
$_SESSION['searched']['eight']=Array();
$_SESSION['searched']['nine']=Array();
$_SESSION['searched']['ten']=Array();
}
else
{
$_SESSION['searchednumber']=$_SESSION['searchednumber']+1;
}

$compare=$matches[0];

foreach ($_SESSION['searched']['one'] as $k => $v)
{

$compare[0] = array_udiff($compare[0], $_SESSION['searched'][$k], 'strcasecmp');


}

$n=$_SESSION['searchednumber'];

        $p=$n%10;

$_SESSION['searched'][$p]=$compare[0];
echo "going well";
print_r($compare);

?>

谢谢你...... :)

1 个答案:

答案 0 :(得分:2)

“存储”是指脚本运行时的ram还是脚本写入后的磁盘空间?

使用ram,他们不会采取任何其他变量。

对于磁盘,它们将采用您放入的数据。您可以获取session_save_path()(如果它是空白的,php默认为您可以使用sys_get_temp_dir()获得的系统临时目录)并亲自查看它们。它们通常具有名称sess_[SESSION ID HERE](我在开发服务器上的当前会话文件名为sess_d22ivcfi1nqqob3gm7v7b1m7sdtabja8)。您也可以调用session_encode()来获取写入会话文件的字符串。其中的strlen将与文件大小相同。

<?php
//get the session id
$sessionID = session_id();

//get the session path
$sessionPath = session_save_path();

//if no path set in php.ini, defaults to temp directory
if($sessionPath == ''){
    $sessionPath = sys_get_temp_dir();
}

//build the session path string
$sessionFilename = $sessionPath.'/sess_'.$sessionID;

//display the session size
var_dump(filesize($sessionFilename));

//get the session encoded as a string
$tmp = session_encode();
//output the size
var_dump(strlen($tmp));