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

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

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