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