Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 23rd, 2006, 04:24 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Counter PHP problem

Problem getting this code to write over 9999 to counter2.txt file ???
Im very much a novice.
At first it writes and displays 10000 then on page reload or an additional visit it reverts to 1000 and the 1001 etc etc.

Im sure its a simple fix ??


<?php
session_start ();

// get current hit
$opFile = fopen ("counter2.txt", "r");
$handle = fread ($opFile, filesize ("counter2.txt"));
fclose ($opFile);

// if new session
if (!isset ($_SESSION['hit'])){

// set session
$_SESSION['hit'] = TRUE;

// add the hit
$handle = $handle + 1;

// print javascript
echo 'document.write("'.$handle.' Hits");';

// put new hit to db
$opFile = fopen ("counter2.txt", "w");
fwrite ($opFile, $handle);
fclose ($opFile);

// else
}else{

// print only
echo 'document.write("'.$handle.' Hits");';
}
?>

Reply With Quote
  #2 (permalink)  
Old June 23rd, 2006, 05:22 AM
Registered User
 
Join Date: Jun 2006
Location: Richards Bay, Kwazulu Natal, South Africa.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tested your code. The code works fine.

Have fun,
Dale

Ctrl-Alt-Del
Reply With Quote
  #3 (permalink)  
Old June 23rd, 2006, 05:25 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yeh it works fin until the counter reaches 9999 then it resets to 1000 and starts to count 1001 and upward.

????

Not my code, it was free out there on the web somewhere. on a how to site.

its implemented onto a page with this code ???
perhapse this is my problem ?

<script language="JavaScript" src="counter2.php" type="text/JavaScript"></script>


Reply With Quote
  #4 (permalink)  
Old June 23rd, 2006, 05:28 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OH ! and how would I edit it so that it counts up on every refresh without need to clear the browser (so I can add the counter to each pahe on my site?)

Reply With Quote
  #5 (permalink)  
Old June 23rd, 2006, 07:00 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You see, I would like to duplicate the counter so that I have an idea of the total page hits throughout the website alongside the more accurate 'homepage' counter which rely's on the session.

Reply With Quote
  #6 (permalink)  
Old June 23rd, 2006, 07:08 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can stop looking now :D

Thank goodness for Google and analytical thought

and the answer was

session_destroy();

at the end of the code :) Im well chuffed with meself.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with CGI Counter arunmohanmp Perl 0 April 9th, 2006 12:40 AM
Counter tp194 Javascript 1 September 2nd, 2004 08:02 AM
hit counter gumgak Classic ASP Basics 2 November 2nd, 2003 12:44 AM
counter Adam H-W Classic ASP Basics 15 August 15th, 2003 11:18 AM



All times are GMT -4. The time now is 07:37 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.