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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{|Border=1
 
{|Border=1
!Time
+
|'''Time'''
!Narration
+
|'''Narration'''
 
|-
 
|-
 
|00:00
 
|00:00
|Welcome to this tutorial on a page counter.   
+
|Welcome to this tutorial on a '''page counter'''.   
 
|-
 
|-
 
|00:02
 
|00:02
Line 13: Line 13:
 
|-
 
|-
 
|00:15
 
|00:15
|Or you can keep it to yourself. Its your choice whether you want to display.
+
|Or you can keep it to yourself. It's your choice whether you want to display.
 
|-
 
|-
 
|00:19
 
|00:19
Line 25: Line 25:
 
|-
 
|-
 
|00:27
 
|00:27
|It'll probably be available by the time you are viewing this.
+
|It'll be probably  available by the time you are viewing this.
 
|-
 
|-
 
|00:30
 
|00:30
Line 31: Line 31:
 
|-
 
|-
 
|00:33
 
|00:33
|It deals with IP addresses.
+
|It deals with '''IP addresses'''.
 
|-
 
|-
 
|00:35
 
|00:35
|However, for now this is a basic counter tutorial and its using file-storage as opposed to database-storage.
+
|However, for now, this is a basic counter tutorial and it's using file-storage as opposed to database-storage.
 
|-
 
|-
 
|00:42
 
|00:42
|Ok. So the first thing we need to do is create a file in which to store our value in.
+
|Ok. So the first thing we need to do is- create a file in which to store our value in.
 
|-
 
|-
 
|00:48
 
|00:48
Line 46: Line 46:
 
|-
 
|-
 
|00:53
 
|00:53
|Or what I'll show you is how to create a file for opening, which is the function 'fopen'.
+
|Or what I'll show you is how to create a file for opening which is the function '''fopen()'''.
 
|-
 
|-
 
|00:59
 
|00:59
|And we'll store it in the file variable.  But its not manadatory.
+
|And we'll store it in the '''$file''' variable.  But it's not mandatory.
 
|-
 
|-
 
|01:05
 
|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.
+
|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
 
|01:22
Line 58: Line 58:
 
|-
 
|-
 
|01:26
 
|01:26
|Ok.  Now I'll say 'fwrite' and I'll write to file and I'll create a value of zero.
+
|Ok.  Now I'll say '''fwrite()''' and I'll write to '''$file''' and I'll create a value of zero.
 
|-
 
|-
 
|01:36
 
|01:36
|So, now we'll open our page-up and refresh.
+
|So, now we'll open our page-up and '''refresh'''.
 
|-
 
|-
 
|01:41
 
|01:41
|We've got counter.php.  Click on that and when we go back see if we've got count.php  
+
|We've got "counter.php".  Click on that and when we go back, see if we've got count.php.
 
|-
 
|-
 
|01:49
 
|01:49
|So, .txt
+
|So, ".txt".
 
|-
 
|-
 
|01:51
 
|01:51
|So, lets refresh this.
+
|So, let's refresh this.
 
|-
 
|-
 
|01:54
 
|01:54
|Ok, so now we should have a .txt file.
+
|Ok, so now we should have a ".txt" file.
 
|-
 
|-
 
|02:00
 
|02:00
|Let me remove this - count.php.
+
|Let me remove this - "count.php".
 
|-
 
|-
 
|02:05
 
|02:05
Line 82: Line 82:
 
|-
 
|-
 
|02:08
 
|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.
+
|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
 
|02:14
Line 91: Line 91:
 
|-
 
|-
 
|02:26
 
|02:26
|So, lets open it and see.
+
|So, let's open it and see.
 
|-
 
|-
 
|02:28
 
|02:28
|Yes, we've got count.txt with zero that will read this and put it into that.
+
|Yes, we've got "count.txt" with zero that will read this and put it into that.
 
|-
 
|-
 
|02:34
 
|02:34
Line 100: Line 100:
 
|-
 
|-
 
|02:37
 
|02:37
|So, instead of 'fopen' I'll say, 'file_get_contents'.
+
|So, instead of '''fopen()''' I'll say, 'file_get_contents()'.
 
|-
 
|-
 
|02:42
 
|02:42
Line 109: Line 109:
 
|-
 
|-
 
|02:48
 
|02:48
|Ok.  Then I'll say 'echo' and use the variable and I'll say 'echo file'.
+
|Ok.  Then I'll say '''echo''' and use the variable and I'll say '''echo $file'''.
 
|-
 
|-
 
|02:52
 
|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.
+
|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
 
|03:02
|And it will say echo out the contents of the file.
+
|And it will say '''echo''' out the contents of the '''$file'''.
 
|-
 
|-
 
|03:05
 
|03:05
|So, lets go back to our page and we will refresh.  
+
|So, let's go back to our page and we will refresh.  
 
|-
 
|-
 
|03:07
 
|03:07
|Click on counter and we've got zero at the moment.
+
|Click on "counter" and we've got zero at the moment.
 
|-
 
|-
 
|03:10
 
|03:10
|Refreshing.  Its still staying at zero as displayed here.
+
|Refreshing.  It's still staying at zero as displayed here.
 
|-
 
|-
 
|03:14
 
|03:14
Line 133: Line 133:
 
|-
 
|-
 
|03:25
 
|03:25
|And right now its zero - the integer zero.
+
|And right now it's zero - the integer zero.
 
|-
 
|-
 
|03:30
 
|03:30
|Now to echo this, I'll say 'You've had file visitors'.
+
|Now to '''echo''' this, I'll have to say "You've had $file visitors".
 
|-
 
|-
 
|03:37
 
|03:37
Line 142: Line 142:
 
|-
 
|-
 
|03:40
 
|03:40
|Now, what I'll do is I will create a new variable called 'visitors'.
+
|Now, what I'll do is I will create a new variable called '''$visitors'''.
 
|-
 
|-
 
|03:46
 
|03:46
|And I'll say that is equal to 'file'.
+
|And I'll say that is equal to '''$file'''.
 
|-
 
|-
 
|03:50
 
|03:50
Line 151: Line 151:
 
|-
 
|-
 
|03:55
 
|03:55
|And I'll say 'visitors' and we can tell what this is going to be.
+
|And I'll say '$visitors' and we can tell what this is going to be.
 
|-
 
|-
 
|04:00
 
|04:00
|And then what we'll say is visitors
+
|And then what we'll say is '''$visitors'''.
 
|-
 
|-
 
|04:05
 
|04:05
|Visit-ors - new - equals this vistors plus 1.
+
|Visit-ors - new - equals this '''$visitors''' plus 1.
 
|-
 
|-
 
|04:14
 
|04:14
|So this will be our new value.
+
|So, this will be our new value.
 
|-
 
|-
 
|04:17
 
|04:17
|Then I'll go ahead and say 'filenew', so I'm creating a new file.
+
|Then I'll go ahead and say '''$filenew''', so I'm creating a new file.
 
|-
 
|-
 
|04:22
 
|04:22
|I'll open that as count.txt because that's what it is.
+
|I'll open that as "count.txt" because that's what it is.
 
|-
 
|-
 
|04:27
 
|04:27
Line 172: Line 172:
 
|-
 
|-
 
|04:30
 
|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.
+
|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
 
|04:38
Line 178: Line 178:
 
|-
 
|-
 
|04:42
 
|04:42
|And then I'll say 'fwrite' like we did in our first part - to 'filenew'.
+
|And then I'll say 'fwrite()' like we did in our first part - to '$filenew'
 
|-
 
|-
 
|04:47
 
|04:47
|And the value that I need to write is 'visitorsnew'.
+
|and the value that I need to write is '$visitorsnew'.
 
|-
 
|-
 
|04:50
 
|04:50
|This is going to work.  Lets just go through it before you run it.
+
|This is going to work.  Let's just go through it before you run it.
 
|-
 
|-
 
|04:55
 
|04:55
|We've got our main file and that's getting the contents of our count.txt which is zero at the moment.
+
|We've got our main file and that's getting the contents of our "count.txt" which is zero at the moment.
 
|-
 
|-
 
|05:04
 
|05:04
|We're setting our variable called 'visitors' to the contents of the file.
+
|We're setting our variable called '$visitors' to the contents of the '$file'.
 
|-
 
|-
 
|05:07
 
|05:07
Line 205: Line 205:
 
|-
 
|-
 
|05:32
 
|05:32
|Then we're writing to our new file the new value which is increment of 1.
+
|And then we're writing to our new file the new value which is increment of 1.
 
|-
 
|-
 
|05:37
 
|05:37
|So, lets refresh and you can see - oh!.
+
|So, let's refresh and you can see - oh!.
 
|-
 
|-
 
|05:41
 
|05:41
|Its not working!
+
|It's not working!Ok, so let's check this code.
|-
+
|05:42
+
|Ok, so lets check this code.
+
 
|-
 
|-
 
|05:44
 
|05:44
|Lets check the spelling of visitors - Visit-ors new.  Ok.  Visit-ors.   
+
|Let's check the spelling of visitors - Visit-ors new.  Ok.  Visit-ors.   
 
|-
 
|-
 
|06:01
 
|06:01
|That's the reason.  I had to put a 'n' there.   
+
|That's the reason.  I had to put a 'n' in there.   
 
|-
 
|-
 
|06:06
 
|06:06
|So, count.txt.
+
|So, "count.txt".Now this time, we are adding 1 each time we refresh.
|-
+
|06:07
+
|Now this time, we are adding 1 each time we refresh.
+
 
|-
 
|-
 
|06:12
 
|06:12
|You can see that the value is in fact going up.
+
|So you can see that the value is in fact going up.
 
|-
 
|-
 
|06:16
 
|06:16
|Now obviously, to reset this, all you would have to do is -  
+
|Now obviously to reset this, all you would have to do is -  
 
|-
 
|-
 
|06:19
 
|06:19
Line 241: Line 235:
 
|-
 
|-
 
|06:27
 
|06:27
|And its changed to 19, as you can see, it displayed 18 there.
+
|And it's changed to 19, as you can see, it displayed 18 there.
 
|-
 
|-
 
|06:30
 
|06:30
Line 247: Line 241:
 
|-
 
|-
 
|06:35
 
|06:35
|So, for maximum efficiency and to get the actual correct value I'll put this code down there.
+
|So, for maximum efficiency and to get the actual correct value, I'll put this code down there.
 
|-
 
|-
 
|06:41
 
|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.
+
|As a matter of fact, when I am refreshing here and let's say- we get to 25 visitors and we come back here, we'll have the value 26.
 
|-
 
|-
 
|06:51
 
|06:51
|Ok, well, maybe I'm being a little disorganised here.  
+
|Ok, well, may be I'm being a little disorganized here.  
 
|-
 
|-
 
|06:55
 
|06:55
Line 259: Line 253:
 
|-
 
|-
 
|06:57
 
|06:57
|This will always echo out 'visitors'   
+
|This will always echo out '$visitors'.  
 
|-
 
|-
 
|06:59
 
|06:59
|So just for variation, we'll say 'visitors_new'.
+
|So just for variation, we'll say '$visitorsnew'.
 
|-
 
|-
 
|07:07
 
|07:07
Line 271: Line 265:
 
|-
 
|-
 
|07:16
 
|07:16
|Right, so lets increment to 35 and we're going to contents and this value equals 35.
+
|Right, so let's increment to 35 and we're going to contents and this value equals 35.
 
|-
 
|-
 
|07:24
 
|07:24
Line 289: Line 283:
 
|-
 
|-
 
|07:43
 
|07:43
|Thanks for watching.  This is Osama Butt dubbing for the Spoken Tutorial Project.
+
|Thanks for watching.  This is Osama Butt, dubbing for the Spoken Tutorial Project.

Latest revision as of 17:10, 24 March 2017

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. It's 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 be probably 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 it's 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 it's not mandatory.
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, let's 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, let's 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, let's 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. It's 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 it's zero - the integer zero.
03:30 Now to echo this, I'll have to 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 $visitors 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. Let's 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 And then we're writing to our new file the new value which is increment of 1.
05:37 So, let's refresh and you can see - oh!.
05:41 It's not working!Ok, so let's check this code.
05:44 Let's check the spelling of visitors - Visit-ors new. Ok. Visit-ors.
06:01 That's the reason. I had to put a 'n' in there.
06:06 So, "count.txt".Now this time, we are adding 1 each time we refresh.
06:12 So 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 it's 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 a matter of fact, when I am refreshing here and let's say- we get to 25 visitors and we come back here, we'll have the value 26.
06:51 Ok, well, may be I'm being a little disorganized 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 '$visitorsnew'.
07:07 So, this will equal exactly - oh no!
07:11 visitors new - another spelling mistake.
07:16 Right, so let's 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