php-如何通过fsockopen将会话传递到新套接字?

我需要通过PHP中的fsockopen将会话传递给异步调用.

您能帮我将会话传递给新的套接字吗?

解:

以下代码有效.

start.PHP

<?PHP
session_start();
$_SESSION['var1'] = 'value1';
async_call('/async.PHP');
echo '<pre>';
print_r($_SESSION);
echo $_COOKIE['PHPSESSID'] . "\r\n";
echo '<a href="verify.PHP">verify.PHP</a>';

function async_call($filepath) {
    $host = 'sandBox'; // set to your domain
    $sock = fsockopen($host, 80);
    fwrite($sock, "GET $filepath HTTP/1.1\r\n");
    fwrite($sock, "Host: $host\r\n");
    fwrite($sock, "Cookie: PHPSESSID=" . $_COOKIE['PHPSESSID'] . "\r\n");
    fwrite($sock, "Connection: close\r\n");
    fwrite($sock, "\r\n");
    fflush($sock);
    fclose($sock);
}
?>

async.PHP

<?PHP
session_start();
logger('confirm logger is working');
logger($_SESSION);  // this is always blank!

function logger($msg) {
    $filename = 'debug.log';
    $fd = fopen($filename, "a");
    if (is_array($msg))
        $msg = json_encode($msg);
    $msg = '[' . date('Y/m/d h:i:s', time()) . "]\t" . $msg;
    fwrite($fd, $msg . "\n");
    fclose($fd);
}
?>

verify.PHP

<?PHP
$logfile = 'debug.log';
echo '<a href="start.PHP">start.PHP</a>';
echo '<pre>' . file_get_contents($logfile);
?>

我可以使它在本地运行,但结果是我在没有任何文档的情况下在不允许的fsockopen上暂存了共享主机.在我自己的服务器上一切都很好.谢谢您的帮助.

解决方法:

感谢您的帮助,Andreas.原来我在没有任何文档的情况下在不允许的fsockopen上暂存的共享主机.这就是为什么我可以使其在本地运行,而不在登台服务器上运行的原因.

以下代码有效.

start.PHP

<?PHP
session_start();
$_SESSION['var1'] = 'value1';
async_call('/async.PHP');
echo '<pre>';
print_r($_SESSION);
echo $_COOKIE['PHPSESSID'] . "\r\n";
echo '<a href="verify.PHP">verify.PHP</a>';

function async_call($filepath) {
    $host = 'sandBox'; // set to your domain
    $sock = fsockopen($host, 80);
    fwrite($sock, "GET $filepath HTTP/1.1\r\n");
    fwrite($sock, "Host: $host\r\n");
    fwrite($sock, "Cookie: PHPSESSID=" . $_COOKIE['PHPSESSID'] . "\r\n");
    fwrite($sock, "Connection: close\r\n");
    fwrite($sock, "\r\n");
    fflush($sock);
    fclose($sock);
}
?>

async.PHP

<?PHP
session_start();
logger('confirm logger is working');
logger($_SESSION);  // this is always blank!

function logger($msg) {
    $filename = 'debug.log';
    $fd = fopen($filename, "a");
    if (is_array($msg))
        $msg = json_encode($msg);
    $msg = '[' . date('Y/m/d h:i:s', time()) . "]\t" . $msg;
    fwrite($fd, $msg . "\n");
    fclose($fd);
}
?>

verify.PHP

<?PHP
$logfile = 'debug.log';
echo '<a href="start.PHP">start.PHP</a>';
echo '<pre>' . file_get_contents($logfile);
?>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...