Open Side Menu Go to the Top
Register
PHP SESSIONS what is most likely the problem here? PHP SESSIONS what is most likely the problem here?

03-26-2013 , 09:09 PM
I'm making a home page in PHP and MySQL with a login function.

the user types in username and password and a $_SESSION variable is created, and now comes the problem. I can only call the session-variable once and then it dies, when I change page the session-variable is empty, it only lasts for a couple of seconds which to me is really weird.

I'm a noob at programming. any help is appreciated
PHP SESSIONS what is most likely the problem here? Quote
03-26-2013 , 10:10 PM
Are you by any chance overwriting it somewhere? So the first page appears but later int he script you overwrite or unset $_SESSION so the next page you visit no longer has the variable?
PHP SESSIONS what is most likely the problem here? Quote
03-26-2013 , 10:40 PM
You might need to call session_start() for each page

Quote:
but later int he script you overwrite or unset $_SESSION so the next page you visit no longer has the variable?
changing the reference of $_SESSION probably has weird effects since it's super global, but in any case variables aren't persistent across multiple page views so even if you did it the next request wouldn't know anything about it.
PHP SESSIONS what is most likely the problem here? Quote
03-26-2013 , 10:57 PM
it would if you did something like the following:
if ($_SESSION['blah'] == 'logged in') {// do stuff }
else { //show error that we are not logged in }

//do page stuff

$_SESSION['blah'] = null;


Now page shows all nicely, but next time we go to page suddenly the session variable we expect isn't there
PHP SESSIONS what is most likely the problem here? Quote
03-26-2013 , 11:20 PM
I thought you meant

$_SESSION = ...

nevermind
PHP SESSIONS what is most likely the problem here? Quote
03-27-2013 , 03:29 AM
Quote:
Originally Posted by well named
You might need to call session_start() for each page



changing the reference of $_SESSION probably has weird effects since it's super global, but in any case variables aren't persistent across multiple page views so even if you did it the next request wouldn't know anything about it.
that might be the case, I'll try that out. I don't call session_start on every page just the login page
PHP SESSIONS what is most likely the problem here? Quote
03-28-2013 , 10:16 AM
Quote:
Originally Posted by OMGtuttlisa
that might be the case, I'll try that out. I don't call session_start on every page just the login page
You need session_start(); on every page very first thing. Put it in a file and just include it at the top of every page. You can also include your function and config file includes in there to save including them separately on every page.
PHP SESSIONS what is most likely the problem here? Quote
04-24-2013 , 11:51 AM
lol i worked out when I added
[PHP]session_start();[/PHP]
to the connect.php -file

as all of you saw immediately lol, now you must think I'm a total noOb! thanks anyway guys!
PHP SESSIONS what is most likely the problem here? Quote
05-07-2013 , 01:32 AM
works fine thank you guys. added it to the connect.php -file
PHP SESSIONS what is most likely the problem here? Quote

      
m