473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to log visitors using PHP?

Can someone point me to a sample script that will log all visitors to my web
site?

I'm looking for a simple script that will capture IP address, Host Name,
Browser, OS, and referring page - and write that info to MySql.

I've created a MySql database - visitors - with the columns mentioned above, but
am fumbling trying to get started with PHP... any help is appreciated!
Jul 17 '05 #1
23 6702
okay, here's what I've got so far:

<?php
//get visitors ip address
$ipaddress = $REMOTE_ADDR;
//get visit date
$vdate = date("m-d-y");
//connect to database
$conn = @mysql_connect("localhost","username","password") or die("cannot connect
to MySQL");
//select the database
@mysql_select_db("ppvisits") or die("cannot connect to the database");
//execute the query
@mysql_query("INSERT INTO guests(ip,date) VALUES ('$ipaddress','$vdate')");
//close the connection
mysql_close($conn);
?>

What I still need is a way to get hostname, browser, OS, and referring page...
"deko" <dj****@hotmail.com> wrote in message
news:56*******************@newssvr25.news.prodigy. com...
Can someone point me to a sample script that will log all visitors to my web
site?

I'm looking for a simple script that will capture IP address, Host Name,
Browser, OS, and referring page - and write that info to MySql.

I've created a MySql database - visitors - with the columns mentioned above, but am fumbling trying to get started with PHP... any help is appreciated!

Jul 17 '05 #2

<snip code >

What I still need is a way to get hostname, browser, OS, and referring page...


Try and have a look at http://www.hotscripts.com/, quite a few scripts
approach that subject.
Also, have a look at getenv("HTTP_USER_AGENT"); that contains some of the
info you need.

Your main problem will be the OS and Browser, but if you look at the php
scripts they provide you will be well on your way.

And why keep the IP BTW?, just curious really.

Sims
Jul 17 '05 #3
I've looked at hotscripts, but mostly an ocean of way too complicated scripts...
hard to navigate. But I have found a few helpful things just by doing a web
search.

As for getenv("HTTP_USR_AGENT");

I tried this, but it did not work...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Get Stats</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
print getenv("HTTP_USR_AGENT");
?>
</body>
</html>
Jul 17 '05 #4
"deko" <dj****@hotmail.com> wrote in message
news:56*******************@newssvr25.news.prodigy. com...
Can someone point me to a sample script that will log all visitors to my web site?

I'm looking for a simple script that will capture IP address, Host Name,
Browser, OS, and referring page - and write that info to MySql.

I've created a MySql database - visitors - with the columns mentioned above, but am fumbling trying to get started with PHP... any help is appreciated!


Forgive me, I may be missing something, but just parse the server logs. its
all there.
less load on server instead of making db connection/writting data/closing
connection for every page

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #5
> Forgive me, I may be missing something, but just parse the server logs. its
all there.
less load on server instead of making db connection/writting data/closing
connection for every page


I agree, but I don't have access to the server logs - it's a shared hosting
environemnt...
Jul 17 '05 #6
> Forgive me, I may be missing something, but just parse the server logs. its
all there.
less load on server instead of making db connection/writting data/closing
connection for every page


perhpas the thing to do would be to write it all to a flat file and then batch
update the db once a day?
Jul 17 '05 #7
"deko" <dj****@hotmail.com> wrote in message
news:U7******************@newssvr25.news.prodigy.c om...
Forgive me, I may be missing something, but just parse the server logs. its all there.
less load on server instead of making db connection/writting data/closing connection for every page
I agree, but I don't have access to the server logs - it's a shared

hosting environemnt...


Gotcha,

Sometimes on a shared server, you can still look at the logs, generaly they
are readable by all system accounts, try something like this:

test.php
----------------------
<?php
$info =`tail /var/log/httpd/access_log`;
print $info;
?>
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #8
If they do not offer up a log to you, then there is a chance they will not
offer up a cron job to schedule daily tasks.

I can think of may workarounds, but they are all more work than just parsing
the logs.

examples

* call a page that does the batching
* make call to another server
* write to a flat file, and if its over xxxx amount of bytes, spawn a
background process to batch

those are just a few.

--
Mike Bradley
http://www.gzentools.com -- free online php tools
"deko" <dj****@hotmail.com> wrote in message
news:me*******************@newssvr25.news.prodigy. com...
Forgive me, I may be missing something, but just parse the server logs. its all there.
less load on server instead of making db connection/writting data/closing connection for every page
perhpas the thing to do would be to write it all to a flat file and then

batch update the db once a day?

Jul 17 '05 #9
I noticed that Message-ID:
<56*******************@newssvr25.news.prodigy.co m> from deko contained
the following:
Can someone point me to a sample script that will log all visitors to my web
site?

I'm looking for a simple script that will capture IP address, Host Name,
Browser, OS, and referring page - and write that info to MySql.

I've created a MySql database - visitors - with the columns mentioned above, but
am fumbling trying to get started with PHP... any help is appreciated!


Check out $_SERVER In fact you are already using part of it. You
should be using $_SERVER['REMOTE_ADDR']
foreach ($_SERVER as $key => $value) {
$fullheaders .= $key . ": " . $value . "<br>\n";
}
$_SERVER will contain whatever headers are sent. Here is an example of
what you might get (I have removed identifying information)

ArrayKey: DOCUMENT_ROOT; Value: /usr/local/www/data
Key: HTTP_ACCEPT; Value: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel,
application/msword, */*
Key: HTTP_ACCEPT_ENCODING; Value: gzip, deflate
Key: HTTP_ACCEPT_LANGUAGE; Value: ie-ee,en-gb;q=0.5
Key: HTTP_CONNECTION; Value: keep-alive
Key: HTTP_HOST; Value: <removed>
Key: HTTP_USER_AGENT; Value: Mozilla/4.0 (compatible; MSIE 6.0;
Windows 98; generic_01_01)
Key: HTTP_VIA; Value: 1.1 webcacheB02 (NetCache NetApp/5.2.1R3)
Key: HTTP_X_FORWARDED_FOR; Value: <removed>
Key: PATH; Value:
/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin
Key: REMOTE_ADDR; Value: 195.92.168.164
Key: REMOTE_PORT; Value: 8008
Key: SCRIPT_FILENAME; Value: <removed>
Key: SCRIPT_URI; Value: <removed>
Key: SCRIPT_URL; Value: <removed>
Key: SERVER_ADDR; Value: <removed>
Key: SERVER_ADMIN; Value: <removed>
Key: SERVER_NAME; Value: <removed>
Key: SERVER_PORT; Value: 80
Key: SERVER_SIGNATURE; Value: <ADDRESS>Apache/1.3.26 Server at
<removed> Port 80</ADDRESS>

Key: SERVER_SOFTWARE; Value: Apache/1.3.26 (Unix) mod_fastcgi/2.2.12
mod_perl/1.27 Resin/2.0.1 PHP/3.0.18 PHP/4.3.2-dev
Key: UNIQUE_ID; Value: QBJd4cFvyAUAAXqgd3k
Key: GATEWAY_INTERFACE; Value: CGI/1.1
Key: SERVER_PROTOCOL; Value: HTTP/1.1
Key: REQUEST_METHOD; Value: GET
Key: QUERY_STRING; Value: 2
Key: REQUEST_URI; Value: <removed>
Key: SCRIPT_NAME; Value: <removed>
Key: PATH_TRANSLATED; Value: <removed>
Key: PHP_SELF; Value: <removed>
Key: argv; Value: Array
Key: argc; Value: 1
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #10
In article <k_*******************@newssvr25.news.prodigy.com> ,
"deko" <dj****@hotmail.com> wrote:
I've looked at hotscripts, but mostly an ocean of way too complicated
scripts...
hard to navigate. But I have found a few helpful things just by doing a web
search.

As for getenv("HTTP_USR_AGENT");

I tried this, but it did not work...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Get Stats</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
print getenv("HTTP_USR_AGENT");
?>
</body>
</html>

HTTP_USER_AGENT
^

--
Sandman[.net]
Jul 17 '05 #11
In article <56*******************@newssvr25.news.prodigy.com> , "deko"
<dj****@hotmail.com> wrote:
Can someone point me to a sample script that will log all visitors to
my web site?

I'm looking for a simple script that will capture IP address, Host
Name, Browser, OS, and referring page - and write that info to MySql.

I've created a MySql database - visitors - with the columns mentioned
above, but am fumbling trying to get started with PHP... any help is
appreciated!


Do this:

<?
foreach ($_SERVER as $key => $value){
print "\$_SERVER[$key] = $value<br>\n";
}
?>

You'll end up with a long list of variables and what their value is.

The variables you should keep an eye on are:

$_SERVER["REMOTE_ADDR"]
$_SERVER["HTTP_REFERER"]
$_SERVER["HTTP_USER_AGENT"]

--
Sandman[.net]
Jul 17 '05 #12
> * write to a flat file, and if its over xxxx amount of bytes, spawn a
background process to batch


Updating the database everytime someone visists the site is kind of silly, so
I'm thinking writing to a file is better. Then I can display the file in a
table (the last 20 or so visitors) on my "stats" page and batch update the
database twice a day. This will keep the stats as current as they need to be.
I could also provide a link on the page that will generate a larger list of past
visitors.

I need a good learning project, so this might as well be it. If you could point
me in the right
direction on a few things that would be great - for example: How do I write the
data to a file so that it will be formatted for batch processing? It's easy to
write to a file, but how to format it so it can be imported into the
database? Should I have more than one file? Also can I use PHP (instead of
cron) to kick off the database update? How do I kick it off as a background
process?

Thanks!

Jul 17 '05 #13
"deko" <dj****@hotmail.com> wrote in message
news:28******************@newssvr27.news.prodigy.c om...
* write to a flat file, and if its over xxxx amount of bytes, spawn a
background process to batch
Updating the database everytime someone visists the site is kind of silly,

so I'm thinking writing to a file is better. Then I can display the file in a table (the last 20 or so visitors) on my "stats" page and batch update the
database twice a day. This will keep the stats as current as they need to be. I could also provide a link on the page that will generate a larger list of past visitors.

I need a good learning project, so this might as well be it. If you could point me in the right
direction on a few things that would be great - for example: How do I write the data to a file so that it will be formatted for batch processing? It's easy to write to a file, but how to format it so it can be imported into the
database? Should I have more than one file? Also can I use PHP (instead of cron) to kick off the database update? How do I kick it off as a background process?

Thanks!


One of the things I like to do is have a directory that has write
permisions, so I done have to change permisions on a file by file basis. so
you could create a directory called _wfiles or something similar, and chmod
it 777

then to write data to you log you just need to do this:

$logData = "data to write to log";
$fp = fopen("_wfiles/mylog.txt","a");
fwrite($fp,"$logData\n");
fclose($fp);
now you also asked what format to save the file in, well thats up to you,
since it is your file, and you will be the onlyone reading/parsing it. I
would suggest the | pipe chr as a seperator for items. thus you can do
something like:

$logData = $_SERVER['REMOTE_ADDR'] . "|" .
$_SERVER['REQUEST_METHOD'] . "|" .
$_SERVER['REQUEST_URI'] . "|" .
$_SERVER['HTTP_REFERER'] . "|" .
$_SERVER['HTTP_USER_AGENT'];

(look at the php manual under Predefined Variables for more info)

to spawn a background proccess, try something like:
exec("php -q myscript.php > /dev/null");

As far as scheduling the background proccess, you can't but we can do some
trickery and get to to run if someone clicks a page and it hasnt run yet.
sorta like this:

we write the time we update the log to a file like this:
$fp = fopen("_wfiles/logtime.txt","w");
fwrite($fp,time());
fclose($fp);

we check the time in the file before we update it, if it is older than a set
time, the spawn background
the whole thing might look like:
$logData = $_SERVER['REMOTE_ADDR'] . "|" .
$_SERVER['REQUEST_METHOD'] . "|" .
$_SERVER['REQUEST_URI'] . "|" .
$_SERVER['HTTP_REFERER'] . "|" .
$_SERVER['HTTP_USER_AGENT'];

$fp = fopen("_wfiles/mylog.txt","a");
fwrite($fp,"$logData\n");
fclose($fp);

$logTime = @implode("",@file("_wfiles/logtime.txt"));
$day = 3600 * 24 // seconds in hour time 24 hours

if ( ($logTime + $day) < time() ) // is it over 24 hours old?
{
// yes, so do some stuff
$fp = fopen("_wfiles/logtime.txt","w");
fwrite($fp,time());
fclose($fp);

exec("php -q myscript.php > /dev/null");
}
Also, as a side note, if you are going to parse the file, rename it to a new
file name before you do so, this way the file will not be updated by another
script as you are trying to parse it.
Ok, sorry for the long post, I might have gotten carried away. Also, this
might not be the best way, it was off the top of my head.

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #14
wow! thanks for the snippets and advice. I'll go to work with this and post
back.

Also - I just bought a book - PHP and MySql Web Development (Welling, Thompson)
2nd Ed. - which should help...
One of the things I like to do is have a directory that has write
permisions, so I done have to change permisions on a file by file basis. so
you could create a directory called _wfiles or something similar, and chmod
it 777

then to write data to you log you just need to do this:

$logData = "data to write to log";
$fp = fopen("_wfiles/mylog.txt","a");
fwrite($fp,"$logData\n");
fclose($fp);
now you also asked what format to save the file in, well thats up to you,
since it is your file, and you will be the onlyone reading/parsing it. I
would suggest the | pipe chr as a seperator for items. thus you can do
something like:

$logData = $_SERVER['REMOTE_ADDR'] . "|" .
$_SERVER['REQUEST_METHOD'] . "|" .
$_SERVER['REQUEST_URI'] . "|" .
$_SERVER['HTTP_REFERER'] . "|" .
$_SERVER['HTTP_USER_AGENT'];

(look at the php manual under Predefined Variables for more info)

to spawn a background proccess, try something like:
exec("php -q myscript.php > /dev/null");

As far as scheduling the background proccess, you can't but we can do some
trickery and get to to run if someone clicks a page and it hasnt run yet.
sorta like this:

we write the time we update the log to a file like this:
$fp = fopen("_wfiles/logtime.txt","w");
fwrite($fp,time());
fclose($fp);

we check the time in the file before we update it, if it is older than a set
time, the spawn background
the whole thing might look like:
$logData = $_SERVER['REMOTE_ADDR'] . "|" .
$_SERVER['REQUEST_METHOD'] . "|" .
$_SERVER['REQUEST_URI'] . "|" .
$_SERVER['HTTP_REFERER'] . "|" .
$_SERVER['HTTP_USER_AGENT'];

$fp = fopen("_wfiles/mylog.txt","a");
fwrite($fp,"$logData\n");
fclose($fp);

$logTime = @implode("",@file("_wfiles/logtime.txt"));
$day = 3600 * 24 // seconds in hour time 24 hours

if ( ($logTime + $day) < time() ) // is it over 24 hours old?
{
// yes, so do some stuff
$fp = fopen("_wfiles/logtime.txt","w");
fwrite($fp,time());
fclose($fp);

exec("php -q myscript.php > /dev/null");
}
Also, as a side note, if you are going to parse the file, rename it to a new
file name before you do so, this way the file will not be updated by another
script as you are trying to parse it.
Ok, sorry for the long post, I might have gotten carried away. Also, this
might not be the best way, it was off the top of my head.

--
Mike Bradley
http://www.gzentools.com -- free online php tools

Jul 17 '05 #15
Uzytkownik "CountScubula" <me@scantek.hotmail.com> napisal w wiadomosci
news:CL******************@newssvr27.news.prodigy.c om...
"deko" <dj****@hotmail.com> wrote in message
news:U7******************@newssvr25.news.prodigy.c om...
Sometimes on a shared server, you can still look at the logs, generaly they are readable by all system accounts, try something like this:

test.php
----------------------
<?php
$info =`tail /var/log/httpd/access_log`;
print $info;
?>


On our server the Apache logs have more entries for Red Alert and various
other IIS worms than real user requests. I have completely given up on them
:-(
Jul 17 '05 #16
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:1v********************@comcast.com...
On our server the Apache logs have more entries for Red Alert and various
other IIS worms than real user requests. I have completely given up on them :-(


Hmm, Chung, you strike me as someone who has thier own T1 and a few servers
lying around :)
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #17
Here's the latest - the stats I am still working on are hostname, referring
page, and ISP

suggestions welcome!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>stats</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body style="padding:5%">
<?php
/*
This code to go in every page on my site

stats to get:

* IP address - $visip
hostname - $vishost
* browser - $visbrow
- operating - system $visos
? referring page - $visref
? ISP - $visisp
* date/time of visit - $visdate
* number of page views

files updated:

visdata - visitor information
viscount - number of visits

*/
// identify IP
$visip = $_SERVER['REMOTE_ADDR'];
?>
<br>
$visip =
<?php print $visip;
// identify browser
if((ereg("Nav", getenv("HTTP_USER_AGENT"))) || (ereg("Gold",
getenv("HTTP_USER_AGENT"))) || (ereg("X11", getenv("HTTP_USER_AGENT"))) ||
(ereg("Mozilla", getenv("HTTP_USER_AGENT"))) || (ereg("Netscape",
getenv("HTTP_USER_AGENT"))) AND (!ereg("MSIE", getenv("HTTP_USER_AGENT"))) AND
(!ereg("Konqueror", getenv("HTTP_USER_AGENT"))))
$visbrow = "Netscape";
elseif(ereg("Opera", getenv("HTTP_USER_AGENT")))
$visbrow = "Opera";
elseif(ereg("MSIE", getenv("HTTP_USER_AGENT")))
$visbrow = "MSIE";
elseif(ereg("Lynx", getenv("HTTP_USER_AGENT")))
$visbrow = "Lynx";
elseif(ereg("WebTV", getenv("HTTP_USER_AGENT")))
$visbrow = "WebTV";
elseif(ereg("Konqueror", getenv("HTTP_USER_AGENT")))
$visbrow = "Konqueror";
elseif((eregi("bot", getenv("HTTP_USER_AGENT"))) || (ereg("Google",
getenv("HTTP_USER_AGENT"))) || (ereg("Slurp", getenv("HTTP_USER_AGENT"))) ||
(ereg("Scooter", getenv("HTTP_USER_AGENT"))) || (eregi("Spider",
getenv("HTTP_USER_AGENT"))) || (eregi("Infoseek", getenv("HTTP_USER_AGENT"))))
$visbrow = "Bot";
else $visbrow = "Other";
?>
<br>
$visbrow =
<?php
print $visbrow;
// identify OS
if(ereg("Win", getenv("HTTP_USER_AGENT")))
$visos = "Windows"; // needs to be expanded to get version - e.g.
NT,XP,2000, etc.
elseif((ereg("Mac", getenv("HTTP_USER_AGENT"))) || (ereg("PPC",
getenv("HTTP_USER_AGENT"))))
$visos = "Mac";
elseif(ereg("Linux", getenv("HTTP_USER_AGENT")))
$visos = "Linux";
elseif(ereg("FreeBSD", getenv("HTTP_USER_AGENT")))
$visos = "FreeBSD";
elseif(ereg("SunOS", getenv("HTTP_USER_AGENT")))
$visos = "SunOS";
elseif(ereg("IRIX", getenv("HTTP_USER_AGENT")))
$visos = "IRIX";
elseif(ereg("BeOS", getenv("HTTP_USER_AGENT")))
$visos = "BeOS";
elseif(ereg("OS/2", getenv("HTTP_USER_AGENT")))
$visos = "OS/2";
elseif(ereg("AIX", getenv("HTTP_USER_AGENT")))
$visos = "AIX";
else $visos = "Other";
?>
<br>
$visos =
<?php
print $visos;
// visit date and time
$hourdiff = "0"; // hours difference between server time and local time.
$timeadjust = ($hourdiff * 60 * 60);
$visdate = date("l, F d Y h:i a",time() + $timeadjust);
?>
<br>
$visdate =
<?php
print $visdate;
// identlfy ISP
/*
if ($visip=='') $visip = $_SERVER['REMOTE_ADDR'];
$longisp = @gethostbyaddr($ip);
$visisp = explode('.', $longisp);
$visisp = array_reverse($isp);
$tmp = $isp[1];
if (preg_match("/\<(org?|com?|net)\>/i", $tmp)) {
$visisp = $isp[2].'.'.$isp[1].'.'.$isp[0];
} else {
$visisp = $isp[1].'.'.$isp[0];
}
preg_match("/[0-9]{1,3}\.[0-9]{1,3}/", $myisp) ? return 'unknown' : return
$visisp;
?>
<br>
$visisp =
<?php
print $visisp;

// identify referring page
//function storereferer () {
// global $HTTP_REFERER, $baseurl;
// /*no referer?*/
// if (!isset ($HTTP_REFERER))
// return;
// /*empty referer?*/
// if ($HTTP_REFERER == "")
// return;
// /*referer is from this site?*/
// if (strstr ($HTTP_REFERER, $baseurl))
// return;
// /*Encode quotes and HTML characters.*/
// $referer = htmlspecialchars ($HTTP_REFERER, ENT_QUOTES);


// page views
// viscount will store number of visits - visdata stores visitor info
$file = '/home/post/public_html/viscount';
if ($counter = @file ($file)) {
$line = each($counter);
if (!$counter) {
$counter = 1;
} else {
$counter = $line[value]+1;
}
}
$fp = fopen ($file, "w");
fwrite ($fp, "$counter");
fclose($fp);
?>

</body>
</html>
Jul 17 '05 #18
"deko" <dj****@hotmail.com> wrote in message
news:MU******************@newssvr27.news.prodigy.c om...
Here's the latest - the stats I am still working on are hostname, referring page, and ISP
suggestions welcome!
This code to go in every page on my site
...snip...


You have some good ideas, but the code is really hard to follow, try to
structure it a bit more.
also, just put it in one file, and do an include() on it from your other
pages.

Also, if I may, don't log the host name, this lookup can add considerable
delay, proccess that when you spawn a bg proccess.

If I may also, don't keep diving into functions getenv, use a simple $var =
.... and reference the $var instead, this keeps it easy to modify your code.

just my 2 cents
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #19
no doubt, the code is a mess - this is just a beta hack - will create functions
and clean up when I can first get everything to print to screen...

You say this will result in a delay:

<?php
function gethost($visip) {
$host = `host $visip`;
return (($host ? end ( explode (' ', $host)) : $visip));
}
print gethost($visip);
?>

Do you suggest doing this for all visitor IPs at the end of the day (2:00am) in
a batch process?
If I may also, don't keep diving into functions getenv, use a simple $var =
... and reference the $var instead, this keeps it easy to modify your code.


You mean like this:

$var = getenv("HTTP_USER_AGENT")

<?php print $visip;
// identify browser
if((ereg("Nav", $var)) || (ereg("Gold", $var)) || (ereg("X11", $var)) ||
(ereg("Mozilla", $var))

and so on throughout the script?
....
$visbrow = "Netscape";
elseif(ereg("Opera", getenv("HTTP_USER_AGENT")))
$visbrow = "Opera";
elseif(ereg("MSIE", getenv("HTTP_USER_AGENT")))
$visbrow = "MSIE";
elseif(ereg("Lynx", getenv("HTTP_USER_AGENT")))
$visbrow = "Lynx";
elseif(ereg("WebTV", getenv("HTTP_USER_AGENT")))
$visbrow = "WebTV";
elseif(ereg("Konqueror", getenv("HTTP_USER_AGENT")))
$visbrow = "Konqueror";
...
?>
Jul 17 '05 #20
This prints all the data I'm after to the screen. One improvement I'd like to
make is to identify the Windows OS version - so it will report "Windows XP" or
"Windows 2000", or whatever, instead of just "Windows".

Note - assigning getenv("HTTP_USER_AGENT") to $var did not work.

The next step is to get this to write everything nicely to a file that can be
easily batch uploaded into MySql - and prior to doing so, run a batch process to
get the host name of all the logged IPs. Once issue with that idea, however, is
that most folks are using DHCP so the IP may be stale when the process is run -
but I suppose it will return the same info regardless - unless the IP is now
unassigned...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>stats</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body style="padding:5%">
<?php

/*
This code to go in every page on site

stats to get:

* IP address - $visip
* hostname - $vishost
* browser - $visbrow
* operating - system $visos
* referring page - $visref
* date/time of visit - $visdate
* number of page views

files updated:

* visdata - visitor information
* viscount - number of visits

*/

// identify IP
$visip = $_SERVER['REMOTE_ADDR'];
?>
<br>
$visip =
<?php print $visip;
// identify browser and OS
// this did not work:
//$var = getenv("HTTP_USER_AGENT")
if((ereg("Nav", getenv("HTTP_USER_AGENT"))) || (ereg("Gold",
getenv("HTTP_USER_AGENT"))) || (ereg("X11", getenv("HTTP_USER_AGENT"))) ||
(ereg("Mozilla", getenv("HTTP_USER_AGENT"))) || (ereg("Netscape",
getenv("HTTP_USER_AGENT"))) AND (!ereg("MSIE", getenv("HTTP_USER_AGENT"))) AND
(!ereg("Konqueror", getenv("HTTP_USER_AGENT"))))
$visbrow = "Netscape";
elseif(ereg("Opera", getenv("HTTP_USER_AGENT")))
$visbrow = "Opera";
elseif(ereg("MSIE", getenv("HTTP_USER_AGENT")))
$visbrow = "MSIE";
elseif(ereg("Lynx", getenv("HTTP_USER_AGENT")))
$visbrow = "Lynx";
elseif(ereg("WebTV", getenv("HTTP_USER_AGENT")))
$visbrow = "WebTV";
elseif(ereg("Konqueror", getenv("HTTP_USER_AGENT")))
$visbrow = "Konqueror";
elseif((eregi("bot", getenv("HTTP_USER_AGENT"))) || (ereg("Google",
getenv("HTTP_USER_AGENT"))) || (ereg("Slurp", getenv("HTTP_USER_AGENT"))) ||
(ereg("Scooter", getenv("HTTP_USER_AGENT"))) || (eregi("Spider",
getenv("HTTP_USER_AGENT"))) || (eregi("Infoseek", getenv("HTTP_USER_AGENT"))))
$visbrow = "Bot";
else $visbrow = "unknown";
?>
<br>
$visbrow =
<?php
print $visbrow;
// identify OS
if(ereg("Win", getenv("HTTP_USER_AGENT")))
$visos = "Windows"; // needs to be expanded to get version - e.g.
NT,XP,2000, etc.
elseif((ereg("Mac", getenv("HTTP_USER_AGENT"))) || (ereg("PPC",
getenv("HTTP_USER_AGENT"))))
$visos = "Mac";
elseif(ereg("Linux", getenv("HTTP_USER_AGENT")))
$visos = "Linux";
elseif(ereg("FreeBSD", getenv("HTTP_USER_AGENT")))
$visos = "FreeBSD";
elseif(ereg("SunOS", getenv("HTTP_USER_AGENT")))
$visos = "SunOS";
elseif(ereg("IRIX", getenv("HTTP_USER_AGENT")))
$visos = "IRIX";
elseif(ereg("BeOS", getenv("HTTP_USER_AGENT")))
$visos = "BeOS";
elseif(ereg("OS/2", getenv("HTTP_USER_AGENT")))
$visos = "OS/2";
elseif(ereg("AIX", getenv("HTTP_USER_AGENT")))
$visos = "AIX";
else $visos = "unknown";
?>
<br>
$visos =
<?php
print $visos;
// visit date and time
$hourdiff = "1"; // hours difference between server time and local time.
$timeadjust = ($hourdiff * 60 * 60);
$visdate = date("l, F d Y h:i a",time() + $timeadjust);
?>
<br>
$visdate =
<?php
print $visdate;
?>
<br>
gethost($visip) =
<?php
//identify hostname - run this as a batch process once a day for all visitor IPs
//otherwise will result in delay in loading page while info is sought over the
net
//note backticks can cause error when php is run in safe mode
function gethost($visip) {
$host = `host $visip`;
return (($host ? end ( explode (' ', $host)) : $visip));
}
print gethost($visip);
//identify referring page
$referer = getenv("HTTP_REFERER")
?>
<br>
$referer =
<?php
print $referer;
?>
</body>
</html>
Jul 17 '05 #21
http://www.gzentools.com -- free online php tools
"deko" <dj****@hotmail.com> wrote in message
news:L0******************@newssvr27.news.prodigy.c om...
no doubt, the code is a mess - this is just a beta hack - will create functions and clean up when I can first get everything to print to screen...

You say this will result in a delay:

<?php
function gethost($visip) {
$host = `host $visip`;
return (($host ? end ( explode (' ', $host)) : $visip));
}
print gethost($visip);
?>

Do you suggest doing this for all visitor IPs at the end of the day (2:00am) in a batch process?
If I may also, don't keep diving into functions getenv, use a simple $var = ... and reference the $var instead, this keeps it easy to modify your
code.
You mean like this:

$var = getenv("HTTP_USER_AGENT")

<?php print $visip;
// identify browser
if((ereg("Nav", $var)) || (ereg("Gold", $var)) || (ereg("X11", $var)) || (ereg("Mozilla", $var))

and so on throughout the script?
...
$visbrow = "Netscape";
elseif(ereg("Opera", getenv("HTTP_USER_AGENT")))
$visbrow = "Opera";
elseif(ereg("MSIE", getenv("HTTP_USER_AGENT")))
$visbrow = "MSIE";
elseif(ereg("Lynx", getenv("HTTP_USER_AGENT")))
$visbrow = "Lynx";
elseif(ereg("WebTV", getenv("HTTP_USER_AGENT")))
$visbrow = "WebTV";
elseif(ereg("Konqueror", getenv("HTTP_USER_AGENT")))
$visbrow = "Konqueror";
...
?>

yes do it in a batch instead of live

$host = gethostbyaddr($visp); // built in php function

--
Mike Bradley

Jul 17 '05 #22
> also, just put it in one file, and do an include() on it from your other
pages.


Do you mean that I don't need to embed the code in an html page? So, I can have
a file in public_html named "visistats.php? and then just call a function from
that file into index.php, or whatever.php? What does teh syntax look like?
What if someone trys to access http://www.domain.com/visistats.php?
Jul 17 '05 #23
Worse, I work at a large university. The server is connected via a gigabit
link to a network with several thousands poorly protected Windows boxes.

Uzytkownik "CountScubula" <me@scantek.hotmail.com> napisal w wiadomosci
news:8n******************@newssvr29.news.prodigy.c om...
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:1v********************@comcast.com...
On our server the Apache logs have more entries for Red Alert and various other IIS worms than real user requests. I have completely given up on them
:-(


Hmm, Chung, you strike me as someone who has thier own T1 and a few

servers lying around :)
--
Mike Bradley
http://www.gzentools.com -- free online php tools

Jul 17 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: toedipper | last post by:
Hello, I am developing a site using php and mysql and I would like to be able to let visitors know how many other visitors are currently browsing the site i.e. 'There are currently x amount of...
5
by: cc | last post by:
From the popular website At this link: http://www.w3schools.com/asp/asp_globalasa.asp I got this code, but it does not work on my server... I can open three IE browsers and watch the count go to...
8
by: gil | last post by:
I'm trying to track how many users are visiting a site and how may are logged in, using application sessions like so: Sub Session_OnStart Session.Timeout = 20 Session("Start") = Now...
2
by: enterthe | last post by:
Hey, I have lots of visitors to my site from search engines, but they don't always get sent to the right page. So... based on the search query of the refering search page (e.g. google or...
0
by: ip2location.inc | last post by:
IP2Location has announced the availability of IP2Location .NET Component, that enables application developers to geolocation the Internet visitors by IP Address from their .NET applications. ...
3
by: johnny | last post by:
hi all! I am starting to study the best way to track site visitors. Logfiles stats which come with every web hosting, have little metrics to be analyzed and also problems with cached pages which...
4
by: Chris | last post by:
Hi, i want to get the amount of visitors online. I did this in global.asax I tested it by opening several browser sessions. The amount increases, but never decreases, when i close some...
6
by: tenxian | last post by:
It is said as for code, developing a website that can stand 10,000 concurrent visitors is much different from developing a website that can stand 1,000 concurrent visitors, is it true?
0
by: Steven Cheng [MSFT] | last post by:
Hi Jonathan, Does the starter kits helps some? Sincerely, Steven Cheng Microsoft MSDN Online Support Lead
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.