Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: what is the purpose of session_name

Tags:

php

session

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?

like image 952
guacamoly Avatar asked Sep 26 '11 06:09

guacamoly


People also ask

What is a named session?

The session name references the name of the session, which is used in cookies and URLs (e.g. PHPSESSID). It should contain only alphanumeric characters; it should be short and descriptive (i.e. for users with enabled cookie warnings). If name is specified, the name of the current session is changed to its value.

What is purpose of Session_destroy () in PHP?

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

Why session_start () is used in PHP?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

How do you set a session name?

session_name() must be called before session_start() for the session to work properly. The session name is reset to the default value stored in session.name at request startup time. Thus, you need to call session_name() for every request (and before session_start() is called).


2 Answers

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.

like image 154
icktoofay Avatar answered Oct 06 '22 05:10

icktoofay


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.

like image 29
klaustopher Avatar answered Oct 06 '22 06:10

klaustopher