Difference between revisions of "PHP-and-MySQL/C4/Simple-Visitor-Counter/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
 
{|Border=1
 
{|Border=1
!Time
+
|'''Time'''
!Narration
+
|'''Narration'''
 
|-
 
|-
 
|00:00
 
|00:00

Revision as of 12:03, 10 July 2014

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

Contributors and Content Editors

Gyan, PoojaMoolya, Pratik kamble, Sandhya.np14