0

I have am using websocket and trying to merge with my custom PHP app and Mysql database. In Database I store IP, ClientID and username when user connects to server. These infromation are to be used when user disconnect from websocket server or sends message. I run server.php with php server.php

The server page is https://github.com/Flynsarmy/PHPWebSocket-Chat/blob/master/server.php

As mentioned above I stored necessary information in Mysql database to identify which users sends message, disconnect and connect.

So for that I need to identify my own mechine IP for further identification with various clients. So when I try to use

<?php 
    echo $_SERVER["REMOTE_ADDR"];

in server.php. It gives an error saying undefined.

tashi
  • 249
  • 1
  • 3
  • 16
  • Duplicate of: http://stackoverflow.com/questions/4594823/problem-with-serverremote-addr – Nytrix Jul 28 '15 at 11:43
  • Didn't help. Would you help me solve it? – tashi Jul 28 '15 at 11:48
  • possible duplicate of [What is the most accurate way to retrieve a user's correct IP address in PHP?](http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php) – bufh Jul 28 '15 at 12:21
  • Not a duplicate of either of those, because WebSockets is not HTTP. – Ghedipunk Jul 28 '15 at 17:37
  • Also, that FlynsArmy websocket server is implementing an old, insecure draft version of the websocket standard. For the sake of you and your users, find a server that implements RFC 6455 ( https://tools.ietf.org/html/rfc6455 ), also known as Version 13. – Ghedipunk Jul 28 '15 at 17:45

2 Answers2

1

You don't.

WebSockets does not deal with web requests. The $_SERVER superglobal does not get populated because it does not make sense to populate it.

Deal with the socket connections directly.

You most likely want to play around with socket_getsockname().

Ghedipunk
  • 1,229
  • 10
  • 22
0

Please try this,

var_dump($_SERVER);

and check if it prints... [REMOTE_ADDR] => .......

But if you are after clients IP, as a PHP developer I use following code

    $ip=NULL;
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) 
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else 
        $ip = $_SERVER['REMOTE_ADDR'];
Narayan Bhandari
  • 426
  • 3
  • 11