tags:

views:

96

answers:

2

Hi. Im using sessions for login systems in PHP. In all login examples, people directly uses "session_start()" function. But im confused about this.

=============================================

On localhost, I have that files;

http://localhost/app1/page1.php

http://localhost/app1/page2.php

http://localhost/app2/page2.php

=============================================

In app1/page1.php, I start the session and set a variable.

 session_start();
 session_regenerate_id( true );
 $_SESSION[ 'name' ]    = 'this is my name';

=============================================

In app1/page2.php and app2/page2.php, I start the session and get the value of that variable.

 session_start();
 echo 'name: ' . $_SESSION[ 'name' ];

=============================================

I open http://localhost/app1/page1.php and then http://localhost/app1/page2.php and it works great. But after that, I open http://localhost/app2/page2.php, and also it show "this is my name" writing on the screen and this is wrong. Because it's another application and I dont want app2 to reach app1's session.

How can I solve this problem? I don't want to use different variable names for each application. There must be another good solution I think. I can regenerate ID at app2/page1.php maybe, but if a person tries to open app2/page2.php, after opened app1, they may get into the app2 and this doesn't become good for me.

Thank you.

+4  A: 

You will need to make use of session_name, which will have to be run before session_start.

session_name() returns the name of the current session.

Example:

session_name("name");
session_start();
Russell Dias
+8  A: 

You need to use session_name() before session_start(); Example:

app1/page1.php & page2.php

session_name('app1_session');
session_start();

app2/page1.php & page2.php

session_name('app2_session');
session_start();
Bogdan
+1, beat me to it.
Eric Petroelje
Well, I think Russell Dias beat me to it. +1 to him too :)
Bogdan
Alternately, if the apps ever need to share some data, just namespace the app-specific data within `$_SESSION` (ie. `$_SESSION['app1']['x']` and `$_SESSION['app2']['x']`.)
Adam Backstrom