
|
View Full Version : PHP: Counters
mouldy_punk 08-04-2004, 09:19 AM I would like to make a script in PHP that will, count how many unique hits I have had, by logging the visitors IP's in the MySQL database, I also want it to log each unique hits' resolution and how many times they have visited.
I have 3 pages I would include the counter in, index.php, content.php, and img.php. The content.php file is every page on my site really except the home and img. It gets the content by ID from the MySQL database. The img file just shows an image basically. go to http://gtaisland.gtagaming.com to see what I'm on about.
I would like to have a page in my admin cp where I can view all the stats, like how many people out of all unique visits have 800x600 resolution, how many have 1024x768 etc, and how many unique visits I get each day, the average unique visits each day and the average unique visits each month.
The question is, how? I'm alright at PHP, I coded everything on my site bymyself its a little CMS. Where do I start with coding all of this?
Burhan 08-04-2004, 09:50 AM Why re-invent the wheel? Just find a script that does this at hotscripts.com (unless you are doing this as a learning exercise).
Another option is that your hosting provider may already provide such information in the form of a log analyzing package (for example, cpanel has a few log analyzers).
mouldy_punk 08-04-2004, 09:54 AM unless you are doing this as a learning exercise
Thats pretty much it really. You don't learn anything by using someone elses scripts.
Burhan 08-04-2004, 10:07 AM Ah -- well then I would recommend you get familiar with $_SERVER, because that's were most (if not all) of your information will be coming from.
In addition to that, I would suggest that you store your statstical information in a table. This way, you can avoid concurrent write issues that can crop up with file-based counters, and you will be able to run queries such as "show me all users from brazil" etc.
mouldy_punk 08-04-2004, 11:27 AM Ok, so far so good. I have made it enter into a table the visitors ip, HTTP_USER_AGENT and the id it auto-increment.
Now, how do I make the visits go up for each time the person visits, its probably something obvious but this is what I've got so far;
include "../global.php";
$ip = getenv(REMOTE_ADDR);
$browser = getenv(HTTP_USER_AGENT);
$resolution = "800x600";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
$sql = mysql_query("SELECT * FROM counter WHERE ip = '$ip'");
while($row = mysql_fetch_row($sql)){
$prev_visits = $row['visits'];
$visits = $prev_visits +1;
}
mysql_query("INSERT into `counter` (browser,ip,visits,resolution) VALUES ('$browser','$ip','$visits','$resolution')");
at the moment I just set resolution at 800x600 because I haven't found the predefined varible for the resolution yet.
How can I make the visits field +1 everytime the person visits?
bhanson 08-04-2004, 12:07 PM At first you need to check to see if they already have an entry in your database. Here is something I whiped up, fully untested but you should see the logic.
include "../global.php";
$ip = getenv(REMOTE_ADDR);
$browser = getenv(HTTP_USER_AGENT);
$resolution = "800x600";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
$sql = mysql_query("SELECT ip,visits FROM `counter` WHERE ip='$ip'");
// No need for a loop here, you're only expecting one row back
if (mysql_num_rows($sql) == "0"){
mysql_query("INSERT into `counter` (browser,ip,visits,resolution) VALUES ('$browser','$ip','1','$resolution')");
} else {
$result = mysql_fetch_array($sql, MYSQL_NUM);
$visits = $sql[1];
$visits++;
mysql_query("UPDATE `counter` SET visits='$visits' WHERE ip='$ip'");
}
mouldy_punk 08-04-2004, 01:14 PM With that code, the visits remain at 1 even after reloading the page which means that its not updating it for what ever reason.
bhanson 08-04-2004, 02:25 PM Sorry, I made a mistake in the code. $sql[1] should be $result[1].
Tested, it works.
include "../global.php";
$ip = getenv(REMOTE_ADDR);
$browser = getenv(HTTP_USER_AGENT);
$resolution = "800x600";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
$sql = mysql_query("SELECT ip,visits FROM `counter` WHERE ip='$ip'");
// No need for a loop here, you're only expecting one row back
if (mysql_num_rows($sql) == "0"){
mysql_query("INSERT into `counter` (browser,ip,visits,resolution) VALUES ('$browser','$ip','1','$resolution')");
} else {
$result = mysql_fetch_array($sql, MYSQL_NUM);
$visits = $result[1];
$visits++;
mysql_query("UPDATE `counter` SET visits='$visits' WHERE ip='$ip'");
}
mouldy_punk 08-04-2004, 02:39 PM Thanks alot. Now I just need to figure out how to detect the resolution. I heard php can't do it, but javascript can and can pass on the variable to php or something, so I'll look into that.
Burhan 08-05-2004, 02:48 AM You can simplify the code a bit further thusly :
$sql = mysql_query("SELECT ip,visits FROM `counter` WHERE ip='$ip'");
// No need for a loop here, you're only expecting one row back
if (mysql_num_rows($sql) == "0"){
mysql_query("INSERT into `counter` (browser,ip,visits,resolution) VALUES ('$browser','$ip','1','$resolution')");
} else {
mysql_query("UPDATE `counter` SET visits= visits + 1 WHERE ip='$ip'");
}
mouldy_punk 08-05-2004, 04:48 AM mysql_query("UPDATE `counter` SET visits= visits + 1 WHERE ip='$ip'");
The visits= visits +1 was the bit I was stuck on. I wasn't sure how to make it add 1 each time. Thanks alot.
still can't find anything about a resolution though. I don't want to use javascript because some people disable it in their browser.
mouldy_punk 08-05-2004, 10:28 AM ok, I am majorly stuck on this resolution thing. I want to just include the counter.php file in the header so that on all pages it will count, but I can't find any javascript that will help me :(
Any ideas?
bhanson 08-05-2004, 09:19 PM The only way I can see doing this is using Javascript since PHP is a server-side language and cannot detect that. There's only three ways to pass a variable from Javascript to PHP. GET, POST, and COOKIE. Since you want this to work seamlessly in the background, COOKIE would be the best option here.
You're going to have to check for the cookie in PHP, if it's there go ahead and finish executing the script and enter the data into your MySQL database. If it is not there then set the cookie using Javascript, and refresh the page to trigger the rest of your script.
Use "screen.width" and "screen.height" variables in Javascript to retrieve the information.
I hope this helps!
Doh004 08-05-2004, 11:49 PM Mouldy Punk I don't believe PHP has a built in function that can detect the users screen resolutions. I've checked two PHP books and even my phpinfo() and I've found nothing.
You could however have a splash page (I don't like them but meh)that says if you have 800x600 than the link to enter will be like: main.php?res=800600 and the other resolution would be main.php?res=7961200 (or whatever numbers you want).
Than when inserting the resolution of the vistor to the table, just use $res as the value.
I hope I didn't just waste my breath there. ;)
|