User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 427,861 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,624 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 4863 | Replies: 12
Reply
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: Hit counter

  #11  
Jul 1st, 2007
Originally Posted by tehgreatmg View Post
When I access http://localhost/counter.php I get this:

document.write("1 Hits");PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: open(C:\DOCUME~1\c9972442\LOCALS~1\Temp\php\session\sess_ql1eb53bk1s8ohkja7ivd6tb13, O_RDWR) failed: Permission denied (13) in C:\Inetpub\wwwroot\counter.php on line 3
PHP Warning:  fopen(counter.txt) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied in C:\Inetpub\wwwroot\counter.php on line 23
PHP Warning:  fwrite(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\counter.php on line 24
PHP Warning:  fclose(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\counter.php on line 25
PHP Warning:  Unknown: open(C:\DOCUME~1\c9972442\LOCALS~1\Temp\php\session\sess_ql1eb53bk1s8ohkja7ivd6tb13, O_RDWR) failed: Permission denied (13) in Unknown on line 0
PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (C:\DOCUME~1\c9972442\LOCALS~1\Temp\php\session) in Unknown on line 0

It looks to me that it will just not let me open the file due to permissions.


Your PHP is set to write session data to the disk. It looks like the directory where session are saved (C:\DOCUME~1\c9972442\LOCALS~1\Temp\php\session\) is unwritable for PHP.

Your file C:\Inetpub\wwwroot\counter.txt is unreadable for PHP.

You have to make sure both of these are writable.

PS: why do you want to use session? does your hit counter only increment for new session?

Having the counter in a JS file does nothing but add another HTTP Request for that JS file.
You can just as well include the file counter.php in your PHP file that writes the page.
include('counter.php'); 

would be teh same as:
<script src="counter.php"></script>

in your case because counter.php is run once per load of the page.
Only using <script> will cause a new HTTP Request.

if you use a php include() then the part:

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

would be:

echo $handle.' Hits';
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Hit counter

  #12  
Jul 1st, 2007
To digital-ether:

It only makes sense to use this approach ( php in js ) from a caching perspective... assuming text/html requests aren't updated frequently; they can have long cache times; an XKB + header text/html response from php or otherwise that's never going to change can be cached on user's PCs and in interim public caches along the way. Every time the cached page is viewed in a browser only the javascript has to be requested again, the response from which is only ever going to be about 30 bytes + header ( assuming 1-byte character encoding.. )

Result is, the first time a page is requested through some network, 2 requests are sent, and the 1KB page and 30 byte js file have to be downloaded. Assuming the time a PHP parser at the server takes to read and process a long file and a short file is the same ( which isn't true, but may aswell be compared to transmit time differences ) it would be better to have a single response : there's less bytes to transfer, since '1 hits' is less bytes than 'document.write('1 hits');', and since the page doesn't need a '<script src=', and since there's only one HTTP header required for the response... There's not much in it, but if the page is dynamic anyway, there would be no point NOT including the hit counter output in the generated page directly.

But if the page in question ISN't dynamic; the second third fourth and etc time the page might be pulled from a cache, and only the 30 byte js is downloaded... = less drain on the server, and less time to download the whole thing.

To tehgreatmg:

Try taking out the session part temporarily:

  1. <?php
  2.  
  3. // get current hit
  4. $opFile = fopen ("counter.txt", "r");
  5. $handle = fread ($opFile, filesize ("counter.txt"));
  6. fclose ($opFile);
  7.  
  8. // add the hit
  9. $handle = $handle + 1;
  10.  
  11. // print javascript
  12. echo 'document.write("'.$handle.' Hits");';
  13.  
  14. // put new hit to db
  15. $opFile = fopen ("counter.txt", "w");
  16. fwrite ($opFile, $handle);
  17. fclose ($opFile);
  18.  
  19. ?>

Certainly make sure that a counter.txt file exists in the same folder as the counter.php file. If that works, then look into the permissions on that C:\DOCUME~1\c9972442\LOCALS~1\Temp\php\session\ folder.. I don't know anything about Windows as a server OS, but try this if you're using IIS: http://www.peterguy.com/php/install_IIS6.html.. link explains how ( and why ) to set some neccessary permissions and how to configure PHP.

Do you manage your own public HTTP server, or do you just use the PC/localhost to test things? If you rent hosting somewhere, it's their responsibility to configure PHP/etc. So such things are usually working.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: Hit counter

  #13  
Jul 1st, 2007
Thanks MattEvans:

Ah.. I see my mistake. Yes, you need the counter included as a JS File if your HTML page will be cached.

For OP:

I would recommend sending some headers to prevent caching of counter.php.

in counter.php:

  1. <?php
  2.  
  3. // no cache headers
  4. header('Expires Mon, 26 Jul 1997 05:00:00 GMT');
  5. header('Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
  6. header('Pragma no-cache');
  7. // etc. etc.
  8.  
  9. // rest of your code...
  10. session_start ();
  11.  
  12. // .. and the rest... etc. etc.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 3:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC