How do I pass session to new socket via fsockopen?

*爱你&永不变心* 提交于 2020-01-11 07:49:08

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/7300871/how-do-i-pass-session-to-new-socket-via-fsockopen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!