www.webdeveloper.com
Results 1 to 4 of 4

Thread: Simple unique hit counter not working

  1. #1
    Join Date
    Jan 2009
    Location
    127.0.0.1
    Posts
    126

    Simple unique hit counter not working

    I am trying to get a unique hit counter working based on IP addresses as it's the only way I can think of identifying unique users.

    Basically what i am doing is taking the ip they view the page with and checking it against a file that includes a list of all the other ip's that have viewed the page. If the new ip isn't listed it is added to the file and the number of hits is incremented by 1.

    I am having trouble getting the while loop to run through the file of ip addresses. the error and code follow, the error occurs in the while statement.

    Error:
    Parse error: syntax error, unexpected '[' in /home2/whackand/public_html/blog/index.php on line 21

    Code:
    Code:
    <?php
    /* This is not part of the script
    if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == true)
    	{
    	header("Location: new_browser.php");
    	}
    */
    
    //Displaying errors
    ini_set('display_errors',1);
    error_reporting(E_ALL|E_STRICT);
    
    //the script
    $ip = $_SERVER['REMOTE_ADDR'];
    echo $ip;
    $ip_log = ("ip_log.txt");
    $ip_log_array = file($ip_log);
    echo $ip_log_array;
    $fp_ip = fopen($ip_log, "r+");
    $i = 0;
    echo $fp_ip;
    echo $ip_log_array[$i];
    
    while(ip_log_array[$i] != feof($fp_ip))
    
    	if($ip != $ip_log_array[$i])
    		{
    		fputs($fp_ip, "$ip");
    		
    		$hit_counter = ("hits.txt");
    		$hits = file($hit_counter);
    		$hits[0]++;
    		$fp = fopen($hit_counter, "w");
    		fputs($fp, "$hits[0]");
    		fclose;
    		}
    	i++;
    	}
    echo $hits;
    ?>
    Thanks for any help you can offer.

  2. #2
    Join Date
    Mar 2011
    Location
    Ponca City, OK
    Posts
    42
    Try this:
    PHP Code:
    <?php
    /* This is not part of the script
    if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == true)
        {
        header("Location: new_browser.php");
        }
    */

    //Displaying errors
    ini_set('display_errors',1);
    error_reporting(E_ALL|E_STRICT);

    //the script
    $ip $_SERVER['REMOTE_ADDR'];
    echo 
    $ip;
    $ip_log = ("ip_log.txt");
    $ip_log_array file($ip_log);
    echo 
    $ip_log_array;
    $fp_ip fopen($ip_log"r+");
    $i 0;
    echo 
    $fp_ip;
    echo 
    $ip_log_array[$i];

    while(
    $ip_log_array[$i] != feof($fp_ip))
    {
        if(
    $ip != $ip_log_array[$i])
            {
            
    fputs($fp_ip"$ip");
            
            
    $hit_counter = ("hits.txt");
            
    $hits file($hit_counter);
            
    $hits[0]++;
            
    $fp fopen($hit_counter"w");
            
    fputs($fp"$hits[0]");
            
    fclose;
            }
        
    i++;
        }
    echo 
    $hits;
    ?>

  3. #3
    Join Date
    May 2010
    Location
    Effingham, IL
    Posts
    87
    I would recommend using a analytics program instead. It will give you more information and will help you market your website. It is not a good idea to display the 'hits' your website has, but if you really want this feature with Google Analytics you can also export this data to a web page give you features of analytics and easy export of the data you want.
    Ryan Condron
    Freelance Web Developer
    http://www.rebelwebdevelopment.com

  4. #4
    Join Date
    Jan 2009
    Location
    127.0.0.1
    Posts
    126
    Thanks for the idea about Google Analytics. I thought about using it but I just wanted something basic plus I have never coded something like that so I wanted to gain some experience.

    I will look into it. Also the echo's were just error checking trying to figure out what was wrong I am not actually going to display it.

    Thanks for the new code, I am going to look at it and figure out where I went wrong.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles