Variable reading and writing in .txt file

I’ve only been using .php-script since today, so I very much hope you’ll forgive me for not immediately grasping the way it all works.

I’ve set up a website capable of using .php-script and it all checks out, it works.

However, I find it very confusing as to which functions to use to read and write variables, any guide I check seem to use different ones and are written differently, copy pasting it often doesn’t work.

So could somebody please show and explain to me the basics of writing and reading a text file with multiple saved variables? Let’s imagine a fictional site that whenever somebody loads a certain page, variable X gets increased by 1. I think I’ll be able to pick it up from there.

Thanks in advance

Here is a really basic hit counter.
[php]<?php
$hitsFile = ‘hits.txt’;

$hits = file_get_contents($hitsFile);
echo 'Hits before this visit: ’ . $hits . ‘
’;

file_put_contents($hitsFile, ++$hits);
echo 'Hits after this visit: ’ . $hits;[/php]

I would however suggest storing data in a database instead of files.

Thanks for answering my question, it certainly pointed me into the right direction.
It gave me errors saying I didn’t have the proper permissions, but I managed to fix that.

As you’ve suggested, I’m working on getting a database up and running properly.
Thanks again for your help, I’m sure it’s very basic to you but it really gave me a nice start into learning php :slight_smile:

In programming you generally have multiple methods of solving problems. Reading an entire file in one chunk may not be wise if it contains a lot of data. You can find tutorials online on how to read files line by line, or even one character at a time. For this use case though a normal read/write is acceptable.

For some reason I ended up making this. Figured I might as well post it, it could prove useful in learning something, I guess. heh

data.json
[php]{
“site”: {
“title”: “My site”,
“author”: “Yonnick”,
“hits”: 0
},
“computers”: [
{
“title”: “Desktop”,
“ip”: “192.168.1.100”
},
{
“title”: “Laptop”,
“ip”: “192.168.1.101”
},
{
“title”: “File server”,
“ip”: “192.168.1.102”
}
]
}[/php]

test.php
[php]<?php

function getStore () {
return json_decode(file_get_contents(‘data.json’));
}

function saveStore ($object) {
return file_put_contents(‘data.json’, json_encode($object));
}

$data = getStore();
++$data->site->hits;

saveStore($data);

?>

Welcome to <?= $data->site->title ?> (by: <?= $data->site->author ?>)

Hit counter: <?= $data->site->hits ?>

My computers:

<?php foreach ($data->computers as $computer) { ?>

Title: <?= $computer->title ?> (<?= $computer->ip ?>)

<?php } ?>

[/php]

I ran your scripts with success, however I noticed the way you added a number to the hit counter by using “++”. I see how this would work if you wanted to just add 1 to a variable, but what if I wanted to add more? In the example below is one of the ways I tried to add a number to the variable, however it always resulted in an error.

Could you explain to me how to add or subtract numbers the correct way?

Error Message:

Notice: Trying to get property of non-object in /home/denzeli97/domains/denzeli97.ninetyseven.axc.nl/public_html/test.php on line 18

Original:
[php]$data = getStore();
++$data->site->hits;
saveStore($data);[/php]

Broken New:
[php]$data = getStore();
$data = $data->site->hits+4;
saveStore($data);[/php]

Thanks in advance

You almost got it, what I did was actually increment by one (++) the property “hits”, in the object “site”, which is in the object “data”.

[php] $data = getStore();
$data->site->hits = $data->site->hits+4;
saveStore($data);[/php]

Change it to this and you can change the title / author :slight_smile:

[php]<?php
ini_set(‘error_reporting’, E_ALL);
ini_set(‘display_errors’, ‘1’);

function getStore () {
return json_decode(file_get_contents(‘data.json’));
}

function saveStore ($object) {
return file_put_contents(‘data.json’, json_encode($object));
}

$data = getStore();
$data->site->hits = $data->site->hits+4;

if (!empty($_GET[‘newSiteTitle’])) {
$data->site->title = $_GET[‘newSiteTitle’];
}

if (!empty($_GET[‘newSiteAuthor’])) {
$data->site->author = $_GET[‘newSiteAuthor’];
}

saveStore($data);

?>

Welcome to <?= $data->site->title ?> (by: <?= $data->site->author ?>)

Hit counter: <?= $data->site->hits ?>

My computers:

<?php foreach ($data->computers as $computer) { ?>

Title: <?= $computer->title ?> (<?= $computer->ip ?>)

<?php } ?>[/php]

Just visit

yoursite.com/thisfile.php?newSiteTitle=Type your new site title here

or

yoursite.com/thisfile.php?newSiteAuthor=Type your new site author here

or both

yoursite.com/thisfile.php?newSiteTitle=Type your new site title here&newSiteAuthor=Type your new site author here
Sponsor our Newsletter | Privacy Policy | Terms of Service