5

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.

pafede2
  • 1,626
  • 4
  • 23
  • 40
  • 24
    Your supervisor is an idiot if he expects you to persist data without actually saving it anywhere. – Martin Bean Jul 26 '13 at 08:25
  • 4
    Please make sure you make your supervisor read the above comment – Mr. Alien Jul 26 '13 at 08:27
  • How about using Local Storage on the client? – peterm Jul 26 '13 at 08:27
  • i guess you may achieve this by saving information in files(preferably some structured language files like XML),because database itself is internally some structured file directory but the security is the issue if you go with this. – San Jul 26 '13 at 08:28
  • 3
    You might want to find a different supervisor .. – tereško Jul 26 '13 at 08:29
  • I would say cookies or localStorage; cookies might be easier because they're part of the request automatically. – Ja͢ck Jul 26 '13 at 08:33
  • 4
    I expect there is a misunderstanding here somewhere - ask the supervisor (work supervisor? or teacher?) which means of storage are acceptable. Is it supposed to be user specific? or persistent? Nobody is likely to willingly ask you to do something that's not possible, unless that's the point of the task - for you to conclude/say it's not possible. – AD7six Jul 26 '13 at 08:36
  • 2
    Yes, your supervisor is a fool. First, get yourself a job where your 'supervisor' actually ***wants*** you to better yourself, but in the mean time, either use sessions / cookies... or memcache / apc (warn the idiot that once the server is restarted, the list of importants are gone). – Jimbo Jul 26 '13 at 09:12
  • Actually, since what we need to store is a really small amount of information my supervisor posed me a kind of challenge trying to do it using the minimum resourses possible, to start with we were wondering if we could solve our problem with no database nor files, then we realized it was not possible, with this I expect to make the misunderstanding clearer. – pafede2 Aug 01 '13 at 13:54

6 Answers6

4

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.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
  • Thanks a lot to everyone for your suggestions! Since it is a client side decision which must be propagated to the server side I guess that the answer to my questions is that it is not possible if I am not able to relief the constrints. Thanks! – pafede2 Jul 26 '13 at 08:56
2

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.

0

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

Bhargav Ponnapalli
  • 9,224
  • 7
  • 36
  • 45
0

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.

Kneel-Before-ZOD
  • 4,141
  • 1
  • 24
  • 26
  • Sessions are typically persisted with either files or a database. Of course, an in-memory database could be used. – Ja͢ck Jul 26 '13 at 08:32
  • I know; those files are created by the system and not the OP; so, in a way, he's still complying with the requirements. – Kneel-Before-ZOD Jul 26 '13 at 08:33
0

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.

Jim
  • 22,354
  • 6
  • 52
  • 80
0

If data doesn't need to be writable with php, php arrays/"associative arrays" can be used, performance should be better than JSON or XML

Example:

Data inside file, storage/php_array/pages/contact.php

<?php
return [
    'title' => 'Test Title',
    'desc' => 'Test Description',
    'heading' => 'Test Heading',
    'html' => <<<'vKf4BpA7en9' // using nowdoc
<div class="form">
</div>
vKf4BpA7en9
];

Usage inside file, index.php

$dataInArray = include 'storage/php_array/pages/contact.php';
proseosoc
  • 1,168
  • 14
  • 23