ghedipunk / PHP-Websockets

A Websockets server written in PHP.
BSD 3-Clause "New" or "Revised" License
913 stars 375 forks source link

Add feature to get remote client address #123

Closed ayuyaj closed 4 years ago

ayuyaj commented 4 years ago

It would be good to have a way to tell the remote address of the connected client. I have been running the script on CLI and I could not find a way to get remote address; $_SERVER['REMOTE_ADDR'] does not work

bragle commented 4 years ago

You should be able to find the IP address in the client's headers. I believe X-Forwarded-For is what you're looking for. I'm using cloudflare, and I get the IP in cf-connecting-ip.

ghedipunk commented 4 years ago

https://www.php.net/manual/en/function.socket-getpeername.php

protected function process ($user, $message) {
    $userIP = socket_getpeername($user->socket);
}

$_SERVER['REMOTE_ADDR'] wouldn't work, because the automatic global variables are all filled at script start... Since the script is started from the command line, there is no concept of a web server, so web-specific concepts don't make sense in that context. (I.e., no sessions, either, although you do have access to a user's session ID, so you can open their session storage yourself -- just make sure to close it immediately when you're done, or the user won't be able to browse your site, since the default session system locks the file when it's accessed.)

I've been looking to completely redo this project for quite some time. Perhaps having the server "make requests" (including populating superglobals like $_SERVER) of the code, just as the PHP Web SAPI does, would help other web developers to pick up on the concepts faster; though of course with an easy way to get down to the raw traffic and the main event loop.

On Mon, Oct 7, 2019 at 5:46 AM Leo Toneff notifications@github.com wrote:

You should be able to find the IP address in the client's headers. I believe X-Forwarded-For is what you're looking for. I'm using cloudflare, and I get the IP in cf-connecting-ip.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ghedipunk/PHP-Websockets/issues/123?email_source=notifications&email_token=AAB46PXMZI7ZKJC3DMCTD6DQNMVQTA5CNFSM4I6DYPXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAQGBDQ#issuecomment-538992782, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB46PXXQF43BPCG55CWJH3QNMVQTANCNFSM4I6DYPXA .

ayuyaj commented 4 years ago

https://www.php.net/manual/en/function.socket-getpeername.php protected function process ($user, $message) { $userIP = socket_getpeername($user->socket); } $_SERVER['REMOTE_ADDR'] wouldn't work, because the automatic global variables are all filled at script start... Since the script is started from the command line, there is no concept of a web server, so web-specific concepts don't make sense in that context. (I.e., no sessions, either, although you do have access to a user's session ID, so you can open their session storage yourself -- just make sure to close it immediately when you're done, or the user won't be able to browse your site, since the default session system locks the file when it's accessed.) I've been looking to completely redo this project for quite some time. Perhaps having the server "make requests" (including populating superglobals like $_SERVER) of the code, just as the PHP Web SAPI does, would help other web developers to pick up on the concepts faster; though of course with an easy way to get down to the raw traffic and the main event loop. On Mon, Oct 7, 2019 at 5:46 AM Leo Toneff @.***> wrote: You should be able to find the IP address in the client's headers. I believe X-Forwarded-For is what you're looking for. I'm using cloudflare, and I get the IP in cf-connecting-ip. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#123?email_source=notifications&email_token=AAB46PXMZI7ZKJC3DMCTD6DQNMVQTA5CNFSM4I6DYPXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAQGBDQ#issuecomment-538992782>, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB46PXXQF43BPCG55CWJH3QNMVQTANCNFSM4I6DYPXA .

This function works well. Thanks for the help.