2

i have a project of nearly consisting of 10-15 session vars. so now i want know how much size does they occupy but i never find a way... so how to know how much space they occupy.? or else what are the factors affecting ..? and in best ,avg, worst case scenarios..? what if in the case of session multidimensional arrays?

if not may know how much space does this occupy..? how to reduce it?

<?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);

?>

thank you... :)

mohan
  • 115
  • 1
  • 1
  • 9
  • See [How to determine the memory footprint (size) of a variable?](http://stackoverflow.com/questions/2192657/how-to-determine-the-memory-footprint-size-of-a-variable). – George Cummins Jun 07 '13 at 17:16
  • You could use your own custom session handler and save them in your database, that way can see what filesize they take in. http://php.net/manual/en/function.session-set-save-handler.php – edwardmp Jun 07 '13 at 17:20
  • When attaching code, please remove the commented lines as they are not executing and do not bring anything. – ssedano Jun 08 '13 at 08:39

1 Answers1

2

By "storage" do you mean ram while the script is running or disk space after the script writes?

With ram, they won't take any more than any other variable.

As for disk, they will take as much data as you put in them. You can get the session_save_path() (if it is blank, php defaults to your system temp directory which you can get with sys_get_temp_dir()) and see them for yourself. They usually have the name sess_[SESSION ID HERE] (my current session file on our development server is named sess_d22ivcfi1nqqob3gm7v7b1m7sdtabja8). You can also just call session_encode() to get a string of what is written to a session file. The strlen of that will be the same as the filesize in bytes.

<?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));
Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • thank you ... OK for that , i was getting problem with optimizing the code so that i can reduce some burden on my server, how to do that? – mohan Jun 07 '13 at 17:48