Get $_SERVER user information via socket

Asked

Viewed 184 times

2

Look, I’m wearing it new WebSocket to develop my applications and need somehow to have access to these variables of the connection I’m receiving through the sockets over PHP, is there way to it or is not possible?

$_SERVER['HTTP_ACCEPT'] 
$_SERVER['HTTP_USER_AGENT']
$_SERVER['HTTP_ACCEPT_ENCODING'] 
$_SERVER['HTTP_ACCEPT_LANGUAGE']

for server development use as the basis of this class:

https://github.com/Flynsarmy/PHPWebSocket-Chat/blob/master/class.PHPWebSocket.php

  • 1

    There’s nothing to be sorry about, we’re all here to learn :) ... Please delete the old comments. + 1 for question

  • @Guillhermenascimento in the waiting :]

1 answer

2


To bring the IP and data you will need to change the function socket_recv by function socket_recvfrom

About my previous answer, ignore her, that part I said on the "CLI doesn’t work", I was totally wrong :)

To get the data you need to edit the file class.PHPWebSocket.php on lines 136 and 143:

...
foreach ($changed as $clientID => $socket) {
    if ($clientID != 0) {
        // client socket changed
        $buffer = '';
        $bytes = @socket_recvfrom($socket, $buffer, 4096, 0, $ipaddress, $port);/*$bytes = @socket_recv($socket, $buffer, 4096, 0);*/

        if ($bytes === false) {
            // error on recv, remove client socket (will check to send close frame)
            $this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR);
        }
        elseif ($bytes > 0) {
            echo $buffer, PHP_EOL;
            echo 'IP: ', $ipaddress, ' e porta ', $port, PHP_EOL;
            echo '-------------------------', PHP_EOL;

            // process handshake or frame(s) 
            if (!$this->wsProcessClient($clientID, $buffer, $bytes)) {
...

Then on the terminal or CMD run the server.php:

php5 ./server.php

Then open the file index.html (by the file protocol also works) on two different browsers, Firefox and Opera for example and send a message from each of the browsers.

Look at the terminal/cmd screen and you will notice that you received two buffers with this:

GET / HTTP/1.1
Host: 127.0.0.1:9300
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: null
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/41.0.2272.118 Safari/537.36 OPR/28.0.1750.51
Accept-Encoding: gzip, deflate, lzma, sdch
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: MU8AuFEjzsMrwFh/R2gmZA==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

IP: 0.0.0.0 e porta 0
-------------------------
2015-04-25 04:08:11: 127.0.0.1 (1) has connected.
  • I think I get it.. But if I can’t access the user’s information, it’s not helpful.. I need an alternative solution, I think.. as "gambiarras"

  • I got a little lost in the explanation I think I centered the whole answer in its last paragraph, the apache method will work?

  • apparently it worked here.. just a doubt, have you can get the data of this variable from the user who connects? Because testing on different browsers I noticed that it always results the same values (that is the pc that started the server)

  • When the user connects I send this message to him md5($_SERVER['HTTP_ACCEPT'] . $_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['REMOTE_ADDR']) as said it returns equal in all connections, Idel was that these data were from the connecting user.. Obg =]

  • if you give me a reference or something like I can start searching..

  • @user3163662 this http://php.net/manual/en/function.socket-connect.php :(

  • what I am looking for but there is nothing that says that it is possible to pass user information beyond IP D:

  • @user3163662 I found out by reading the link I sent you, I was totally wrong, I edited the answer, I hope I can redeem myself like this :) See you more.

  • Voce is a genius hehehe, thank you very much <33

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.