Jump to content

Hit counter


rnintulsa

Recommended Posts

Hi, I am not getting any text on the page that is supposed to tell me the number of hits to my site.

 

New to php and working on this one for three days now.  Need help. Searched high and low for a hidden counter tutorial that would work on my index.html page.

 

Both of these pages are in a folder named log, and the file permissions of that folder are set to chmod 777 command.

 

This is the Javascript I put in my head tags on the index page.

<script language="javascript" type="text/javascript" src="/log/webcounter.php"></script>

 

This is the php page that does the processing called webcounter.php

<?php
$maxlines = 100; // the maximum number of lines to show
$fn = "log.txt"; // the name of the log file to create
$s = 'document.write(\'© 2008 All Rights Reserved\')';
$host = 'my host'; // the shortest url

// get referring page
$page = @$HTTP_REFERER;
$uarray = parse_url($page);
$refhost = $uarray["host"];

// check referring page is on this site
if ( strpos($refhost, $host) === false ) {
  exit ("Access denied from: $refhost");
}

// create new file if necessary
if (!file_exists($fn)) {
   $fp = fopen ($fn, "w");
   $os = "0\n";
   $fw = fwrite($fp, $os);
   fclose($fp);
}

// read existing data
$fa = file($fn);
$nl = count($fa);

$c = $fa[0] + 1; // inc hit

// write data to file
$fp = fopen ($fn, "w");
if (flock($fp, LOCK_EX)) { //lock the file
  $os = $c . "\n\n";
  $fr = fwrite($fp,$os);
  $ip = getenv("REMOTE_ADDR");
  $os = $uarray["path"]."\t".date("D M jS g:ia")."\t$ip\t".$_GET['ref'];
  $fr = fwrite($fp,$os);
  if ($nl > $maxlines) { $nl = $maxlines; }
  if ($nl > 1) {
     for ($i = 1; $i < $nl; $i++)
   	    $fr = fwrite($fp,$fa[$i]); 
  }
flock($fp, LOCK_UN); //release lock
  fclose($fp);
}

//output the javascript text
echo ($s);
?>

 

If I understand right, I should be able to go to the log.txt via my browser to view the current number of hits, right?

 

I got this code online while searching for a tutorial. 

 

I appreciate all suggestions and explanations.

 

 

 

 

Link to comment
Share on other sites

I have been searching to learn more about AJAX and how I

could possibly use it to call the php file. 

 

Can anyone help me understand what discomatt meant?

 

I know I would use this

XMLHttpRequest

but how and where?

 

I will keep looking to understand, but this is new to me and so is php.

Link to comment
Share on other sites

Well, your BEST bet is to just have PHP pages and include the counter script directly, but if it's not an option

 

http://www.w3schools.com/Ajax/Default.Asp

 

Will give you the basics on calling an external page with javascript. It's up to you to learn a bit of javascript to apply it to your site. I'll gladly help but I won't code for you

Link to comment
Share on other sites

Unfortunately these pages have to be html pages.

Doesn't the way I have it :

<script language="javascript" type="text/javascript" src="/log/webcounter.php"></script>

call the external page?

 

Can you explain why this javascript code will not work.

I am trying to learn, and explanations really help me.

 

I will now go to the link you posted, thanks.

 

I am new to php and javascript. 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.