PHP question - using $_SERVER variables within a called function for logging page hits?

Adam S.

New member
So I'm writing a snipped of PHP code to log hits to my web site.

If I put the PHP code in-line, I can refer to $_SERVER['REQUEST_URI'] and $REMOTE_ADDR and the like and it writes it to the database properly.

If I put the code in a function, however, these variables get written to the database as null.

I'd rather put the code in a function than copy the code into every page I'm tracking. What's the elegant solution to this, minimizing the cut-and-paste code on every page?

Thanks!
 
Well, i read your request. Sometimes it might be a miscall, or a type-o that cause blank feed from a function. But i typed out the following functions and classes in an effort to assist you hope you find them useful.
The following code would return REQUEST_URI:

<?php
/*
Source Code:geturi.php;
Author: Andron Smith
Purpose: Yahoo Answers
*/
function getURI() {
$myuri = $_SERVER['REQUEST_URI'];
return $myuri;
}

?>

the following function would return the IP address of the visitor:

<?php
/*
Source Code:getip.php;
Author: Andron Smith
Purpose: Yahoo Answers
*/
function getUserIP(){
$userIP = $_SERVER['REMOTE_ADDR'];
return $userIP;
}
?>

you may want to post everything in a class:

<?php
/*
Source Code: GSD.PHP
Author: Andron Smith
Purpose: Yahoo Answers
*/
class GetServerDetails {

public function getURI() {
$myURI = $_SERVER['REQUEST_URI'];
return $myURI;
}

public function getIP() {
$userIP = $_SERVER['REMOTE_ADDR'];
return $userIP;
}


}


?>
<?php
/*
Source Code: example.php
Author: Andron Smith
Purpose: Yahoo Answers
*/
include "GSD.php";

$GSD = new GetServerDetails();

echo $GSD->getURI();
?>
if you want it as just a function just go with the functions with out the class wrapper.
 
Back
Top