Welcome to this tutorial on a page counter.
This will count how many people have visited your page per refresh.
So every time someone enters the page, a value will be incremented, will be stored in a text file and it will be displayed to the user.
Or you can keep it to yourself. Its your choice whether you want to display.
Please note, this is a very simple way of doing this.
It won't count unique visitors.
I'll make a unique visitors tutorial soon.
It'll probably be available by the time you are viewing this.
So check it out. It will be more specific.
It deals with IP addresses.
However, for now this is a basic counter tutorial and its using file-storage as opposed to database-storage.
Ok. So the first thing we need to do is create a file in which to store our value in.
There are 2 ways to do this.
Either right-click and create a new text document.
Or what I'll show you is how to create a file for opening, which is the function 'fopen'.
And we'll store it in the file variable. But its not manadatory.
And we'll say count.php and another parameter for this is whether you want it for writing, for reading or to append that, for example.
So, I'll say for writing.
Ok. Now I'll say 'fwrite' and I'll write to file and I'll create a value of zero.
So, now we'll open our page-up and refresh.
We've got counter.php. Click on that and when we go back see if we've got count.php
So, .txt
So, lets refresh this.
Ok, so now we should have a .txt file.
Let me remove this - count.php.
Now we've done that and we really don't need this code.
So I'll delete this part but I'll retain this and now I'll say I want to read from the file.
You could type this manually also. You just have to create a file for writing instead of reading.
So, we've got our file and we've got our value of zero in it.
So, lets open it and see.
Yes, we've got count.txt with zero that will read this and put it into that.
So, now, I need to get the contents of the file.
So, instead of 'fopen' I'll say, 'file_get_contents'.
So, I'll type 'file_get_contents'.
And that will get the contents of 'count.txt'.
Ok. Then I'll say 'echo' and use the variable and I'll say 'echo file'.
Now what this will do is it will say file_get_contents and it will get the contents of our text file with our count variable in there.
And it will say echo out the contents of the file.
So, lets go back to our page and we will refresh.
Click on counter and we've got zero at the moment.
Refreshing. Its still staying at zero as displayed here.
If I change this to 'hello' and I went back to our page and refreshed, it'll have the value 'hello'.
So, we are just echoing out whatever is in this text file at the moment.
And right now its zero - the integer zero.
Now to echo this, I'll say 'You've had file visitors'.
So, that will give us something like that.
Now, what I'll do is I will create a new variable called 'visitors'.
And I'll say that is equal to 'file'.
I'll put this up here to be more efficient and easy to read as well.
And I'll say 'visitors' and we can tell what this is going to be.
And then what we'll say is visitors
Visit-ors - new - equals this vistors plus 1.
So this will be our new value.
Then I'll go ahead and say 'filenew', so I'm creating a new file.
I'll open that as count.txt because that's what it is.
And I'll say to write this file.
Now if it was 'a+' that means 'append' - so I'm appending something onto the file, which means that I'm adding to it.
What I want to do is overwrite, so I'll put 'w'.
And then I'll say 'fwrite' like we did in our first part - to 'filenew'.
And the value that I need to write is 'visitorsnew'.
This is going to work. Lets just go through it before you run it.
We've got our main file and that's getting the contents of our count.txt which is zero at the moment.
We're setting our variable called 'visitors' to the contents of the file.
We're echoing out to the user how many visitors there have been.
And we're creating a new variable with the 'visitors + 1' - namely the person that is viewing this page at the moment.
That becomes significant. That's the person adding the extra 1 in there.
And then we're opening a new file as we saw in the start of this tutorial but instead we're using 'w' for write.
Then we're writing to our new file the new value which is increment of 1.
So, lets refresh and you can see - oh!.
Its not working!
Ok, so lets check this code.
Lets check the spelling of visitors - Visit-ors new. Ok. Visit-ors.
That's the reason. I had to put a 'n' there.
So, count.txt.
Now this time, we are adding 1 each time we refresh.
You can see that the value is in fact going up.
Now obviously, to reset this, all you would have to do is -
Ah! a warning. 'count.txt' has been changed because we have edited it.
I'll say 'reload from disk'.
And its changed to 19, as you can see, it displayed 18 there.
The reason is that we're echoing this out before we put our new value in.
So, for maximum efficiency and to get the actual correct value I'll put this code down there.
As matter of fact, when I am refreshing here and lets say, we get to 25 visitors and we come back here, we'll have the value 26.
Ok, well, maybe I'm being a little disorganised here.
There's no major efficient way to do it.
This will always echo out 'visitors'
So just for variation, we'll say 'visitors_new'.
So, this will equal exactly - oh no!
visitors new - another spelling mistake.
Right, so lets increment to 35 and we're going to contents and this value equals 35.
Position isn't everything when you have to deal with a code as simple as this but it does help.
Ok - so that's the basic tutorial.
If you'd like to get any help on it, then please get in touch.
But for now, try this out, give it a go.
Also watch my other tutorial on the more advanced counter that takes IP addresses into account.
Thanks for watching. This is Osama Butt dubbing for the Spoken Tutorial Project.