Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


PHP Referring page (for page counter)

PHP Referring page (for page counter)

PHP Referring page (for page counter)

I'm working on a page counter for a user that doesn't know much about modifying html to include one properly.

It needed to be able to track multiple pages (for reporting purposes), be simple for the user, and add new page easily when necessary.  The counter is not visible on the website.

The user has no knowledge of php, and shouldn't have to play with the file.

So, I came up with a solution.  Have the user include the counter script as an image (which would output a 1px square image just so it outputs something), and the script would pull the name of the file via HTTP_REFERER and store it in the database (either adding, or updating the hits), with of course an admin page that allows the user to view all the pages and the number of hits.

Currently, it works fine - for grabbing the name of the file, however - the name of the file could be the same for several pages.  Here's what I have so far:


$page_name = $_SERVER['HTTP_REFERER'];
$slash_idx = strrpos($page_name, "/");
$page_name = substr($page_name, $slash_idx + 1);

What I'm hoping to accomplish is to pull, not the name of the file - but the Title that the referring page has.  I'm even sure if that's possible.

Any insight would be appreciated.

RE: PHP Referring page (for page counter)

The title of a page is HTML and PHP has no knowledge of what each title of an HTML page could be.
when loading the image you could use something like

<img src="counter.php?pageTitle=Default">

To place a variable in the get. That would require the user just add a little to the link and nothing more.

RE: PHP Referring page (for page counter)

It's possible, but it will slow your applications and be non-deterministic.

It will slow your application because in order to find the title of the referring page, your script will have to use fopen() or cURL or some other PHP function to fetch the referring page, parse the file and extract the required data.

It will be non-deterministic because $_SERVER['HTTP_REFERER'] is only available when the browser reports that data to the server.  And more and more browsers both give the user the power to turn off referer reporting and make NOT reporting referers the default.

Want the best answers? Ask the best questions!


RE: PHP Referring page (for page counter)

Thanks for the quick responses guys,

I actually thought of that KingCrab, and ran it by the user (who actually wants it for another user - go figure), and he has enough confidence in the final user to get it right for the first couple of weeks, before he forgets a step and ends up with two different pages with the same counter (normally not to bad of an issue, but this website depends on it).

sleipnir214 - I realize that, and its unfortunate that browsers behave that way (for us programmers, but can be nice for security) - I was hoping that by being in an image tag that the program doing the refering would be the webserver, but now that I think about it (duh) the browser does that too.

RE: PHP Referring page (for page counter)

Well, I found a solution.

I have the webserver parsing htm and html files as php as well so that they can include a <? require_once("counter.php"); ?> in there.  This way the php can get the actual file name, without having to use the referer.

Thanks to sleipnir214 for pointing me in the right direction for opening the file and parsing out the title - however I'm not using fopen as that would be slow as sleipnir214 mentions.

I'm instead using an xml parser (PHP 3 >= 3.0.8, PHP 4, PHP 5).
Here at first $page_name contains a reference to the filename.


$data = implode("",file($page_name));
$parser = xml_parser_create();
foreach($tags as $key=>$val){
    if($key=="TITLE"){ //find the title tag
        $ranges = $val; //grab the tag location
        $t_tag = $values[$ranges[0]]; //get the tag values (ranges should only contain 1 value - the location in the other array
        $page_name = $t_tag["value"]; //pull the title value

Thanks again to sleipnir214 - and I hope this can help someone as well.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close