Monkuar Posted December 20, 2014 Share Posted December 20, 2014 (edited) This code was extracted from phpwebsockets server from google: All I am trying to do is grab the USERS IP ADDRESS. $clientIP is returning: "2130706433" Which makes NO SENSE WHATSOEVER (I'm on localhost) AND WHY is the $clientIP variable SET TO NOTHING before the $result? How the fuck does it get 2130706433 from $clientIp when the variable is fucking set to = ''? That makes ABSOLUTELY no sense whatsoever. Then it gets passed to socket_getpeername? The SECOND parameter for that NEEDS A HOST, HOW can the HOST be = ''? Are you telling me this phpwebsockets server is for localhost development only? BECAUSE if that is the case, EACH new connection will have the same $clientIP = ''; THAT MAKES NO SENSE? It should be the USERS REMOTE IP ADDRESS??. I know ip2long is doing something and converting it. All I want is the regular IPV4 ADDRESS, is it really that hard to ask? Full function: http://pastebin.com/u9fNMcBf Literally, how hard is it to grab the users ip from the main socket connection? This is killing development time, what a fucking joke. I am infuriated. Should have never started using php as a websocket server. // fetch client IP as integer $clientIP = ''; $result = socket_getpeername($client, $clientIP); $clientIP = ip2long($clientIP); $clientIP2 = 'test'; if ($result !== false && $wsClientCount < WS_MAX_CLIENTS && (!isset($wsClientIPCount[$clientIP]) || $wsClientIPCount[$clientIP] < WS_MAX_CLIENTS_PER_IP)) { wsAddClient($client, $clientIP); } Edited December 20, 2014 by mac_gyver rude language fixed Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/ Share on other sites More sharing options...
bsmither Posted December 20, 2014 Share Posted December 20, 2014 Allow me to make an assumption without checking the manual on any of it: * socket_getpeername() is taking it's second parameter by reference. Thus, there should be something to reference (a zero-length string) Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500215 Share on other sites More sharing options...
Monkuar Posted December 20, 2014 Author Share Posted December 20, 2014 (edited) Allow me to make an assumption without checking the manual on any of it: * socket_getpeername() is taking it's second parameter by reference. Thus, there should be something to reference (a zero-length string) Okay, can accept that. // fetch client IP as integer $clientIP = ''; $test = ''; $result = socket_getpeername($client, $clientIP); $clientIP = ip2long($clientIP); //This returns 1 $result2 = socket_getpeername($client, $test); Now #result2 is returning "1". What does 1 mean? True? Or 1 because the socket is being run on localhost? Edited December 20, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500216 Share on other sites More sharing options...
Monkuar Posted December 20, 2014 Author Share Posted December 20, 2014 (edited) Okay, no idea. But I got it to work. I have no IDEA how 1 can convert to 2130706433. But in any event. I just use http://php.net/manual/en/function.ip2long.php and it converted it into 127.0.0.1. Grrr. Sorry for the frustration. Edited December 20, 2014 by mac_gyver rude language fixed Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500219 Share on other sites More sharing options...
ginerjm Posted December 20, 2014 Share Posted December 20, 2014 Frustration? You are apologizing for you "frustration"? How about your language? 1 Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500220 Share on other sites More sharing options...
bsmither Posted December 20, 2014 Share Posted December 20, 2014 (edited) "#result2 is returning "1". What does 1 mean? True?" When attempting to print a boolean value (true, false), PHP (I assume) converts them to a string that the equivalent printable value is '1' for true and a zero-length string for false (you might think it would be '0' (zero), but no). Why is socket_getpeername() returning a boolean? If a parameter is being passed by reference, the value assigned to that parameter is your actual return. Edited December 20, 2014 by bsmither Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500222 Share on other sites More sharing options...
kicken Posted December 20, 2014 Share Posted December 20, 2014 Watch your language. There's no need for the swearing. ip2long converts an IP address from it's string representation (ie, 127.0.0.1) into a raw 32-bit (for ipv4) number (2130706433). If you want the readable format (127.0.0.1) then why are you calling this function at all? There's also no need to be calling socket_getpeername twice, once is plenty. It will populate the variable passed in the second parameter with the remote end's IP address. Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500223 Share on other sites More sharing options...
Ch0cu3r Posted December 21, 2014 Share Posted December 21, 2014 Which makes NO SENSE WHATSOEVER (I'm on localhost) AND WHY is the $clientIP variable SET TO NOTHING before the $result? How the fuck does it get 2130706433 from $clientIp when the variable is fucking set to = ''? You could of answered that question yourself by looking at the php documentation for the functions used in the code you posted. Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500251 Share on other sites More sharing options...
Monkuar Posted December 21, 2014 Author Share Posted December 21, 2014 (edited) You could of answered that question yourself by looking at the php documentation for the functions used in the code you posted. Huh? The documentation is as follows: bool[/size] [/size]socket_getpeername[/size] ( [/size]resource $socket[/size] , [/size]string &$address[/size] [, [/size]int &$port[/size] ] )[/size] And Address: If the given socket is of type AF_INET or AF_INET6, socket_getpeername() will return the peers (remote) IP address in appropriate notation (e.g. 127.0.0.1 or fe80::1) in the address parameter and, if the optional port parameter is present, also the associated port. If the given socket is of type AF_UNIX, socket_getpeername() will return the Unix filesystem path (e.g. /var/run/daemon.sock) in theaddress parameter This function is returning '1', which is meant to be localhost I'm using? That doesn't make sense to me, it should be returning the address the documentation states. This function literally is misinterpreted and doesn't actually work as intended. // fetch client IP as integer $clientIP = ''; $clientIP2 = ''; $result = socket_getpeername($client, $clientIP); $clientIP = ip2long($clientIP); How does $clientIP return my address (my up) when the original variable is SET TO = ''? (BLANK, NADA, ZIP). I can see how the $result works, because $client is linked to my websocket message: $client = socket_accept($wsRead[0]);But you guys are not getting my original point: // listen socket changed $client = socket_accept($wsRead[0]); if ($client !== false) { // fetch client IP as integer $clientIP = ''; $clientIP2 = ''; $result = socket_getpeername($client, $clientIP); $clientIP = ip2long($clientIP);This is not making sense, $clientIP variable is SET to '', but it's still returning my ip adresss (which is fine, from $client [the websocket message buffer], but how is ip2long reading a $clientIP variable that is SET to = '' and returning localhost? The only thing that I can get from this is that: The $clientIP variable that is passed in socket_getpeername is somehow magically storing the ip address? Lol what? How is that possible? If that is true because it's being passed by socket_getpeername, where the hell does it say that $clientIP will be stored the IP? No where in the php documentation does it say that. Edited December 21, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500259 Share on other sites More sharing options...
bsmither Posted December 21, 2014 Share Posted December 21, 2014 "socket_getpeername() will return ... in the address parameter" (Emphasis is mine.) The passing by reference into a parameter is one way to return a value. "how is ip2long reading a $clientIP variable that is SET to = [a zero-length string]" Passing by reference allows code that would otherwise not have direct access to the variable's value, to actually change the value of that variable. Having changed the actual memory space holding $clientIP's value with the new value, PHP then gives ip2long() a copy of a valid value (passing by value) from the variable's memory space for do ip2long() to do its thing. 1 Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500268 Share on other sites More sharing options...
Monkuar Posted December 21, 2014 Author Share Posted December 21, 2014 (edited) "socket_getpeername() will return ... in the address parameter" (Emphasis is mine.) The passing by reference into a parameter is one way to return a value. "how is ip2long reading a $clientIP variable that is SET to = [a zero-length string]" Passing by reference allows code that would otherwise not have direct access to the variable's value, to actually change the value of that variable. Having changed the actual memory space holding $clientIP's value with the new value, PHP then gives ip2long() a copy of a valid value (passing by value) from the variable's memory space for do ip2long() to do its thing. Got it. Thank you very much for the explanation. Didn't actually know you could pass variables through a parameter without the use of using global. I was wondering where the hell it was getting it's value from! Edited December 21, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500273 Share on other sites More sharing options...
Solution kicken Posted December 21, 2014 Solution Share Posted December 21, 2014 Huh? The documentation is as follows: bool socket_getpeername ( resource $socket , string &$address [, int &$port[/color] ] ) See the & sign before the second and third parameters? That means the parameters are passed by reference and the function is able to modify their contents directly. This type of setup is often used if you need to return multiple values along with a success or failure indicator. socket_getpeername's return value indicates whether the function was successful at all or if some error condition occurred. If it's successful, then the two reference parameters contain the information you want. 1 Quote Link to comment https://forums.phpfreaks.com/topic/293204-this-literally-makes-no-sense/#findComment-1500285 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.