T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 64
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 215
  Visitor Online : 5
  Today Visit: 322
  Total Visitor : 301344
  Most Online : 41
::: Sponsored Links :::

PR 4 For This Webpage

Vinaora Logo
Home Today :

Building a Counter


The way this counter works is it creates a cookie on the user's computer with their IP and it updates the count according to the status of that cookie. If the cookie is present and the IP in the cookie is the same as the user's current IP then the script knows the user has already been there and it does nothing or if it is present, but has an IP different than the user's current IP or is not present the script knows that the user is either new or was there before, but the cookie has expired so it then updates the count. This script will be using the setcookie() function, which must be sent before any headers are sent. Basically what that means is that it must be placed before any HTML. The count will be held in a text file for ease of use but if you desire to use a database that is perfectly acceptable. Now for the code.

PHP Tutorials :

<?
$c_ip 
$HTTP_COOKIE_VARS["user_ip"]; 
$counter_file "counter/count.txt"
$counter_file_line file($counter_file);<br>  if(!$c_ip) { 
setcookie("user_ip"$REMOTE_ADDRtime()+360000);  $counter_file_line[0]++; 
$cf fopen($counter_file"w+"); 
fputs($cf"$counter_file_line[0]");   <br>  fclose($cf); 

elseif(
$c_ip != $REMOTE_ADDR){ 
$counter_file_line[0]++; <br>  $cf fopen($counter_file"w+"); 
fputs($cf"$counter_file_line[0]"); 
fclose($cf); 

?>


Okay then, well there is certainly a lot going on there. Let's break it down shall we.

PHP Tutorials :

<?
$c_ip 
$HTTP_COOKIE_VARS["user_ip"]; 
  
$counter_file "counter/count.txt"
$counter_file_line file($counter_file); 
?>


This first line is reassigning a variable name as you can hopefully tell. The variable we are naming is the variable that refers to our cookie that the counter script will later create. It is not necessary to use $HTTP_COOKIE_VARS[] to refer to a cookie, they can generally be called by just name as a variable (i.e. $user_ip), but to be safe we have. The second variable indicates where the text file for the counter is being stored. You can change this to whatever you please. The third variable opens the counter and writes it's content to an array using the file() function. The counter file is only one line long so therefore it only has one array element.

PHP Tutorials :

<?
if(!$c_ip) { 
?>



PHP Tutorials :

<?
setcookie
("user_ip"$REMOTE_ADDRtime()+360000); 
?>


This line creates our cookie. If you do not understand the setcookie() function I advise you look it up in the manual for the greatest detail, but basically what is happening is we are making a cookie who's name is "user_ip" and who's value is the user's IP Address. The third argument is the amount of time before the cookie is deleted and it makes a call to the time() function. As you can see it says time()+360000 which means the current time Unix time index plus 360000 seconds. This is the number I chose for my script and it is a pretty decent amount of time before the cookie is removed however you may use whatever you please. If you leave the third argument out the cookie will never be deleted unless done manually (which I do not suggest). This code is obviously only used if the server doesn't find the required cookie.

PHP Tutorials :

<?
$counter_file_line
[0]++; 
$cf fopen($counter_file"w+"); 
fputs($cf"$counter_file_line[0]"); 
fclose($cf); 

?>


This part is pretty straight forward. It is our code to add to the counter. The first line in this chunk of code just adds 1 to the value of the first line in the counter file. The second line here opens the counter file for editing so we can write the data back in. The third line actually does the writing of the new value. Finally, the sixth line closes the file.

PHP Tutorials :

<?
elseif($c_ip != $REMOTE_ADDR) { 
?>


This line is pretty straight-forward. All this line does is check the cookie to see it's value. If the value of the cookie is not equal to the users current IP the code is executed.

PHP Tutorials :

<?
  $counter_file_line
[0]++; 
$cf fopen($counter_file"w+"); 
fputs($cf"$counter_file_line[0]"); 
fclose($cf); 

?>


This line is exactly the same as before and should require no explaining. This line also completes all the code we need to write data with though what good is data in a file if no one sees the data. To display our counter we only need one line of code assuming that the above code is executed on the same page as the count is viewed on.

PHP Tutorials :

<?
echo $counter_file_line[0]; 
?>


The above code will display our count. If you desire to get the count on a page other than the page that you increment the counter use this:

PHP Tutorials :

<?
 $counter_file 
"counter/count.txt"
$counter_file_line file($counter_file); 
  echo 
$counter_file_line[0]; 
?>



Thanks for Lakario

All information from phpfreaks]

Other Tutorials

::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007