2

I have php script that counts web site visits. I need to call that script each time my html page loads.

How do I do that?

3 Answers 3

5

If it's a PHP page, you can include/require a page in to another PHP page using require or include see include and require documentation

require("counting_script.php");
// or
include("counting_script.php");

If it's a purely HTML file, then you could consider using an iFrame to load your other page see tutorial

<iframe src="page_counter.php"></iframe>

Or if you know jQuery, you could make an ajax call (using jQuery) to your counting page. See documentation

$.get('page_counter.php', function(data) {
  alert('Counter page was called');
});
Sign up to request clarification or add additional context in comments.

Comments

3

Some webpages uses a transparent 1x1 pixel image that can count number of requests, it works by letting PHP return a image. This is the code for image.php:

header('Content-type: image/gif');
echo chr(71).chr(73).chr(70).chr(56).chr(57).chr(97).chr(1).chr(0).chr(1).chr(0).chr(128).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(33).chr(249).chr(4).chr(1).chr(0).chr(0).chr(0).chr(0).chr(44).chr(0).chr(0).chr(0).chr(0).chr(1).chr(0).chr(1).chr(0).chr(0).chr(2).chr(2).chr(68).chr(1).chr(0).chr(59);

So now you simply create the correct HTML for it like this:

<img src="image.php"/>

Notice that you also can send variables and other useful information to your PHP-file with arguments like:

<img src="image.php?username=test"/>

You also recieve a lot of other useful information in this approach like being able to use the $_SERVER-variables to determine what URL the user saw when the image.php file was requested, so now you are able to make per-page statistics.

1 Comment

I liked this better than using an IFrame.
2

Another workaround is to use an img tag

<img src="page_counter.php" alt="counter"/>

and inside your page_counter.php file

<?php 
//your php counter script

//return an 1x1 transparent image
$image = file_get_contents('your_path/1x1.png'); 
header('content-type: image/png'); 
echo $image;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.