Drill down to the PHP data cache instructions

  • 2020-06-03 05:59:17
  • OfStack


<?php
// https://www.ofstack.com/article/23093.htm
function set_cache($name, $value) {
    //  Set relative or absolute directories without adding at the end  "/"
    $cache_dir = "./cache";
    //  Set the extension 
    $cache_extension = ".php";

    $cache_str_begin = "<?php\n//Cache Created at: " . date ( "Y-m-d H:i:s" ) . "\n";
    if (! is_array ( $value )) {
        $cache_str_middle = "\$$name = \"$value\";";
    } else {
        $cache_str_middle = "\$$name = " . arrayeval ( $value ) . ";";
    }
    $cache_str_end = "\n?>";

    $cache_str = $cache_str_begin . $cache_str_middle . $cache_str_end;

    //  Cache file path 
    $cache_file = "$cache_dir/$name$cache_extension";
    if ($fp = @fopen ( $cache_file, "wb" )) {
        fwrite ( $fp, $cache_str );
        fclose ( $fp );
        return true;
    } else {
        echo $cache_file;
        exit ( "Can not write to cache files, please check cache directory " );
        return false;
    }
}
//  will array Into a string ,  from discuz!
function arrayeval($array, $level = 0) {
    if (! is_array ( $array )) {
        return "\"$array\"";
    }

    $space = "";
    for($i = 0; $i <= $level; $i ++) {
        $space .= "\t";
    }
    $evaluate = "Array\n$space(\n";
    $comma = $space;
    if (is_array ( $array )) {
        foreach ( $array as $key => $val ) {
            $key = is_string ( $key ) ? "\"" . addcslashes ( $key, "\"\\" ) . "\"" : $key;
            $val = ! is_array ( $val ) && (! preg_match ( "/^\-?[1-9]\d*$/", $val ) || strlen ( $val ) > 12) ? "\"" . addcslashes ( $val, "\"\\" ) . "\"" : $val;
            if (is_array ( $val )) {
                $evaluate .= "$comma$key => " . arrayeval ( $val, $level + 1 );
            } else {
                $evaluate .= "$comma$key => $val";
            }
            $comma = ",\n$space";
        }
    }
    $evaluate .= "\n$space)";
    return $evaluate;
}
$test_array = array (
        "6b" => "a\\",
        "b",
        "c",
        array (
                "c",
                "d" 
        ) 
);
$fileAndVarName = "newFile";
//  In the generated $encode_str when , To keep the original character format in the string unchanged, the system prefixes predefined characters in the string at compile time  \  Keep the predefined characters in the string, but when you print or print the string, only the predefined characters will be printed, not the ones before the predefined characters  \
$encode_str = json_encode ( $test_array );
//  Because we're going to print out the string PHP Code, when the output, the string of predefined characters will disrupt the operation of the program, so the original escape character before the escape character is added, so that the string output before the escape character can also output 
$addslashes_str = addslashes ( $encode_str ); // addslashes Prefixes predefined characters in a string  \  So that it can be stored in a string without any effect, without participating in the program 
echo stripslashes($addslashes_str); //  An anti-escape function that removes backslash characters from a string. If it is a continuous 2 A backslash, get rid of it 1 A, leaving 1 A. If only 1 I'll just get rid of the backslash. 
echo "<br>";

//  Array objects can be passed or converted json String, converted to json String, which needs to be converted to an array when used 
set_cache ( "$fileAndVarName", $addslashes_str );
var_dump ( $addslashes_str );
echo "<br/>";
include_once "./cache/$fileAndVarName.php";
var_dump ( $$fileAndVarName );
echo "<br/>";
$decode_arr = ( array ) json_decode ( $$fileAndVarName );
var_dump ( $decode_arr );
echo "<br/>";
 
//  Cache the other 1 In a way, with serialize The array sequence number into a string, stored in any extension file, when used fopen Open to read the contents of the string, then use unserialize Deserialize to raw data 
$serialize_str = serialize ( $test_array );
echo $serialize_str; //  This is the array that was described but here it is 1 It's just a string 
echo "<br/>";
$unserialize_str = unserialize ( $serialize_str ); //  Restore the described data 
var_dump($unserialize_str); // Reduction to become  $test_array  The array structure is not lost. 
?>


Related articles: