0

I want to be able to keep a PHP session, even after the browser has been closed. I know on this question, they suggested using the session_set_cookie_parameters() parameter, but I am unable to find a working example of this that doesn't expire and works across all directory on my domain. If someone could help out, it would be amazing.

3
  • I wanted to avoid using cookies because they are client side, and therefore can be edited or modified by the client; henceforth making it easier for people to gain unauthorised access to an others account. Commented Jan 14, 2013 at 3:54
  • so store the session id in the cookie and recreate when they come back Commented Jan 14, 2013 at 4:04
  • That's what I ended up doing. Thanks. :) Commented Jan 14, 2013 at 6:44

2 Answers 2

1

Session stands for "until browser is closed". Session is something that expires. If you don't want it to be expired, you're probably don't want a session at all. You better read on cookie too. I guess that's what you need.

But You can use session_set_cookie_parameters() to give the session cookie a non-zero lifetime before starting the session, or set session.cookie_lifetime to non-zero.

Remeber session_set_cookie_params() needs to be called before session_start() for every single page request. Read more

Code:

<?php session_set_cookie_params(30 * 60, "/"); session_start(); print_r(session_get_cookie_params()); ?> 

Output:

Array ( [lifetime] => 1800 [path] => / [domain] => [secure] => )

Explanation:

Sets the cookie to expire after 30 minutes and be available anywhere on the site.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, okay. So it is probably just better to use cookies then?
yes it could be, it depends on your requirement. Read the links which will helps you to figure out what you need
1

Use session_set_cookie_parameters() with a non zero value before session starts.

Refer here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.