Log in

View Full Version : lost in PHP session



sfchun
11-18-2008, 06:20 AM
hello i have a little trouble with php session ...
i have read some php tutorials , but nothing is clear for me ...

- i've created a web page with dynamic content.
- i'm not using cookies
- A php session is created with session_star() in each new page inserted.

the problem , when the user login in all is fine , but when a page refresh is performed (using code or pressing F5), user is disconnected and so lost session and have to login again.

the question is do i need to use cookies to keep the session alive after refresh ?? is there a way to keep the session (without cookies) until the browser is closed ?

sfchun
11-18-2008, 06:03 PM
i thought it would be a common problem, but i see nothing on google , and no reply here ... :(:confused:

sfchun
11-19-2008, 06:54 AM
If no one have ever experienced this problem , can you just show up how you do create a session (which stay alive after refreshing) ??

rodneykm
11-19-2008, 09:08 PM
How long is your session timeout? You can check using the following bit of code mate.


// Get the current Session Timeout Value
$currentTimeoutInSecs = ini_get(’session.gc_maxlifetime’);

sfchun
11-20-2008, 01:26 PM
timeout : 1440
i think it's default settings

but i don't think it comes from this , cause if i log on and perform a refresh just after, i get disconnected ... -_-;

olveyphotodesign
11-20-2008, 07:59 PM
Good Day,

Will you post the code that is giving you trouble. It would help us try to help you.

sfchun
11-21-2008, 12:12 AM
realy there is nothing much ...
nothing more basic than this :



<?php
session_start();
$_SESSION['CurrentUser']=$User
$_SESSION['CurrentUPassword']=$uPasswd
...
session_write_close();
?>


then when i click on disconnect the code is this one :


<?php
session_start();
session_unset();
session_destroy();
echo '<br />Byebye';
?>

JasonDFR
11-21-2008, 02:19 PM
Hi.

As you said, when you want to use SESSION variables put

session_start();

at the top of your page.

It looks like you know how to assign SESSION variables too.

Your session variables will remain until you "logout" by destroying "session_destory();" your session.

Where are your variables $User and $uPasswd coming from?

The code you posted is not all on the same page is it?

I have never used session_write_close() before. I'll have to look it up.

You do not need to set cookies to make session variables work.

Sessions will remain in place after a refresh.

Post all of the code you are using and we'll be able to figure it out. Sessions are pretty straight forward once you get the hang of it, so I think you must be making a simple mistake somewhere.

Good Luck,

Jason

olveyphotodesign
11-22-2008, 01:20 AM
Good Evening,

I think your problem lies in the session_write_close. Php.net states that this closes the current session. Therefore when you reload the page, the session has already been closed. Try commenting that function out and see if that fixes your problem.

Good Luck,
Ben

sfchun
11-22-2008, 06:20 AM
Your session variables will remain until you "logout" by destroying "session_destory();" your session.

Where are your variables $User and $uPasswd coming from?
Variables comes from a form posted.
I have no problem with them as i use them for all the time that the session is still alive.


I think your problem lies in the session_write_close

I've also though about this, but i can comment the line and nothing change.

JasonDFR
11-22-2008, 07:56 AM
Get rid of session_write_close();

What exactly is happening to make you think your session is lost?

Are you trying to use your session variables after a refresh and they don't work?

Try this:

After you set your session for username, put


echo $_SESSION['CurrentUser'];

somewhere on the page. This will display the username on the page. Hit refresh and see if it is still displayed.

If it is still displayed, then you'll know that your problem is somewhere else.

Post more of your code too. You might have a problem, but it isn't in the code you have posted.

Good Luck!

sfchun
11-22-2008, 09:18 AM
I think you put the finger where it hurts !

my problem is that the html code in index.php is not generated by php !
I did not think about this but as i'm not testing if $_SESSION variables are available at the page load (as it is html code) , it replace the display and shows again the login screen ... but the session is still running !

thanks a lot !
So now i know i should generate/call all my html code using php.

JasonDFR
11-22-2008, 09:26 AM
I'm glad you found the problem, but you don't have to use php to generate all your code! And I don't think you should.

You talked about a login screen being displayed when the user is logged in?

I'm assuming it shouldn't be displayed when the user is logged in?

Check out this thread:

http://www.dynamicdrive.com/forums/showthread.php?t=37942

In the third post I posted some code. See how the <?php tags open and then close ?> You can use this technique throughout your page, only putting php where you need it.

Take a minute to understand what is going on in that code and I think it will help you a lot!

Good luck!

Jason

sfchun
11-22-2008, 01:24 PM
Yes i understand well ^^

i said generated, cause i have part of my page which should be hide or display following who is loging in ...
Only this part should be done through php =P