Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2006, 04:24 AM
Registered User
 
Join Date: Jun 2006
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");';
}
?>

 
Old June 23rd, 2006, 05:22 AM
Registered User
 
Join Date: Jun 2006
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
 
Old June 23rd, 2006, 05:25 AM
Registered User
 
Join Date: Jun 2006
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>


 
Old June 23rd, 2006, 05:28 AM
Registered User
 
Join Date: Jun 2006
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?)

 
Old June 23rd, 2006, 07:00 AM
Registered User
 
Join Date: Jun 2006
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.

 
Old June 23rd, 2006, 07:08 AM
Registered User
 
Join Date: Jun 2006
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.






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





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