简体   繁体   中英

Destroy session when going out from specific folder

Basically, I am after a function which would destroy a php session when the user goes from one folder to another one on the same domain like this: mywebsite.com/folder1/index.php

This folder has a specific list of users who are able to start a session, but I don't want them to be able to carry this session to mywebsite.com/folder2/ (which has it's own list of users).

I thought to check if isset($_SESSION['login']) and compare it to the list of users every time I start a page. However, I can't imagine there is another way to do this because down the line it's not that good to have an sql req at the beginning of each page.

When setting the cookie, restrict its path :

session_set_cookie_params(0, '/folder1');

http://php.net/session_set_cookie_params

If you additionally use unique session names per folder, you'll have even better assurance that the session is restricted to a certain path.

You can use additional level array:

$_SESSION['folder1']['login']
$_SESSION['folder2']['login']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM