0

I'm creating a page for myself that could be accessed without internet connection (local storage only).

I want that page to somehow store data (that I put in the website) on my computer.

I've heard there are ways to edit .txt files with a help of php? Also maybe Chrome could somehow save that info easier?

Appreciate any help

EDIT: I want a fast and easy access to a website via Chrome only, so I prefer not to be using XAMPP or any other software.

Smelis
  • 33
  • 7
  • [PHP](http://php.net/), [XAMPP](https://www.apachefriends.org/index.html), [MySQL](http://www.mysql.com/). – Celeo Sep 24 '14 at 21:03
  • I want a fast and easy access to a website, so I prefer not to be using XAMPP – Smelis Sep 24 '14 at 21:04

3 Answers3

2

The easiest way would be to use HTML5's localStorage (no server-side languages needed), but it won't be easy to get that data outside of your page (I understood you'll be using that offline page which has stored data).

It's as simple as:

window.localStorage.setItem('myItem', 'Hello World');

And then to get it, you'd just do:

window.localStorage.getItem('myItem');

Array approach works as well (localStorage.myItem, etc.).

Read more about it here and here.

Here is a simple example from above: http://jsfiddle.net/h6nz1Lq6/

Notice how the text remains even after you remove the setter line and rerun the script (or just go to this link: http://jsfiddle.net/h6nz1Lq6/1/).

The downside of this approach is that the data can easily be cleared by accident (by clearing browser/website data, but again this is similar to accidental deleting of a file, so nothing to be afraid of if you know what you're doing) and that it doesn't work across browsers (each browser stores its own localStorage).


If you still decide to use a server-side language, there are millions of tutorials about them. For a beginner, it would probably be the easiest to use a simple PHP script to write a file, but that would require using a server on your machine.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • I'm writing this into my script: window.localStorage.setItem('myItem') = "Hello World"; document.getElementById("box1").innerHTML = window.localStorage.getItem('myItem'); but it doesn't seem to work. What am I doing wrong? I'm really new here.. – Smelis Sep 24 '14 at 21:12
  • Oh so it's required to run a server if I want to work with other .php files? – Smelis Sep 24 '14 at 21:15
  • Sorry, sorry, gave you a bad syntax. Fixed it, see here: http://jsfiddle.net/h6nz1Lq6/. Yes, PHP needs a server to run (Apache, nginx, etc.). – Shomz Sep 24 '14 at 21:16
  • By the way, how safe am I with data I'm saving? I mean, in what ways the data could be lost being saved this way? – Smelis Sep 24 '14 at 21:21
  • You're welcome! Personally, I wouldn't be fully at ease if I stored something *really* important that way, probably because that technology is still young and everyone is used to databases and files. But other from wiping it by accident, you should be pretty safe. – Shomz Sep 24 '14 at 21:24
1

PHP example:

<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>

Taken from http://www.w3schools.com/php/func_filesystem_fwrite.asp

Microsis
  • 279
  • 1
  • 2
  • 11
  • Your example is fine, but that's not the best site to recommend to a beginner, see this: http://www.w3fools.com/ (ah, now I see they removed tons of bad examples from w3schools) – Shomz Sep 24 '14 at 21:12
0

You can read and write directly to storage with PHP or use a database for i/o. Check in PHP+MySQL for a common solution and use file upload with HTML or textarea field for plain text.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424