NYCPHP Meetup

NYPHP.org

[nycphp-talk] variable scope question

mikesz at qualityadvantages.com mikesz at qualityadvantages.com
Sun Feb 4 22:12:37 EST 2007


Jürgen Wind wrote:

>
>mikesz at qualityadvantages.com wrote:
>  
>
>>Hello everyone,
>>
>>I have a question about variable scope that I probably should know the 
>>answer to but it is a puzzle at the moment.
>>
>>I have a packaged script that I work on that has this problem but I can 
>>simplify the question with an example.
>>
>>I have a scipt routine that collects a user IP address from  
>>$_SERVER['REMOTE_ADDR']. If I have $ip = $_SERVER['REMOTE_ADDR']; 
>>defined in the script, no problem but if I put the definition in an 
>>include statement like "require_once" it has no value but if I put the 
>>definition in a function in the include file and call the function, it 
>>contains the user IP address. By the way, the mod I am doing is for 
>>tracking badguys activity on a client site so I can see what parts of 
>>the script they are trying to access. I am collecting a lot more than 
>>just the ip address but this is a simplified example.
>>
>>In the case I am working on, the definitions are in a header file and 
>>the IP collector is at the top of all the pages of the script. I have 
>>other "hardcoded" values defined in the header file that work?
>>
>>Any ideas on what my problem is? I have created a function in the header 
>>file as a work around but I am wondering why the simple definition does 
>>not get parsed or why some variables work and others do not?
>>
>>If the answer is obvious and I just don't get it, I hope someone can 
>>enlighten me. Thanks in advance.
>>
>>mikesz
>>
>>
>>
>>_______________________________________________
>>New York PHP Community Talk Mailing List
>>http://lists.nyphp.org/mailman/listinfo/talk
>>
>>NYPHPCon 2006 Presentations Online
>>http://www.nyphpcon.com
>>
>>Show Your Participation in New York PHP
>>http://www.nyphp.org/show_participation.php
>>
>>
>>    
>>
>why don't just use $_SERVER['REMOTE_ADDR'] everywhere in your script?
>it's a "superglobal".
>  
>
thanks for the reply, that would not be too practical as it would need 
to be distributed over 50 pages (its a packaged app that I am doing some 
integration on) here is the code that would need to be included on each 
page:

$visitor_host = @getHostByAddr( $ip );
$ip                                = $_SERVER['REMOTE_ADDR'];
$referer               = $_SERVER['HTTP_REFERER'];       // URL of 
calling page
$refer                = $_SERVER['HTTP_REFERER'];       // URL of 
calling page
$rhost                   = $_SERVER['REMOTE_HOST'];           // host 
name of visitor
$ruser                   = $_SERVER['REMOTE_USER'];        // Remote 
Username if available
$user                        = $_SERVER['PHP_AUTH_USER'];             
//current user
$user_agent             = $_SERVER['HTTP_USER_AGENT'];
$uri                             = $_SERVER['REQUEST_URI'];
$user                         = $_SERVER['PHP_AUTH_USER'];
$proxyIP                     = $_SERVER['HTTP_X_FORWARDED_FOR'];
$proxyhost                = $_SERVER['HTTP_X_FORWARDED_HOST'];
$proxyserver            = $_SERVER['HTTP_X_FORWARDED_SERVER'];
$request_method     = $_SERVER['REQUEST_METHOD'];
$server_protocol     = $_SERVER['SERVER_PROTOCOL'];
$url                          = $_SERVER['HTTP_HOST']. $_SERVER[PHP_SELF];

$sql  =  "INSERT INTO MyTracker VALUES('',now(), 
'$ip','$referer','$user_agent','$rhost','$ruser','$ruri','$request_method','$server_protocol','$proxyIP','$proxyserver','$proxyhost','$url', 
'$visitor_host');";

$results  =  mysql_query ( $sql);

as you can see, that would get pretty unwieldy in a short time, 
expecially if I needed to change something in this call...

regards, mikesz



More information about the talk mailing list