PHP login with sessions

I use sessions for PHP login systems. In all login examples, people directly use the " session_start() " function. But I was embarrassed about it.

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

On localhost, I have these files;

http: //localhost/app1/page1.php

http: //localhost/app1/page2.php

http: //localhost/app2/page2.php

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

In app1/page1.php I start a 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 a session and get the value of this variable.

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

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

I open http: //localhost/app1/page1.php and then http: //localhost/app1/page2.php and it works fine. But after that I open http: //localhost/app2/page2.php and also shows "this is my name" on the screen, and this is wrong. Because it is a different application, and I do not want application2 to reach app1 session.

How can I solve this problem? I do not want to use different variable names for each application. I think there must be another good solution. I can restore the ID on app2 / page1.php, perhaps, but if a person tries to open app2 / page2.php, after opening app1 they can get into application2, and this will not be good for me.

Thanks.

+4
source share
2 answers

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

app1 / page1.php and page2.php

 session_name('app1_session'); session_start(); 

app2 / page1.php and page2.php

 session_name('app2_session'); session_start(); 
+8
source

You will need to use the site_name , which must be started before session_start.

session_name () returns the name of the current session.

Example:

 session_name("name"); session_start(); 
+5
source

Source: https://habr.com/ru/post/1313651/


All Articles