PHP Sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

When you say connection details, what are you talking about?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

BigAbe wrote:1) How do I handle timeouts and/or specifying how long I want the user to stay "logged in"?
You can set the session timeout in the php.ini file. You can also do something programatic -- for example every time they acccess a page you check to see if the time in a timestamp saved in a session variable has been passed. If it has not then save a timestamp an hour in the future to that var. If you have passed the save timestamp then clear the session vars and redirect to the login page.
BigAbe wrote:2) Do you recommend keeping the connection data in a session variable? I have one main index page with tons of include() statements, so I can easily just keep the connection details there, but if there's an easier/better way of doing it, I'd love to hear your thoughts.
No. Keep as little as possible and only temporary data necessary to maintain session context for the user.
(#10850)
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

Everah wrote:When you say connection details, what are you talking about?
Database connextion details

Thanks for the tips you two. You've both been a great deal of help!

-- Abe --
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Database connection details should be stored in an include file a folder below your server root. For example, if your server path is /var/www/public_html/ the you should put your connection details in /var/www/. This is just my opinion. I am sure there are many different, viable approaches to this question.

As for session data... the whole concept of a session is the time a user spends visiting your site. Their 'session' if you will, is the time they are at a particular position. Storing information outside the scope of the session is not a recommended use of session handling. Again, this is my opinion.
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Bonus question

Post by BigAbe »

If on page 1 I declare

Code: Select all

$_SESSION['var'] = 1;
then go to page 2 and say

Code: Select all

$var = 2;
if I go to page 3, what should

Code: Select all

echo $_SESSION['var'];
display?

Just wondering...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

1.
Post Reply