Forum Moderators: coopster

Message Too Old, No Replies

getting viewer ip address

gettin ip, storing it in a global

         

monsto

10:34 pm on Oct 25, 2003 (gmt 0)

10+ Year Member



I wanna get the ip address, and output it later on. so i got this here li'l function:


function getIP() {
// Get the users ip address.
// called and updated when they download or log in
unset($GLOBALS["USERIP"]);
$tmpip;
if ($_SERVER['HTTP_CLIENT_IP']) $tmpip = $_SERVER['HTTP_CLIENT_IP'];
else if ($_SERVER['REMOTE_ADDR']) $tmpip = $_SERVER['REMOTE_ADDR'];
else if ($_SERVER['HTTP_X_FORWARDED_FOR']) $tmpip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else $tmpip = "UNKNOWN";
$GLOBALS["USERIP"] = long2ip($tmpip);
}

later on i do this


<? GLOBAL $USERIP;
print($USERIP . "testes")?>Add a comment to "<?= htmlspecialchars($torrow["name"]);?>

$torrow["name"] is irrelevant. (that part works)

the output is basically:

testesAdd a comment to "Snow White part 2"

but i'm trying to get

255.255.255.255testesAdd a comment to "Snow White part 2"

anyone got tips on what i missed?

[edited by: monsto at 11:24 pm (utc) on Oct. 25, 2003]

dreamcatcher

11:20 pm on Oct 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a thought, but shouldn`t

//return $ip;

be

//return $tmpip;

As I see it, you aren`t actually passing anything back to the main program. I see its commented out, so maybe its not that.

monsto

11:23 pm on Oct 25, 2003 (gmt 0)

10+ Year Member



the 'return' was commented out in favor of using the global which is set on the line above. (i'll delete that line to prevernt further conf)

dreamcatcher

11:40 pm on Oct 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I think the reason its not working is because you aren`t passing anything back from the function. Things declared inside a function are only available inside a function unless you use "return". The more experienced coders will be able to help you.

adamas

12:42 pm on Oct 27, 2003 (gmt 0)

10+ Year Member



Try

if( array_key_exists('HTTP_CLIENT_IP', $_SERVER) ) $tmpip = $_SERVER['HTTP_CLIENT_IP'];
etc
etc
etc

Although I am guessing here as I don't have a machine with php to hand to check what happens when you look for a key that doesn't exist.

If it doesn't work then the first thing I'd do to debug this is to replace the '$tmpip = ' parts with a print statement to see which one, if any is getting executed i.e

if ($_SERVER['HTTP_CLIENT_IP']) print('Executing the HTTP_CLIENT_IP clause - ' . $SERVER['HTTP_CLIENT_IP']);

monsto

4:45 pm on Oct 27, 2003 (gmt 0)

10+ Year Member



excellent info with the print statements. i'll try that

edit: adamas since you didn't mention much else, i'm assuming that i've set and called the global correctly, and all the other crap looks right, afa you're concerned...

vincevincevince

5:12 pm on Oct 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



in the main (not function) code put

global $userid;

in a place before you first call your function getIP

adamas

1:37 pm on Oct 28, 2003 (gmt 0)

10+ Year Member



monsto: I didn't comment on your use of globals because it isn't the method I use and didn't feel qualified to comment on that.

I use, I believe, the same method as vincevincevince. Declare a global variable outside of any functions


global $globalVar;

and then in the function where you wish to use the global -

function UsesGlobalVar() {
global $globalVar;

// miscellaneous code.

$globalVar = $whatever + $thingumajig;

// rest of function.
}

(Disclaimer: done purely from memory and I've done all content and no coding for several weeks. I recommend looking up the syntax to check)