Jump to content


Photo

calling variables from function?


  • Please log in to reply
5 replies to this topic

#1 StirCrazy

StirCrazy
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 06 July 2006 - 04:54 PM

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;
}

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 July 2006 - 04:56 PM

Yes, you need to specify what it returns:
<?php
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 ($ip);
}

$ip = get_IP();
?>


#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 July 2006 - 04:58 PM

If you want to use $proxy and $ip outside of the function you'll have to make them global eg:
function get_IP() {
    global $proxy, $ip


#4 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 06 July 2006 - 04:59 PM

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 this page 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).

Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#5 StirCrazy

StirCrazy
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 06 July 2006 - 05:10 PM

Nice one: all of you :D

Still 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 --//

#6 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 06 July 2006 - 08:37 PM

@ 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);
}

Again, see the manual, Example 17-12, for returning multiple values from a function.
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users