Jump to content

Archived

This topic is now archived and is closed to further replies.

StirCrazy

calling variables from function?

Recommended Posts

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

Share this post


Link to post
Share on other sites
Yes, you need to specify what it returns:
[code]<?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();
?>[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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 --//

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.