Logs....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Logs....

Post by Dale »

I have a little thing that logs who comes on my site and heres what it record's:

$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$user = $_SERVER['PHP_AUTH_USER'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];

Question...
Are there anymore things that can be added in the top bit?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

foreach($_SERVER as $value){
// log $value
}
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I have read the manual of foreach but i still don't understand. Could anyone tell me how you use it, when you use it and how does it work? please....
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

it loops thru every value of an array...

Code: Select all

$array = array(
"one" => "1",
"two" => "2");

foreach($array as $value){
echo $value;
// would print "12"
}

foreach($array as $key => $value){
echo $key,$value;
//would print one1two2
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

each time you add something to an array it is simply appended (or replaced if the index already existed) no matter what the index was.
$arr = array();
$arr[4] = "a"; // $arr <-> [ 4=> "a"]
$arr[1] = "b"; // $arr <-> [ 4=> "a", 1=>"b"]
and so on...
using print_r you can see this each()-order

foreach walk the array in this each()-order assigning in each iteration the current entry value to a variable (or with '...as $key=>$value' the index as well) and executes the loop-body
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Thank you... :D
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

eh???

In the header of every page is this:

Code: Select all

<?php 
    $agent = $_SERVER&#1111;'HTTP_USER_AGENT']; 
    $uri = $_SERVER&#1111;'REQUEST_URI']; 
    $user = $_SERVER&#1111;'PHP_AUTH_USER']; 
    $ip = $_SERVER&#1111;'REMOTE_ADDR']; 
    $ref = $_SERVER&#1111;'HTTP_REFERER']; 
    $dtime = date('r'); 
     
    if($ref == "")&#123; 
        $ref = "None"; 
    &#125; 
    if($user == "")&#123; 
        $user = "None"; 
    &#125; 
     
    $entry_line = "-=&#1111; $dtime ]=-
Agent: $agent
IP: $ip
URL: $uri
Referrer: $ref

\n"; 
    $fp = fopen("/usr/local/etc/httpd/dale.unrealism.com/logs.txt", "a"); 
    fputs($fp, $entry_line); 
    fclose($fp); 
?>
So whats that code thing about then and where do i insert it...
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

<?
$message = date('r')."\n";
foreach($_SERVER as $key => $value)&#123;
$message .= $key," :",$value,"\n";
&#125;
$message .= "\n";

    $fp = fopen("/usr/local/etc/httpd/dale.unrealism.com/logs.txt", "a"); 
    fputs($fp, $message); 
    fclose($fp); 

?>
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Thx M8 ;)
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Parse error: parse error, unexpected ',' in /usr/local/etc/httpd/dale.unrealism.com/menu.php on line 4
Line 4:

Code: Select all

$message .= $key," :",$value,"\n";
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

$message .= $key." :".$value."\n";
sorry
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Neat Script!


Is there a way of adding:
===============================================
At the bottom.. so it seperates them in the TXT file?
User avatar
theChosen
Forum Newbie
Posts: 15
Joined: Sun Aug 18, 2002 11:00 am
Location: RO, Europe

Post by theChosen »

Code: Select all

<? 
$message = date('r')."\n"; 
foreach($_SERVER as $key => $value)&#123; 
$message .= $key." :".$value."\n"; 
&#125; 
$message.="=======================================\n";
$message .= "\n"; 

    $fp = fopen("/usr/local/etc/httpd/dale.unrealism.com/logs.txt", "a"); 
    fputs($fp, $message); 
    fclose($fp); 

?>
parts (c) hob_goblin :)
also check
http://www.php.net/manual/en/function.str-pad.php
http://www.php.net/manual/en/function.str-repeat.php
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Cheers ;)
Post Reply