upscalesoftware / swoole-session Goto Github PK
View Code? Open in Web Editor NEWPHP sessions compatibility with Swoole web-server
License: Apache License 2.0
PHP sessions compatibility with Swoole web-server
License: Apache License 2.0
Hi,
Should swoole-session be working with Swoole 4.5.2, PHP 7.4.3?
This is what I'm getting:
PHP Warning: session_id(): Cannot change session id when headers already sent in /var/www/html/mvc_swoole/vendor/upscale/swoole-session/src/SessionMiddleware.php on line 62
PHP Warning: session_start(): Cannot start session when headers already sent in /var/www/html/mvc_swoole/vendor/upscale/swoole-session/src/SessionMiddleware.php on line 63
PHP Warning: session_id(): Cannot change session id when headers already sent in /var/www/html/mvc_swoole/vendor/upscale/swoole-session/src/SessionMiddleware.php on line 80
Regards.
When starting a website conversion with swoole, I had to use this middleware to be able to use the sessions properly.
Like many people using twig for rendering, I ran into the typical header already sent problem when working with sessions.
While trying to understand the origin with the #1 issue and searching on the internet, I could observe that nobody else had looked at the issue.
So, through this issue I would like to bring for future people, how I could solve my problem and why. Any information is good to take from other people.
I can already say that I have had no problems with coroutine so far.
So to start with, I made a simple application like everyone else, with these few lines:
$server = new Swoole\HTTP\Server("localhost", 9501);
$server->on("WorkerStart", function($server, $workerId) {
echo "Worker Start: $workerId\n";
});
$server->on("Start", function() {
echo "Swoole HTTP Server Started @ 127.0.0.1:9501\n";
});
$server->on('Request', new SessionMiddleware(function (Swoole\Http\Request $request,Swoole\Http\Response $response) use ($app) {
$response->end("Hello world !");
}));
$server->on("Shutdown", function($server, $workerId) {
echo "Server shutting down...\n";
});
// Triggered when worker processes are being stopped
$server->on("WorkerStop", function($server, $workerId) {
echo "Worker Stopped: $workerId\n";
});
$server->start();
A basic web server with swoole using SessionMiddleware.
However, warnings will appear every time you want to access your site. Normal, we'll see about that.
I thought it was a problem on the on('Request')
side, but not at all because we finish the request directly, so there is no need to print elsewhere.
After some research, I noticed that by removing the echo from WorkerStart
, everything worked fine. And this is quite logical, in fact, since they are the ones who will carry out the different requests, if we do an echo, the requests that we will process afterwards will be bugged as we do not want.
In conclusion, when rendering with any library, as a precaution use the output buffering to prevent any risk.
As for logging, rather than echoing or printing, use a lib to log like monolog
under file to avoid any risk of writing to the buffer.
And if we follow these precautions, I do not see why coroutine would not work, if nothing is written in the buffer.
However, as soon as a PHP warning is thrown by the code and this one is displayed, the system will bug. It is therefore necessary to prevent any risk by removing warnings?
Dear friends.
Session management with files was successful in the swoole environment,
but session information cannot be generated on the redis server when managing sessions with redis.
<< test result >>
nginx + php-fpm = redis session test success
nginx + php-fpm = file session test success
swoole + SessionMiddleware = file session test succes (session.save_handler=file)
swoole + SessionMiddleware = redis session test fail (session.save_handler=redis)
<< php.ini - fail config >>
session.save_handler=redis
session.save_path="tcp://localhost:6379?auth=1234"
php : 7.4.15
swoole : 4.6.5
Could this be compatible with openswoole? atm composer complains about the missing extension when using openswoole.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.