问题
I am going to improve a web site in Apache and PHP which has a page with a table containing a list of files. My goal is to allow the user to set one of those files as the “important” one based on some specific and subjective criteria. In order to do that, I want to store the information regarding the most “important” file in some way, with the restriction that I am able to use neither databases nor files (constraints imposed by supervisor).
My questions are:
- Is it possible?
- How can I do this?
I already did a search in this site, but I did not find an answer.
EDIT: By the way, finally I solved my problem using an XML file. Thanks a lot to everyone.
回答1:
Assuming these criteria are client-side rather than server-side, because if they're server-side and it's supposed to be one 'important' file for all users then there's no way to do this without storage.
The supposed answer to your solution is localStorage()...
It's Javascript dependent and definitely not a perfect solution, but HTML5 localStorage allows you to store preferences on your users' computers.
First, detect support for localStorage():
if (Modernizr.localstorage) { // with Modernizr
if (typeof(localStorage) != 'undefined' ) { // Without Modernizr
Then set a parameter if it's supported:
localStorage.setItem("somePreference", "Some Value");
And then later retrieve it, as long as your user hasn't cleared local storage out:
var somePreference = localStorage.getItem("somePreference");
When you want to clear it, just use:
localStorage.removeItem("somePreference");
For those using unsupported (older) browsers, you can use local storage hacks abusing Flash LSOs, but those are definitely not ideal.
What about sessions or cookies?
Both of these are intentionally temporary forms of storage. Even Flash LSOs are better than cookies for long-term storage.
The constraint is literally encouraging poor practices...
All of these options are browser-side. If the user moves to another PC, his/her preferences will be reset on that PC, unlike with a database-powered authentication system where you can save preferences against a login.
The best way to store this kind of data is in a database. If you can't run a database service, you could use SQLite or store the data in JSON or XML files.
回答2:
Cookies might be helpful. I can't think of any other secure way. This isn't too graceful either.
http://support.mozilla.org/en-US/kb/cookies-information-websites-store-on-your-computer
回答3:
You can persist data temporarily using sessions and cookies. They don't require files or database; However, as mentioned, it is temporary because the data is gone when the browser is closed or the cookie expires.
If you are told to persist data permanently without the use of files or database, your supervisor has already set you up to fail.
Hope this helps.
回答4:
The only options I can see would be to either use cookies (which aren't permanent and are stored as files on the client).
Another option may be to use something like Parse which is allows you to store data in the cloud.
It depends how strict your supervisor is however since the first solution uses (client) files and the latter will be using a database on another server.
回答5:
You could try using a cookie to store the data
To set the filename use:
<?php
setcookie("important", $importantFileName);
?>
To read the data use:
<?php
$importantFileName = $_COOKIE["important"];
?>
But this wil only work for the current user and other users wont be able to view this.
来源:https://stackoverflow.com/questions/17876424/php-store-information-with-no-database