I'm not quite sure what the purpose of session_names is.. Can someone please explain in what circumstances defining a name would be beneficial?
Asked
Active
Viewed 1.7k times
31
-
22i know you all mean well by recommending the software documentation, and in general encourage the skill to figure out answers on your own; its just that sometimes.. people need a more practical/pragmatic explanation. Of course I had looked up on php.net the definition of session_name.. and while that page showed me how to use session_name, it didn't specify why and in what cases!. Such clarifications often need additional help from our fellow peers. Thanks to icktoofay for the explanation below. A simple one minute answer.. no links necessary :-) – guacamoly Nov 30 '11 at 01:12
-
2moreover in this case, where it is very evident that @definitelyundefinable did not read said reference at all, or he would have understood that the reference is in no way clear that you can actually manage two sessions by assigning them two names... – Félix Adriyel Gagnon-Grenier Jul 30 '14 at 16:15
-
And the irony @guacamoly is that SO doesn't like you to post these types of questions either, but I think they are way more valuable, for the reasons you stated. – garek007 Dec 22 '22 at 22:15
2 Answers
47
You have two sites on the same domain. (say, a blog and a forum)
They both run different pieces of software.
If they ran on the same session and used the same variables in $_SESSION
, (say, user_id
), they would conflict.
session_name
lets you give each application a different session.

icktoofay
- 126,289
- 21
- 250
- 231
-
Why use two session we could use user_id_blog and user_id_forum right? I am newbie to programming and just asking. Will using session_name be more efficient in anyway? if yes could you care to explain how? – Prajwol Onta May 03 '13 at 06:04
-
5@Geniusintrouble: If you've written both pieces of software, that may be an option; however, if they're huge codebases that you've never touched before, it may be easier to get it to use `session_name` than to track down wherever it touches `$_SESSION`. – icktoofay May 04 '13 at 03:56
-
@icktoofay: And what about `session_id`. I have a shared server hosting, I tried to do the same, but do not face such a problem. However, days back I had used `session_id()` to set the `session_id` manually, then I faced a problem of Internal Server Error, when more than one client tried to connect to the same website. – Veer Shrivastav Apr 16 '14 at 09:15
-
2@Veer: `session_id` might be used if PHP's default handling of the session ID is insufficient for some reason. I've never had to use it. – icktoofay Apr 17 '14 at 06:08
-
5
The default is - I think - PHPSESSID. If you have more than one application on the same host, they would share those sessions. So, you should set different session names for each application, so that there is no weird stuff happening.

klaustopher
- 6,702
- 1
- 22
- 25
-
1I am having problems with sessions on my site and some users losing their session. Half our site is WordPress which sets a PHPSESSID, and my booking process is self written and also sets a PHPSESSID. Do I simply use session_name("mysite") at the top of my buying process pages to avoid conflict? – James Wilson Oct 17 '14 at 11:31
-
It's important to point out that not the same _host_ is what may cause a conflict, but the same (most likely virtual) _domain_ on that host. Different domains on the same host are fine with the same session name. – Sz. May 21 '20 at 02:08