开发者

PHP Login System with Sessions

开发者 https://www.devze.com 2023-01-04 16:50 出处:网络
Im using sessions for login systems in PHP. In all login examples, people directly uses \"session_start()\" function. But im confused about this.

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';

=========开发者_StackOverflow====================================

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.


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();


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号