我正在尝试从HTTP请求字符串中获取cookie值。如何处理请求头字符串并获取其中的cookie值?是否有任何方法或任何不同的解决方案来处理它?
server.php
<?php
$host = "127.0.0.1";
$port = 20205;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket");
$result = socket_bind($sock, $host, $port) or die("Could not bind to socket");
$result = socket_listen($sock, 3) or die("Could not set up socket listener");
echo "Connection Established\n";
$agents = array();
do {
$accept = socket_accept($sock) or die("Could not accept incoming connection");
$header = socket_read($accept, 1024) or die("Could not read input");
var_dump($header);
perform_handshaking($header, $accept, $host, $port);
while (socket_recv($accept, $data, 1024, 0) >= 1) {
$data = unmask($data);
$data = json_decode($data);
$action = $data->action;
if (isset($data->action) && $action == 'connect') {
foreach ($agents as $key => $value) {
if ($key == $agentid && $value != $accept) {
socket_close($value);
$agents[$key] = $accept;
}
}
}
break; //exist this loop
}
} while (true);这就是我从HTTP请求中获取头字符串的方式。
发布于 2022-02-05 09:46:53
这是我如何解析头字符串的方法。
function perform_handshaking($receved_header, $client_conn, $host, $port)
{
$headers = array();
$lines = preg_split("/\r\n/", $receved_header);
foreach ($lines as $line) {
$line = chop($line);
if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
$headers[$matches[1]] = $matches[2];
}
}
$secKey = $headers['Sec-WebSocket-Key'];
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
//hand shaking header
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"WebSocket-Origin: $host\r\n" .
"WebSocket-Location: ws://$host:$port/demo/shout.php\r\n" .
"Sec-WebSocket-Accept:$secAccept\r\n\r\n";
socket_write($client_conn, $upgrade, strlen($upgrade));
}https://stackoverflow.com/questions/70996650
复制相似问题