Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted session start from \Template::instance()->render #320

Open
radioboss opened this issue Mar 27, 2018 · 3 comments
Open

Unwanted session start from \Template::instance()->render #320

radioboss opened this issue Mar 27, 2018 · 3 comments
Labels

Comments

@radioboss
Copy link

When custom session handler is used (in my case it's \DB\SQL\Session), \Template::instance()->render call from a page where custom session handler is not initialized destroys the session cookie.

This appears to happen because of this code in base.php for the Preview->render method :
if (isset($_COOKIE[session_name()]) &&
!headers_sent() && session_status()!=PHP_SESSION_ACTIVE)
session_start();

Currently I work around this issue by calling unset($_COOKIE[session_name()]); before rendering, but I'd like to see an option to prevent render method starting the session in the first place.

@ikkez
Copy link
Member

ikkez commented Sep 14, 2018

The problem is, you cannot access SESSION variables within the template, if the session was not started before. I can understand that starting a needless session is also less optimal, but there's currently no way to automatically know if there's access to a session var within a template or its filters and extentions. So to keep things working as they are right now, starting the session seems mandatory.
Maybe we can adjust this behaviour with a setting and to let the developer decide when to start the session in a newer major version (v4).

@radioboss
Copy link
Author

Yes, a parameter should do. Looking forward to v4, meanwhile I'll use the workaround.

@Jiab77
Copy link

Jiab77 commented Jan 23, 2019

I'm using this code in my actual project to detect if the session array is initialized and not empty:

// Check if session is correctly initialized
// Return boolean
function session_active() {
	if (isset($_SESSION) && (is_array($_SESSION) && count($_SESSION)>0)) {
		return true;
	}
	else {
		return false;
	}
}

then just use it that way: if (session_active() === true) { #code }. it's pretty simple and can be minimized I guess.

@ikkez ikkez transferred this issue from bcosca/fatfree Dec 13, 2020
@ikkez ikkez added the v4 label Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants