Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Store information with NO database

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.

like image 652
pafede2 Avatar asked Jul 26 '13 08:07

pafede2


1 Answers

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.

like image 89
Glitch Desire Avatar answered Nov 15 '22 07:11

Glitch Desire