Save PHP variables to a text file

后端 未结 7 1569
梦如初夏
梦如初夏 2020-11-29 00:59

I was wondering how to save PHP variables to a txt file and then retrieve them again.

Example:

There is an input box, after submitted the stuff that was writ

相关标签:
7条回答
  • 2020-11-29 01:53

    (Sorry I can't comment just yet, otherwise I would)

    To add to Christian's answer you might consider using json_encode and json_decode instead of serialize and unserialize to keep you safe. See a warning from the PHP man page:

    Warning

    Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.

    So your final solution might have the following:

    $file = '/tmp/file';
    $content = json_encode($my_variable);
    file_put_contents($file, $content);
    $content = json_decode(file_get_contents($file), TRUE);
    
    0 讨论(0)
提交回复
热议问题