Get information from user $ _SERVER via socket

2

I'm using new WebSocket to develop my applications and I need to somehow have access to these connection variables I'm getting through sockets over PHP, is there a way to do this or is it not possible?

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

For the server development use this class as the base:

link

    
asked by anonymous 24.04.2015 / 22:20

1 answer

2

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

  

About my previous answer , ignore it, that part I said that in "CLI does not work", I was totally mistaken:)

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 in terminal or CMD execute server.php :

php5 ./server.php

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

Look at the terminal / cmd screen and note that you have 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.
    
25.04.2015 / 02:57