Jump to content

Hit Counter ... Basic Stuff Here


gcarterm

Recommended Posts

Hello,

Just scratching the PHP surface. A dilemma. I did a search on the topic here and came up empty, so ...

 

I have a site up, using Host Monster. It looks ok and the links work. I know enough PHP to have set up comment boxes to route comments to the intended targets and to be filed in phpMyAdmin in the HostMonster database so I can monitor them. No problem.

 

But now I am trying to set up a counter (invisible) to track visits. And I am aware of the pitfalls of this as an accurate gage of real traffic, but my site is so small that I simply want an idea of who shows up there. It's a hobby site, non-commercial.

 

The dilemma is this. All my pages are html. But I want to use PHP to set up the counter, preferably storing the results in a flat file. I understand the basics of how to read and write to one. BUT, without getting into the realm of htaccess, how can I manage to run the simple PHP counter code when all my pages are in HTML code? The server won't read it. This morning I had the bright idea of making my index.htm into index.php. Is that it? Simply save every page as PHP?

 

The simple coding examples I Googled say things like, "Just stick this (PHP) code at the very top of the page." Yep, but of course it won't be read unless the page extension is .php!!!!

 

I can't help but think I'm missing something VERY basic here. Thanks to anyone who can set me straight.

 

Mike

Link to comment
Share on other sites

Setting the system to parse html files as php is as simple as adding this line to your .htaccess file.

 

AddType application/x-httpd-php .html  

 

If you don't have an .htaccess file at present, simply create one in a good text editor and upload it to the root of the account.

Yes, the period belongs there, and No, there is no file extension.

 

Some systems will not allow you to see or save the file without an extension on the file (Mac). Simply save it as a .txt file, upload and rename the file.

Edited by jlhaslip
Link to comment
Share on other sites

just rename the pages from page.htm to page.php

 

to insert a php code block use <?php to start it and ?> to close it, the html will work as it is as a .php page is php + html

 

as for hit counter, create a table in your mysql database called stats, with;

 

ip(int 11 auto increment primary key), referer(vary 150) , browser(vary 150) , date(date), time(time), page(vary 150)

 

and add the following code to the top of each php page you wish to track

 

<?php

 

// db settings

$db_host = "mysql.yourhost.com";

$db_user = "yourdatabaseuser";

$db_pwd = "yourdatabasepassword";

$db_name = "yourdatabasename";

mysql_connect($db_host, $db_user, $db_pwd);

mysql_select_db($db_name);

 

// record stats

$ip = $_SERVER['REMOTE_ADDR']; // there ip

$referer = $_SERVER['HTTP_REFERER']; // where they came from eg google

$browser = $_SERVER['HTTP_USER_AGENT']; // browser - can also show search bots

$date = date("Y/m/d"); // current date

$time = date("H:i:s"); // current time

$page = $_SERVER['PHP_SELF']; // page they visited

mysql_query("INSERT INTO `stats` (ip, referer, browser, date, time, page) VALUES ('$ip', '$referer', '$browser', '$date', '$time', '$page')");

// end of stats

?>

Edited by sjhwebdesign
Link to comment
Share on other sites

THIS has GOT to be the greatest site going for PHP help! Thank you jlhaslip and sjhwebdesign for you useful help.

 

Ok, I'm going the rename-the-pages-to-php route because I should have done that in the first place and I can't help but think that will make them more flexible. I used the database info from sjhwebdesign, adapted the database opening code for this purpose from another file I had, and it works!! (When any code I write works, I'm always amazed.)

 

I've yet to use htaccess, though I'm sure that would work with HostMonster, as well. I'm saving that information.

 

One question. In the IP field of the database, showing the visitor's IP, is there a way to find out, in human terms, who they actually are? Can I look up the IP address somewhere and cross-reference it to a domain name?

 

Again, THANK YOU BOTH!!!

 

Mike

Link to comment
Share on other sites

Sorry, one more question for sjhwebdesign. Working with MySQL a bit, I'm confused about the IP field. It's given as "auto increment." The first number given when I tested it is 7252. If it is " there ip," why is it auto incremented? The count is simple; I just count the number of times each page is visited, but why the auto increment on an IP? Isn't that an address? Also, when I revisted that page (closing the browser and reopening it) and then refreshed the browser, only 1 entry was showing. Is it set up to only list discrete visitors?

 

Thanks,

Mike

Link to comment
Share on other sites

http://domains.whois.com/domain.php

 

enter the IP address into the textare/input box on that site

 

*edit*

And yes, the auto increment should not be there because the data is not unique in the table.

If anything, add another column to the table to auto increment as a record id, but the IP addy is not an auto increment field.

 

As this script is written, it writes an entry into the DB for each page hit. You still need to have a script written to select/sort/sub-total the entries by IP or page for the data to be meaningful.

Edited by jlhaslip
Link to comment
Share on other sites

look up ip with h tt p://w ww.geo ipto ol.c om

 

its a bit hit and miss though, because isp companies eg aol will route someone in northampton through an exchange in the lake district! other isp companies are more acurrate.

 

the ip in this case is used so you can follow a visitors journey through your website.

Link to comment
Share on other sites

SITE STATS


PAGE VIEWS TODAY: <? echo $dailyvisits; ?> TOTAL PAGE VIEWS: <? echo $totalvisits; ?>

IP

PAGE VIEWED

DATE

TIME

REFERER

BROWSER

<?php

while ($stats = mysql_fetch_array($getstats)) {

?>

<? echo $stats['ip']; ?>

<? echo $stats['page']; ?>

<? echo $stats['date']; ?>

<? echo $stats['time']; ?>

<? echo $stats['referer']; ?>

<? echo $stats['browser']; ?>

<?php

}

?>

Link to comment
Share on other sites

I use the same script myself on my site and works ok, I use dreamweaver so not sure bout Webuilder.

 

have you tried uploading it to server, it may be Webuilder don't like it, only thing that may upset it is ;

 

<? echo $stats['ip']; ?>

 

as it would normally be page.php?IP=theip, but thats how that site works.

Link to comment
Share on other sites

sjhwebdesign --

Thanks -- still working on it. Not a problem. My BIG question was how in the heck to have the server read code in an html page, which, of course, it can't without htaccess. All I had to do was put a .php extension on the pages and save them that way.

Spending my time tinkering.

 

Thanks,

Mike

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...