StirCrazy Posted July 6, 2006 Share Posted July 6, 2006 Hope someone can help ~ I've set up a function to store the users IP address (and proxy it using one) in $ip and $proxy.below works if I work it directly into the script but not when I get_IP() Any ideas why? am I meant to specify what variables it returns with?Cheers,S.c>function get_IP() { if ($_SERVER["HTTP_X_FORWARDED_FOR"]) { if ($_SERVER["HTTP_CLIENT_IP"]) {$proxy = $_SERVER["HTTP_CLIENT_IP"];} else {$proxy = $_SERVER["REMOTE_ADDR"];}$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];} else { if ($_SERVER["HTTP_CLIENT_IP"]) {$ip = $_SERVER["HTTP_CLIENT_IP"];} else {$ip = $_SERVER["REMOTE_ADDR"];}}// $ip = Real IP// if $proxy has a value it is the Proxy IP return;} Quote Link to comment https://forums.phpfreaks.com/topic/13867-calling-variables-from-function/ Share on other sites More sharing options...
kenrbnsn Posted July 6, 2006 Share Posted July 6, 2006 Yes, you need to specify what it returns:[code]<?phpfunction get_IP() { if ($_SERVER["HTTP_X_FORWARDED_FOR"]) { if ($_SERVER["HTTP_CLIENT_IP"]) {$proxy = $_SERVER["HTTP_CLIENT_IP"];} else {$proxy = $_SERVER["REMOTE_ADDR"];}$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];} else { if ($_SERVER["HTTP_CLIENT_IP"]) {$ip = $_SERVER["HTTP_CLIENT_IP"];} else {$ip = $_SERVER["REMOTE_ADDR"];}}// $ip = Real IP// if $proxy has a value it is the Proxy IP return ($ip);}$ip = get_IP();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13867-calling-variables-from-function/#findComment-53993 Share on other sites More sharing options...
wildteen88 Posted July 6, 2006 Share Posted July 6, 2006 If you want to use $proxy and $ip outside of the function you'll have to make them global eg:[code]function get_IP() { global $proxy, $ip[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13867-calling-variables-from-function/#findComment-53994 Share on other sites More sharing options...
Wildbug Posted July 6, 2006 Share Posted July 6, 2006 Are you expecting the variables $ip and $proxy to be visible to the rest of the script once it returns? If so, you either have to have declared them globally or return them (and I think you'd have to return them in an array or list form if you want both).See [url=http://www.php.net/manual/en/functions.returning-values.php]this page[/url] for an example of returning multiple values from an array (second example).If $ip and $proxy are global in scope, then you need a "global $ip,$proxy;" line in your subroutine (at the beginning). Quote Link to comment https://forums.phpfreaks.com/topic/13867-calling-variables-from-function/#findComment-53996 Share on other sites More sharing options...
StirCrazy Posted July 6, 2006 Author Share Posted July 6, 2006 Nice one: all of you :DStill a novice at this kinda thing ~ just to clarify :: if I had this in the main script, how would I get_IP() both variables.//-- set variables ready for fraud cookie ////-- START --// $fraud1=$username; get_IP();if (!isset($proxy)) { $fraud2 = $ip . "|" . "none";} else { $fraud2=$ip . "|" . $proxy;}//-- END --// Quote Link to comment https://forums.phpfreaks.com/topic/13867-calling-variables-from-function/#findComment-54006 Share on other sites More sharing options...
Wildbug Posted July 6, 2006 Share Posted July 6, 2006 [code]@ list($ip,$proxy) = get_IP();function get_IP() { if ($_SERVER["HTTP_X_FORWARDED_FOR"]) { $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; $proxy = $_SERVER["HTTP_CLIENT_IP"] ? $_SERVER["HTTP_CLIENT_IP"] : $proxy = $_SERVER["REMOTE_ADDR"]; } else { $ip = $_SERVER["HTTP_CLIENT_IP"] ? $_SERVER["HTTP_CLIENT_IP"] : $_SERVER["REMOTE_ADDR"]; $proxy = null; } return array($ip,$proxy);}[/code]Again, see the manual, [url=http://www.php.net/manual/en/functions.returning-values.php]Example 17-12[/url], for returning multiple values from a function. Quote Link to comment https://forums.phpfreaks.com/topic/13867-calling-variables-from-function/#findComment-54127 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.