Hello and welcome to my tutorial on how to create a Hits counter for your site, either being for your eyes only, or for everyone to see We will first start off with how to make it so that it will only be visible for you to see. The first step that we need to do, is to create the command that will be added to everypage that you want to count on the Hits count (All hits will be culminative). Please note that all file's extensions must be .PHP for this to work, except for the file counterhits.txt Code (markup): To accomplish this, we are going to use PHP's wonderful include command. On your page, if you are mostly using HTML codes, you will need to paste this code anywhere in your <BODY></BODY> tags - doesn't matter where because this part is invisible: <?php include 'counter.php'; ?> PHP: What that code does, is it performs the code of counter.php which we will create in the next step. The next step is to create the code which counts all hits and saves them to a text file also saved on your server. To do this, we are going to use variables for the current number of hits, as well as some read and write commands to save this to the text file, after adding a hit to it. The code for this is: <?php $fp = fopen("counterhits.txt", "r"); $hitcount = fread($fp, 1024); fclose($fp); $hitcount = $hitcount + 1; $fp = fopen("counterhits.txt", "w"); fwrite($fp, $hitcount); fclose($fp); ?> PHP: As you can see from that code, it mentions counterhits.txt. This will be the file where your hit count is saved to. To make your Hit Counter save the number of hits to the file, you will have to create a blank file named counterhits.txt, and CHMOD it to 777 (If you do not know what CMOD'ing is or how to do it, please Google it). CHMOD'ing to 777 makes it read and writeable by the counter.php file which is important, as it would not work without this. So now you have a fully functional Hit Counter that allows only you to see the number of hits going to your site. Now you might wondering, "How do I make it so that other people can see how many hits I have recieved?", so I will also be showing you how to accomplish this. Again, we will be using the PHP include command, as well as the PHP echo command. You will need to paste this code into the spot where you would like the counter to be shown: <?php echo 'Number of hits: '; include 'counterhits.txt'; ?> PHP: What this code does, is it simply writes Number of Hits:, and using the variable of what the Hit Counter is at, writes the current number. And then you are done! Please go ahead and reference the files that I have attached! *iTrade Me IF YOU USE or LIKE THIS TUTORIAL Thanks in advanced.
Thank you again Husameddin for the sharing your tutorial on how to create a hit counter in a site... ) keep the good work..
Yeah...im just start my 'Tutorial Folder/Thread'....and will add another folder like a 'HTML' Folder, 'JavaScript Folder', 'Programming' and etc And iTrade given are to show your support and appreciate...also to keep my tutorial continuos... Thanks jigolo for your honor!!
For those using .Net a very basic webcontrol would be like: <%@ Control Language="VB" ClassName="HitCounter" %> <%@ Import Namespace="System.IO" %> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) ' read the file to memory Dim Filename As String = Server.MapPath("~/counter.txt") Dim objStreamReader As StreamReader = File.OpenText(Filename) Dim Count As Integer = CInt(objStreamReader.ReadToEnd()) objStreamReader.Close() ' Update file Dim objStreamWriter As StreamWriter = File.CreateText(Filename) objStreamWriter.Write(Count + 1) objStreamWriter.Flush() objStreamWriter.Close() ' Display count CounterLT.Text = Count End Sub </script> <asp:Literal ID="CounterLT" runat="server" /> Code (ASP.Net): Just place the webcontrol on every page you want it for and bobs your uncle
Too many file accesses and overhead - and you have an opportunity for the file-lock to 'miss'. You only need to open once if you open it read-write "r+" and then do a fseek. ... and since you'd be likely to call it at the same time as writing out the value, I'd just make it echo the value when you read it. <?php $fp=fopen("counterhits.txt","r+"); $hitcount=fread($handle, filesize("counterhits.txt")); $hitcount+=1; echo $hitcount; fseek($fp,0); fwrite($fp,$hitcount); fclose($fp); ?> Code (markup): of course this is raw reads, not unique hits - a useful number, but not necessarily an indication of your number of visitors - something that would be better handled by a database, though you could fake a quick database off a normal file using comma delimiteds (just plan on it being slow) Though $hitcount being called thus makes it a global, so if you don't want to have the routine echo it's result, just <?php echo $hitcount; ?> from anywhere in your code as desired.
tutorial is good........bt itrader is for some monetary exchange i think....although we can give u reputation points