PHP-and-MySQL/C4/Simple-Visitor-Counter/English-timed

From Script | Spoken-Tutorial
Revision as of 15:49, 25 October 2013 by Gyan (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
0:00 Welcome to this tutorial on a page counter.
0:02 This will count how many people have visited your page per refresh.
0:07 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.
0:15 Or you can keep it to yourself. Its your choice whether you want to display.
0:19 Please note, this is a very simple way of doing this.
0:21 It won't count unique visitors.
0:23 I'll make a unique visitors tutorial soon.
0:27 It'll probably be available by the time you are viewing this.
0:30 So check it out. It will be more specific.
0:33 It deals with IP addresses.
0:35 However, for now this is a basic counter tutorial and its using file-storage as opposed to database-storage.
0:42 Ok. So the first thing we need to do is create a file in which to store our value in.
0:48 There are 2 ways to do this.
0:50 Either right-click and create a new text document.
0:53 Or what I'll show you is how to create a file for opening, which is the function 'fopen'.
0:59 And we'll store it in the file variable. But its not manadatory.
1:05 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.
1:22 So, I'll say for writing.
1:26 Ok. Now I'll say 'fwrite' and I'll write to file and I'll create a value of zero.
1:36 So, now we'll open our page-up and refresh.
1:41 We've got counter.php. Click on that and when we go back see if we've got count.php
1:49 So, .txt
1:51 So, lets refresh this.
1:54 Ok, so now we should have a .txt file.
2:00 Let me remove this - count.php.
2:05 Now we've done that and we really don't need this code.
2:08 So I'll delete this part but I'll retain this and now I'll say I want to read from the file.
2:14 You could type this manually also. You just have to create a file for writing instead of reading.
2:22 So, we've got our file and we've got our value of zero in it.
2:26 So, lets open it and see.
2:28 Yes, we've got count.txt with zero that will read this and put it into that.
2:34 So, now, I need to get the contents of the file.
2:37 So, instead of 'fopen' I'll say, 'file_get_contents'.
2:42 So, I'll type 'file_get_contents'.
2:44 And that will get the contents of 'count.txt'.
2:48 Ok. Then I'll say 'echo' and use the variable and I'll say 'echo file'.
2:52 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.
3:02 And it will say echo out the contents of the file.
3:05 So, lets go back to our page and we will refresh.
3:07 Click on counter and we've got zero at the moment.
3:10 Refreshing. Its still staying at zero as displayed here.
3:14 If I change this to 'hello' and I went back to our page and refreshed, it'll have the value 'hello'.
3:20 So, we are just echoing out whatever is in this text file at the moment.
3:25 And right now its zero - the integer zero.
3:30 Now to echo this, I'll say 'You've had file visitors'.
3:37 So, that will give us something like that.
3:40 Now, what I'll do is I will create a new variable called 'visitors'.
3:46 And I'll say that is equal to 'file'.
3:50 I'll put this up here to be more efficient and easy to read as well.
3:55 And I'll say 'visitors' and we can tell what this is going to be.
4:00 And then what we'll say is visitors
4:05 Visit-ors - new - equals this vistors plus 1.
4:14 So this will be our new value.
4:17 Then I'll go ahead and say 'filenew', so I'm creating a new file.
4:22 I'll open that as count.txt because that's what it is.
4:27 And I'll say to write this file.
4:30 Now if it was 'a+' that means 'append' - so I'm appending something onto the file, which means that I'm adding to it.
4:38 What I want to do is overwrite, so I'll put 'w'.
4:42 And then I'll say 'fwrite' like we did in our first part - to 'filenew'.
4:47 And the value that I need to write is 'visitorsnew'.
4:50 This is going to work. Lets just go through it before you run it.
4:55 We've got our main file and that's getting the contents of our count.txt which is zero at the moment.
5:04 We're setting our variable called 'visitors' to the contents of the file.
5:07 We're echoing out to the user how many visitors there have been.
5:11 And we're creating a new variable with the 'visitors + 1' - namely the person that is viewing this page at the moment.
5:20 That becomes significant. That's the person adding the extra 1 in there.
5:24 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.
5:32 Then we're writing to our new file the new value which is increment of 1.
5:37 So, lets refresh and you can see - oh!.
5:41 Its not working!
5:42 Ok, so lets check this code.
5:44 Lets check the spelling of visitors - Visit-ors new. Ok. Visit-ors.
6:01 That's the reason. I had to put a 'n' there.
6:06 So, count.txt.
6:07 Now this time, we are adding 1 each time we refresh.
6:12 You can see that the value is in fact going up.
6:16 Now obviously, to reset this, all you would have to do is -
6:19 Ah! a warning. 'count.txt' has been changed because we have edited it.
6:24 I'll say 'reload from disk'.
6:27 And its changed to 19, as you can see, it displayed 18 there.
6:30 The reason is that we're echoing this out before we put our new value in.
6:35 So, for maximum efficiency and to get the actual correct value I'll put this code down there.
6:41 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.
6:51 Ok, well, maybe I'm being a little disorganised here.
6:55 There's no major efficient way to do it.
6:57 This will always echo out 'visitors'
6:59 So just for variation, we'll say 'visitors_new'.
7:07 So, this will equal exactly - oh no!
7:11 visitors new - another spelling mistake.
7:16 Right, so lets increment to 35 and we're going to contents and this value equals 35.
7:24 Position isn't everything when you have to deal with a code as simple as this but it does help.
7:30 Ok - so that's the basic tutorial.
7:32 If you'd like to get any help on it, then please get in touch.
7:35 But for now, try this out, give it a go.
7:37 Also watch my other tutorial on the more advanced counter that takes IP addresses into account.
7:43 Thanks for watching. This is Osama Butt dubbing for the Spoken Tutorial Project.

Contributors and Content Editors

Gyan, PoojaMoolya, Pratik kamble, Sandhya.np14