Results 1 to 8 of 8

Thread: Counter?

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Counter?

    I have a client who wants the counter on their site to start at 3838. Well, I decided instead of figuring out a way to have the counter I already have start at 3838, I'd just get all of my myspace friends and I to refresh until we got that many.

    Well, we were almost to 2000 when the script suddenly restarted.
    It was so weird.
    And I have NO idea why it did that.

    Here's the script:

    PHP Code:
    $file fopen("counter.txt""r");
    $num fgets($file,3838);
    $num += 1;
    fclose($file);
      
    $file2 fopen("counter.txt""w");
    fputs($file2$num);
    fclose($file2);
                    
    $num $num/2;
    $num ceil($num);
                    
    $visits '<i>'.$num.'</i> visits.<br />';
    echo 
    $visits
    Then, I just include that file where I want the number of visits.

    counter.txt just has "3838" in it, because I wondered if putting the number I wanted to start with in there would work.
    It didn't work.

    If any of you has another script, or a way to make this one work...help?

    There are also some quirks:

    For some reason, everytime it was adding a visit, it was adding 2 instead. So I divided $num by 2.
    Then, when it was doing that, eventually it was having numbers with decimals...
    So that's why I did the ceil($num) part
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Jan 2007
    Location
    Boulder City, Nevada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I notice you have your target conter value below
    "$num = fgets($file,3838);"
    That should not make a difference, as that number is being used as the number of bytes to return.

    However, what about the following 2 lines:
    $num = $num/2;
    $num = ceil($num);

    I don't think you need these, unless you only want the counter value to be half of what it really is?

    I set the "counter.txt" to 3838 and removed the above lines and it seems to work fine.

    So I would say just set the counter.txt to 3838 (ftp to server) and rem out the two lines in question and it should work.

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I explained those two lines above:

    For some reason, everytime it was adding a visit, it was adding 2 instead. So I divided $num by 2.
    Then, when it was doing that, eventually it was having numbers with decimals...
    So that's why I did the ceil($num) part
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  4. #4
    Join Date
    Jan 2007
    Location
    Boulder City, Nevada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorry, I missed that,
    I am using it like this and it seems to be consistant...

    Code:
    <?php
    $file = fopen("counter.txt", "r");
    $num = fgets($file,3838);
    $num += 1;
    fclose($file);
    
    $file2 = fopen("counter.txt", "w");
    fputs($file2, $num);
    fclose($file2);
                    
    //$num = $num/2;
    //$num = ceil($num);
                    
    $visits = '<i>'.$num.'</i> visits<br />';
    
    echo $visits;  
    ?>
    it's at http://debbiemerrillshow.com/counter.php

  5. #5
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Hmm, that's so weird. For some reason, it was about seven thousand something when I included the script...
    Then, when I just put the script in there, it was the right number.
    Dude, that's so weird.

    Well, thanks.
    It's working now.
    So that means a happy client.
    And we like those.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  6. #6
    Join Date
    Jan 2007
    Location
    Boulder City, Nevada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Here Is The Counter I Use
    This COUNTER script only increments once per IP address and only on the INDEX page. If you include your IP address it will not count you.
    Here is a FUNCTION I use for a page counter in all my scripts.

    Code:
    /*
    ****************************************
    Function page_hits
    ****************************************/
    #----- http://www.iwcomm.com/ -----#
    function page_hits(){
    
    	#-----Finds Path To File-----#
    	@session_start();
    	$exp=explode('/',$_SERVER['SCRIPT_FILENAME']);
    	$ref='';
    	$n=4;
    	while(!strpos($exp[$n],'php')){
    		$ref.= '../';
    		$n++;
    	}
    	#-----For Testing -----#
    	//echo 'site_root = '.$_SERVER["DOCUMENT_ROOT"].'/include/hitcounter.dat<br />';
    	//$COUNT_FILE = $ref."include/hitcounter.dat";
    	
    	#-----Path To Data File-----#
    	$COUNT_FILE = $_SERVER["DOCUMENT_ROOT"]."/inc/hitcounter.dat";
    
    	#-----Make Sure Data File Exists-----#
    	if (file_exists($COUNT_FILE)){
    		#-----Opens The Data File And Gets Current Value-----#
    		$fp = fopen("$COUNT_FILE", "r+");
    		flock($fp, 1);
    		$count = fgets($fp, 4096);
    		#-----Check To See If It Is OK To Increment-----#
    		if(($_SERVER['REMOTE_ADDR'] != $_SESSION['SERVER_IP'] && $_SERVER['REMOTE_ADDR'] != $_SESSION['CURRENT_IP']) && ($_SESSION['PAGE']=='/index.php')){
    			#-----Increment The Data File-----#
    			$count += 1;
    			#-----Set The Current Users IP Address-----#
    			$_SESSION['CURRENT_IP'] = $_SERVER['REMOTE_ADDR'];
    		}
    		#-----Update And Close The Data File-----#	
    		fseek($fp,0);
    		fputs($fp, $count);
    		flock($fp, 3);
    		fclose($fp);
    	}else{
    		#-----Set Displayed Counter Value If An Error Occured-----#
    		$count = 'Many';
    	}
    	#-----Set Count Session Value And Return $count Values
    	$_SESSION['count'] = $count;
    	return $count;
    }

    Variables Used:

    $COUNT_FILE - Name of the counter data text file (chmod 666) on server
    $_SERVER["DOCUMENT_ROOT"] - Your server base path
    $_SERVER['REMOTE_ADDR'] - IP address of user connecting to your server
    $_SESSION['SERVER_IP'] - Your local IP address (when connecting to the server)
    $_SESSION['CURRENT_IP'] - Set by script to prevent duplicate count per session
    $_SESSION['PAGE'] - Will only count hits on this page
    $_SESSION['count'] - Current counter value

    Usage:
    I call it from a page "footer" file, so it appears on all pages.

    Code:
    <?page_hits()?><?=COUNT_SITE_HAS?> <?=$_SESSION['count']?> <?=COUNT_VISITORS?>
    Looks Like:
    This Site Has Had 244 Visitors Since January 1st, 2007
    Last edited by apollo; 01-14-2007 at 04:21 AM.

  7. #7
    Join Date
    Jan 2007
    Location
    Boulder City, Nevada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Great...
    I just posted a smarter counter!

    Good Luck!

  8. #8
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Alright, I may use yours in the future.
    Thanks.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

Bookmarks

Posting Permissions

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