Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

PHP Referring page (for page counter)Helpful Member! 

Borvik (Programmer) (OP)
1 Dec 05 11:54
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:

CODE

$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.
KingCrab (MIS)
1 Dec 05 12:00
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.
Helpful Member!  sleipnir214 (Programmer)
1 Dec 05 12:00
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!

TANSTAAFL!!

Borvik (Programmer) (OP)
1 Dec 05 12:08
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.
Borvik (Programmer) (OP)
5 Dec 05 10:42
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.

CODE

$data = implode("",file($page_name));
$parser = xml_parser_create();
xml_parse_into_struct($parser,$data,$values,$tags);
xml_parser_free($parser);
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.

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!

Back To Forum

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