Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: php - My own access log |
|
Author |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Sun 07 May '06 21:09 Post subject: php - My own access log |
|
|
If you are not your own ISP mostly you don't can access in the log files. But for real optimising your webpage you have to know from where did the user come from and where did he exit your pages.
That causes we will build our own access.log
Listening 1
Code: |
<?
$ref=$_SERVER['HTTP_REFERER']; //where does he come from
$user=$_SERVER['HTTP_USER_AGENT']; //Browser?
$wo=$_SERVER['REQUEST_URI']; //current page?
$wann=date("D M d, Y G:i"); //when?
$ip = $_SERVER["REMOTE_ADDR"]; //Who?
?>
|
Users get their IPs often dynamicly and maybe behind a simply proxy
Listening 2
Code: |
<?
if($_SERVER['HTTP_X_FORWARDED_FOR'] != ""){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
$proxy = $_SERVER["REMOTE_ADDR"];
$host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
}else{
$ip = $_SERVER["REMOTE_ADDR"];
$host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
$proxy = "none";
}
?>
|
Now we have to save the data.
Listining 3
Code: |
<?
$m_user='user.txt'; //Where save it?
$input =$wann;
$input .=" on page:";
$input .=$wo;
$input .=" come from: ";
$input .=$ref;
$input .=" User ";
$input .=$ip;
$input .=" ";
$input .=$user;
$input .=" Proxy: ";
$input .=$proxy;
$input .=" Host: ";
$input .=$host;
$f=fopen($m_user, 'a'); //If there is no file create it.
fputs($f,$input ."\n");
fclose($f);
?>
|
Now patch that all together
Listining 4 log.php
Code: |
<?
$m_user='user.txt';
//if(!file_exists($m_user)){$fh=fopen($m_user, 'w'); fclose($fh);}
$ref=$_SERVER['HTTP_REFERER'];
$user=$_SERVER['HTTP_USER_AGENT'];
$wo=$_SERVER['REQUEST_URI'];
$wann=date("D M d, Y G:i");
if($_SERVER['HTTP_X_FORWARDED_FOR'] != ""){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
$proxy = $_SERVER["REMOTE_ADDR"];
$host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
}else{
$ip = $_SERVER["REMOTE_ADDR"];
$host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
$proxy = "keiner";
}
$input =$wann;
$input .=" auf Seite:";
$input .=$wo;
$input .=" kam von: ";
$input .=$ref;
$input .=" Besucher: ";
$input .=$ip;
$input .=" ";
$input .=$user;
$input .=" Proxy: ";
$input .=$proxy;
$input .=" Host: ";
$input .=$host;
$f=fopen($m_user, 'a');
fputs($f,$input ."\n");
fclose($f);
?>
|
Now put the access log into owr page
Listening 5
Code: |
<?
// mein stuff
......
//
include "log.php";
?>
|
If there is much traffic you better save the datas in a Database |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Sun 07 May '06 21:18 Post subject: |
|
|
For more traffic the MySQL variant
The '' are single ' not "
Code: |
CREATE TABLE stats (
id int(11) NOT NULL auto_increment,
wann varchar(22) NOT NULL default '',
wo varchar(50) NOT NULL default '',
ref varchar(100) NOT NULL default '',
ip varchar(15) NOT NULL default '',
user varchar(100) NOT NULL default '',
proxy varchar(15) NOT NULL default '',
host varchar(50) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY (id),
KEY id_key(id),
KEY wann_key(wann),
KEY wo_key(wo),
KEY ref_key(ref),
KEY ip_key(ip),
KEY user_key(user),
KEY proxy_key(proxy),
KEY host_key(host)
)
TYPE=MyISAM;
|
Against SQL-INJECTION mysql_escape_string();
Code: |
<?php
@mysql_query(sprintf("INSERT INTO %s('wann','wo','ref','ip','user','proxy','host') VALUES('%s','%s','%s','%s','%s','%s','%s')",
$table,
mysql_escape_string($wann),
mysql_escape_string($wo),
mysql_escape_string($ref),
mysql_escape_string($ip),
mysql_escape_string($user),
mysql_escape_string($proxy),
mysql_escape_string($host)
), $link)
or die_error(mysql_error($link));
?>
|
Reload-Blocker
Code: |
<?php
//some IP time thing
if(mysql_num_rows($result_von_vorher)==0){
//Daten eintragen
@mysql_query("INSERT INTO...");
}
|
What does the log show
Usefull is the my mysql Function count(); und GROUP BY
Don't forget, that I used the variable user for the browser.
Code: |
<?PHP
$result= @mysql_query(sprintf("SELECT user, count(*) AS count GROUP BY user ORDER BY count DESC",
$table
), $link)
or die_error(mysql_error($link));
?>
|
I'd like to see your complete solutions. |
|
Back to top |
|
|
|
|
|
|