Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I pass session to new socket via fsockopen?
    primarykey
    data
    text
    <p>I need to pass the session to an asynchronous call via fsockopen in php.</p> <p>Can you help me pass the session to the new socket?</p> <p><strong>SOLUTION:</strong></p> <p>The following code works.</p> <p><strong>start.php</strong></p> <pre><code>&lt;?php session_start(); $_SESSION['var1'] = 'value1'; async_call('/async.php'); echo '&lt;pre&gt;'; print_r($_SESSION); echo $_COOKIE['PHPSESSID'] . "\r\n"; echo '&lt;a href="verify.php"&gt;verify.php&lt;/a&gt;'; 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); } ?&gt; </code></pre> <p><strong>async.php</strong></p> <pre><code>&lt;?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); } ?&gt; </code></pre> <p><strong>verify.php</strong></p> <pre><code>&lt;?php $logfile = 'debug.log'; echo '&lt;a href="start.php"&gt;start.php&lt;/a&gt;'; echo '&lt;pre&gt;' . file_get_contents($logfile); ?&gt; </code></pre> <hr> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload