Can I store data in a PHP file?
Is there any way of storing for开发者_运维技巧m-posted data directly into a PHP file, rather than storing it in a text or binary format?
Why would you want to store data in a PHP file over a TXT file? Really, if you're writing data to a file on the filesystem, it really doesn't matter what the extension is going to be. (Except, if you write to a PHP file and go to it in a browser, it's going to try to execute whatever data is inside the file and this could be a big security problem.)
My suggestion would be to write to a text file a value on each line. Then, when you want to read the data, use PHP to read that file back into an array.
Sure, you can save it into whatever type of file you want. Though putting it in a php file would be a major security risk, as someone could upload a script that does bad things, then launch it just as they would any other page on your site.
This is technically possible, but there is no good reason for doing so. What problem are you trying to solve?
You can open a PHP file for append, using fopen('file.php', 'a') and write something to it using fwrite. To read it, read the whole file, skip the PHP part and read the data you just wrote.
If you don't want to store your data (just be able to pass it from script to script), you could try storing your form as a session variable :
session_start(); $_SESSION['myData'] = $myData;Then all the other pages that share the session can acces the data just by calling
$_SESSION['myData']
.Yes, it is possible. If you want to do this, take a look at a function like var_export
.
Like everyone else says, however, it's for most intents and purposes a Bad Idea™. Writing things to an executable file means you'll have to be extra careful about user input, for instance, as they might exploit it to run their own code.
You should therefore strive to use a database or some sort of non-executable flat file for storing your data when possible.
In addition, if you try to do this for the sake of "security", where you put protective code in the file, it can be easily bypased by reading the file as a text file and just skipping over your security measures — remember, PHP code is plain text and doesn't have to be executed to be read. If you choose to encrypt it as your security measure, you can store encrypted data in a database as well, thus making your point moot.
If you find yourself without access to a database system, you should consider using a SQLite-database, which is stored in a file, and therefore requires nothing installed but the PHP SQLite extension, which comes as a standard part PHP.
There may be better way to store data, but if you really want this can be done this way:
file_put_contents($storage_file, '<?php return ' . var_export($data) . ';');
This way all data will be well formatted and well escaped.
Then you can retrieve the data with a code like this:
$data = include $storage_file;
Not in the PHP file itself. But you can simply store something using this script:
file_put_contents(dirname(__FILE__).'/data.txt', var_export($_REQUEST, 1), FILE_APPEND);
If you really want to store it directly in the PHP file itself, simply change the script
file_put_contents(__FILE__, '?>'.var_export($_REQUEST, 1), FILE_APPEND);
;-)
Yes.
http://php.net/manual/en/function.halt-compiler.php
精彩评论