title: Use session data on websocket handshake
body: <p>If a logged in user  navigates to a certain area of the site which is to use WebSockets, How can I grab that session Id so I can identify him on the server?</p>
  
  <p>My server is basically an endless while loop which holds information about all connected users and stuff, so in order to grab that id I figured the only suitable moment is at the handshake, but unfortunately the handshake's request headers contain no cookie data:</p>
  
  <blockquote>
    <p><strong>Request Headers</strong>  </p>
    
    <p>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,<em>/</em>;q=0.8<br>
    Accept-Encoding: gzip, deflate 
    Accept-Language: en-US,en;q=0.5<br>
    Cache-Control: no-cache<br>
    Connection: keep-alive, Upgrade<br>
    DNT: 1<br>
    Host: 192.168.1.2:9300<br>
    Origin: <code>http://localhost</code><br>
    Pragma: no-cache<br>
    Sec-WebSocket-Key: 5C7zarsxeh1kdcAIdjQezg==<br>
    Sec-WebSocket-Version: 13<br>
    Upgrade: websocket<br>
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64;
    rv:27.0) Gecko/20100101 Firefox/27.0</p>
  </blockquote>
  
  <p>So how can I really grab that id? I thought I could somehow force javascript to send cookie data along with that request but any self-respecting website in 2014 will have httpOnly session cookies so that wont work out. Any help is greatly appreciated!</p>
  
  <p>Here's a link for the server I'm using: <a href="https://github.com/Flynsarmy/PHPWebSocket-Chat/blob/master/class.PHPWebSocket.php" rel="nofollow noreferrer">https://github.com/Flynsarmy/PHPWebSocket-Chat/blob/master/class.PHPWebSocket.php</a> (thanks to accepted answer)</p>