Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Referring page (for page counter) 1

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
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.
 
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.
 
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!!
 
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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top