Jump to content

calling variables from function?


StirCrazy

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;
}
Link to comment
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]
Link to comment
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).
Link to comment
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 --//
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.