play_ Posted December 6, 2007 Share Posted December 6, 2007 I usually have session_start() in the header of my sites, causing a session to start even when the visitor enters it for the first time. that's unnecessary though and i'd like to start a session only if there actually is one present. Any way to do this? I thought something like this might've worked, but it doesn't: if(session_id() != "") { session_start(); echo 'There is a session_id, start session'; } else { echo 'no session_id exists. session not started - unnecessary'; } Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/ Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 causing a session to start even when the visitor enters it for the first time A session isn't going to start unless you actually register one. session_start() doesn't "create" any sessions. So calling session_start on your header page isn't a bad thing. If you really want to do that, just do this <?php if (!empty($_SESSION)) session_start(); ?> Now session_start will only be called if the user has a registered session. Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407562 Share on other sites More sharing options...
trq Posted December 6, 2007 Share Posted December 6, 2007 Now session_start will only be called if the user has a registered session. Which means it will never be called, think about it. Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407587 Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 Now session_start will only be called if the user has a registered session. Which means it will never be called, think about it. Ah, your right. Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407615 Share on other sites More sharing options...
teng84 Posted December 6, 2007 Share Posted December 6, 2007 Now session_start will only be called if the user has a registered session. Which means it will never be called, think about it. yah.. lol :D sorry i cant help my self laughing Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407620 Share on other sites More sharing options...
teng84 Posted December 6, 2007 Share Posted December 6, 2007 i think what you mean is set the session on diff situation Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407621 Share on other sites More sharing options...
play_ Posted December 6, 2007 Author Share Posted December 6, 2007 A session isn't going to start unless you actually register one. session_start() doesn't "create" any sessions If i do: <?php session_start(); echo SID; ?> it outputs the session id. therefore it has started, no? Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407624 Share on other sites More sharing options...
trq Posted December 6, 2007 Share Posted December 6, 2007 Yes. But whats the problem? Don't call session_start() unless you need it then. Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407629 Share on other sites More sharing options...
play_ Posted December 6, 2007 Author Share Posted December 6, 2007 Exactly what i want to do. every page is in the following structure: header content/body footer header and footer are includes in every page. currently i have session_start() at the top of the header, which causes a session to start even if a user enters the site for the first time. I want a session to start only if the user logs in. (or if i need to display a session variable here and there, such as the user's first name. but this isnt' related to the question) Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407632 Share on other sites More sharing options...
trq Posted December 6, 2007 Share Posted December 6, 2007 You should be able to put.... <?php if (!empty(session_id()) { session_start(); } ?> In your header. And also call session_start() just prior to loging in (when your first set variables in the $_SESSION array). I just fail to see the point. Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407637 Share on other sites More sharing options...
revraz Posted December 6, 2007 Share Posted December 6, 2007 I don't see what you are gaining by doing this. I want a session to start only if the user logs in. Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407646 Share on other sites More sharing options...
play_ Posted December 6, 2007 Author Share Posted December 6, 2007 Have you ever visited a website for the first time in that day, and when you click on the first link, you see the session id in the url? i'd like that not to happen. and with this, it will not start a session unless it's required. thank you Link to comment https://forums.phpfreaks.com/topic/80386-checking-if-a-session-exists-before-session_start/#findComment-407647 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.