Making Usage Statistics in PHP - Save the HTML Title
(Page 2 of 7 )
As I've already mentioned above, it can be useful to save the HTML title of the page. We'll use header.php to show the <title></title> HTML tag. My solution to know the HTML title is to define this PHP constant 'TITLE'. I define it in the index.php, before it includes header.php.
// showing a topic using given data
define('TITLE', "Adam's forum > Topic: $topicname");
include('header.php')
// here comes the main part
Now the included header.php can save the TITLE constant.
Here's the query to save the data. Put this piece to the beginning of the header.php.
$q
='insert into hits (Host, Site, Title, Date) values
("'.gethostbyaddr($_SERVER['REMOTE_ADDR']).'",
"'.addslashes($_SERVER['QUERY_STRING']).'", "'.addslashes(TITLE).'", now())';
mysql_query($q) or print(mysql_error());
Details explained above. Note addslashes() that's necessary when writing strings in a database because of the hacking attempts. This is some kind of encoding method, so you'll need to decode it when you write the string out by the stripslashes() function.
Next: Showing the Statistics Table >>
More PHP Articles
More By Adam Szanto