在websocket上识别/验证客户端

I am creating a simple socket server in php. But I have some doubts since it is the first time I do something like this. My question is how do I identify the client, this is perhaps the authentication system.

What I've tried so far:

  • Cookies
  • Hash

Server Code: https://www.codepile.net/pile/vBZP9xkq

Cliente Code: https://www.codepile.net/pile/YlD2JDjB

Cookies

When I started to create the server I thought that no authentication system was needed, since I have the site with login, in principle the socket was going to share the same cookie and that way, on the socket server, I would have access to the client session.

And so he knew that socket X was client X.

But to my surprise in the headers comes a different phpsessid.

PHPSESSID=7fo97nhaqfgir55ar17cr8c364 // php and js (site)
PHPSESSID=isjbjik06p0d8srm4j1ccptj2b // socket

How different they do not work.

Hash

It was my second approach, adding parameters to the socket address:

  • mysocket.com:9000/?h=123456789

But I quickly realized that on the socket server I could not catch anything, the headers and variable $_SERVER and $_REQUEST do not take the parameters.

Later I understood that it does not pass since it is not an http request, so it does not carry this information.

Conclusion

At this moment I am a little lost, I do not know how to identify the client that enters the socket, being that the only information I have is the ip and this is impractical since in the same ip can have X clients.

Can someone explain to me the correct way to authenticate to a socket?