Results 1 to 5 of 5

Thread: how to put cookie inside this particular counter code?

  1. #1
    Join Date
    Sep 2010
    Posts
    5

    how to put cookie inside this particular counter code?

    I am new to this and have no clue even after checking how to do this. I see codes but not sure how to implement it so it "reads" right. I found this, but nto sure how to put it correctly for it to read out ok with my counter.
    setcookie ("TestCookie", $value,time()+3600); /* expire in 1 hour */

    I have this counter code below, which records a count each time the person/bot visit the main index file. (ex. www.somesite.com, or to /index.php). What I want to do is to actually count the same visitor again if he/she/it visits the site 1 hour later. so basically, I don't want the counter to go up if someone or bot goes to it again in 1 sec or less period.

    belows script use a db file that has some numbers for "today, yesterday, and total" through ($today, $yesterday, $total, $date, $days) and shows the output on a site through another php file.

    Can anyone help me with this? I will greatly appreciate it.



    Code:
    <?php
    
    
    		$file_count = fopen('counter/count.db', 'rb');
    		$data = '';
                    while (!feof($file_count)) $data .= fread($file_count, 4096);
    		fclose($file_count);
    		list($today, $yesterday, $total, $date, $days) = split("%", $data);
    		
    		if ($date == date("Y m d")) $today++ ;              		
    		
    
    			else {
    				$yesterday = $today;
    				$today = 1;  //  $today = 1;
    				$days++;
    				$date = date("Y m d");
    			}
    		$total++;
    		$line = "$today%$yesterday%$total%$date%$days";
    		
    	$file_count = fopen('counter/count.db', 'wb');		
    		fwrite($file_count, $line, strlen($line));
    		fclose($file_count);
    
    	 
    ?>

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: how to put cookie inside this particular counter code?

    Your
    Code:
    setcookie ("TestCookie", $value,time()+3600); /* expire in 1 hour */
    code should go after

    Code:
    <?php
    
    
            $file_count = fopen('counter/count.db', 'rb');
            $data = '';
                    while (!feof($file_count)) $data .= fread($file_count, 4096);
            fclose($file_count);
            list($today, $yesterday, $total, $date, $days) = split("%", $data);
    this part of your code. So basically, you put this code once you do some initialization or check ups before you actually start your main part of your code.

  3. #3
    Join Date
    Sep 2010
    Posts
    5

    Re: how to put cookie inside this particular counter code?

    So basically, this is ok below?

    Code:
    <?php
    
    
    		$file_count = fopen('counter/count.db', 'rb');
    		$data = '';
                    while (!feof($file_count)) $data .= fread($file_count, 4096);
    		fclose($file_count);
    		list($today, $yesterday, $total, $date, $days) = split("%", $data);
    	setcookie ("CookieName", $value,time()+3600); 
    
    		if ($date == date("Y m d")) $today++ ;              		
    		
    
    			else {
    				$yesterday = $today;
    				$today = 1;  //  $today = 1;
    				$days++;
    				$date = date("Y m d");
    			}
    		$total++;
    		$line = "$today%$yesterday%$total%$date%$days";
    		
    	$file_count = fopen('counter/count.db', 'wb');		
    		fwrite($file_count, $line, strlen($line));
    		fclose($file_count);
    
    	 
    ?>
    Do I need to put anything else in this script like, if it needs to check the cookie file, then start counting or not?

  4. #4
    Join Date
    Dec 2008
    Posts
    183

    Re: how to put cookie inside this particular counter code?

    I have not practically checked it, but the logic seems to be correct. So, according to me, there should be not any major changes.

    Code:
    list($today, $yesterday, $total, $date, $days) = split("%", $data);
    Just write the $date instead of $data in split. (I know this is minor mistake, but counts while implementing the script)

  5. #5
    Join Date
    Sep 2010
    Posts
    5

    Re: how to put cookie inside this particular counter code?

    ahhhhhhhhhhh thanks for that notice! I will try this out with the "date" fix! thanks again!

Similar Threads

  1. How to reset my MEP code entry counter
    By Syker in forum Portable Devices
    Replies: 4
    Last Post: 07-02-2011, 07:39 PM
  2. Inserting image inside the php code
    By garfield1 in forum Software Development
    Replies: 3
    Last Post: 03-12-2009, 02:00 PM
  3. I have a code inside that needs tweaking JAVA
    By Daren in forum Software Development
    Replies: 1
    Last Post: 28-05-2009, 12:07 PM
  4. I need JAVA help code inside
    By Daren in forum Software Development
    Replies: 3
    Last Post: 23-05-2009, 06:58 PM

Tags for this Thread

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
  •  
Page generated in 1,713,961,127.87148 seconds with 17 queries