Mega Code Archive

 
Categories / Php / Algorithms
 

These 2 functions write and read the contents of a specially designated

multi-dimensional array to and from a text file <? Function read_session() { global $session, $session_path, $session_id; /* set up session variables */ /* What should we do if the script cannot find the session file? */ $fp = fopen( $session_path.$session_id, "r" ); while(!feof($fp)) { $line = fgets($fp,100); $tok = strtok($line, "="); $a = $tok; $tok = strtok( "="); $b = chop($tok); $c = UrlDecode($b); if((strlen($c) > 0) && (strlen($a) > 0)) { eval( "\$session$a = \$c;"); } } fclose($fp); return($fp); } Function write_session() { global $session_path, $session_id, $session, $sm_string_array; $level = 0; unset($sm_string_array); reset($session); /* Write array to session file */ $fp = fopen($session_path . $session_id, "w"); if($fp != "false") { write_session_loop($fp); } else { echo "<h2>Session Error " . $fp . ": Cannot Write to File!</h2><br>\n"; } fclose($fp); } Function write_session_loop($fp) { global $session, $sm_string_array; static $level=0; if(IsSet($sm_string_array)) { $num_elements_string_array = count($sm_string_array); } else { $num_elements_string_array = 0; } if($num_elements_string_array > 0) { $s=0; $string = ""; while(($s < $num_elements_string_array) && IsSet($sm_string_array[$s])) { if(gettype($sm_string_array[$s]) == "string") { $string = $string . "['" . $sm_string_array[$s] . "']"; } else { $string = $string . "[" . $sm_string_array[$s] . "]"; } $s++; } } else { $string= ""; } eval( "\$test_array= \$session$string;"); if(gettype($test_array) == "array") { $num_elements = count($test_array); } else { $num_elements = 0; eval( "\$value = \$session$string;"); $value_url = UrlEncode($value); $pair = $string . "=" . $value_url; fputs($fp, "$pair\n"); } eval( "reset(\$session$string);"); $x=0; while($x < $num_elements) { eval( "\$key = key(\$session$string);"); $sm_string_array[$level] = $key; $level++; unset($sm_string_array[$level]); write_session_loop($fp); // loop recursively!!! $level--; eval( "next(\$session$string);"); $x++; } } ?>