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

发布于 2024-12-03 01:28:02 字数 1504 浏览 1 评论 0原文

我需要通过 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 上暂存的共享主机没有任何文档。在我自己的服务器上一切都很好。感谢您的帮助。

I need to pass the session to an asynchronous call via fsockopen in php.

Can you help me pass the session to the new socket?

SOLUTION:

The following code works.

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);
?>

I could get this working locally, but it turned out the shared host I was staging on disallowed fsockopen without any documentation. All good on my own servers. Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2024-12-10 01:28:03

感谢您的帮助,安德烈亚斯。事实证明,我在没有任何文档的情况下在不允许的 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);
?>

Thanks for the help, Andreas. Turns out the shared host I was staging on disallowed fsockopen without any documentation. That's why I could get it working locally but not on the staging server.

The following code works.

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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文