0

I'm trying to make a game.

However, I can't get the user ip address on the php side.

I am using plain websocket.

I tried the following codes but it doesn't give what I want. The server gives the ip address.

JAVASCRIPT

websocket = new WebSocket("wss://site.com/game_play/"); 
websocket.binaryType = "arraybuffer";
websocket.onopen = function(event) { 
    
    var send = {
        type: "sign",
        oauth: g_oauth,
        room: g_room,
    };
    websocket.send(binary_encode(send));
}
.....

PHP

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 1, 'usec' => 0));
socket_bind($socket, 0, $port);

socket_listen($socket);
.....

APACHE

ProxyPass /game_play/ ws://site.com:8090/server.php
ProxyPassReverse /game_play/ ws://site.com:8090/server.php

socket_getsockname, socket_getpeername

I tried some functions but unsuccessful. Because the server gives the ip address. I am trying to get user ip address.

12
  • Does $_SERVER['REMOTE_ADDR'] work? Not sure how this will behave with sockets as I haven't used php w/them. Commented Jul 15, 2021 at 17:45
  • 1
    @jshrc $_SERVER['REMOTE_ADDR'] not working in websocket. Commented Jul 15, 2021 at 17:47
  • "I tried some functions but unsuccessful." perhaps the wrong functions? or perhaps because that were wild guesses? Maybe a remote address with Websockets works differently than your understanding what a remote address is? Commented Jul 15, 2021 at 17:48
  • 1
    @jshrc $_SERVER is for http requests. this is a websocket. I'm booting over SSH. Example: php xx.php Commented Jul 15, 2021 at 17:53
  • 1
    @hakre I don't understand why you're being aggressive. I wrote all the codes myself, but the ip address issue is a complete mystery. Commented Jul 15, 2021 at 18:09

1 Answer 1

1

According to the docs you may need to use socket_accept() instead of socket_connect():

Note: socket_getsockname() should not be used with AF_UNIX sockets created with socket_connect(). Only sockets created with socket_accept() or a primary server socket following a call to socket_bind() will return meaningful values.

Edit: wrong function called, should be socket_getpeername()

Either way, the second param in socket_getpeername() is passed by reference and will contain the client IP if the socket is good:

<?php 
....
socket_listen($socket);

$address = '';
socket_getpeername($socket, $address); //$address is passed by reference and will contain the remote IP. 
Sign up to request clarification or add additional context in comments.

15 Comments

$socket_new = socket_accept($this->server); socket_getsockname($socket_new, $address); echo "#$socket_new - $address CONNECTED!\n"; Not client ip. Server ip. socket_connect trying...
My bad, it should be socket_getpeername is for the client. socket_getsockname is for the local side. Edited my answer.
You're not running this locally right? The php is running on a totally different machine? (Sorry, I have to ask as the docs make it pretty clear this would give the remote ip)
I think we succeed. I detected a ip in the headers section. X-Forwarded-For: So how do I just get the ip adress ? ?
Yes, However, the return value of $ip address is still the server ip address. The ip address in the header information is correct, but.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.