简体   繁体   English

受限页面 - 仅对登录用户可见 - PHP

[英]Restricted pages - Only visible to logged in users - PHP

I have Login & Register system with the protected page ( home page ).我有受保护页面(主页)的登录和注册系统。 When users create an account and log in with a username and password, the system will redirect the user to the home page.当用户创建帐户并使用用户名和密码登录时,系统会将用户重定向到主页。 On the home page, I have a code for restriction ( only logged in users can be on the home page ):在主页上,我有一个限制代码(只有登录的用户才能在主页上):

session_start();
session_destroy();

include $_SERVER['DOCUMENT_ROOT'] . '/web/route.php';
include $_SERVER['DOCUMENT_ROOT'] . '/app/database/config.php';
include $_SERVER['DOCUMENT_ROOT'] . '/app/functions/navigation.php';

if (!isset($_SESSION['username'])) {
    header('location: ../');
    exit();
}

And if I try to open that page if I am not logged in, the system will automatically redirect me back to the login page.如果我在未登录的情况下尝试打开该页面,系统会自动将我重定向回登录页面。 Now I create the second page ( contact support ) and copy the same code to a new page, I don't get any error's but I only have redirection to the home page and when I try to open a manual new page, the system redirects me again and I am logged in. My logout code is:现在我创建第二个页面(联系支持)并将相同的代码复制到新页面,我没有收到任何错误,但我只有重定向到主页,当我尝试打开手动新页面时,系统重定向我再次登录。我的注销代码是:

session_start();
session_destroy();

if (isset($_COOKIE['authenticationSystem'])) {

    unset($_COOKIE['authenticationSystem']);
    setcookie('authenticationSystem', null, -1, '/');
}

header('location: index');

And if I add new code to a new page:如果我将新代码添加到新页面:

if (isset($_COOKIE['authenticationSystem'])) {
    header('location: ../');
    exit();
}

I resolve the problem but I can't get user logged-in information.我解决了问题,但我无法获取用户登录信息。 Can someone explain to me where is the problem, if any other information needs I will provide it?有人可以向我解释问题出在哪里,如果需要任何其他信息,我会提供吗?

Thanks all谢谢大家

You should be careful with cookies.您应该小心 cookies。 I believe in most cases just working with sessions should be enough if using cookie is not significant.我相信在大多数情况下,如果使用 cookie 并不重要,那么只使用会话就足够了。 By user logged-in information you do mean username as a session?通过用户登录信息,您的意思是用户名是 session? Is it possible that you forget to session_start()?您是否有可能忘记 session_start()? Because you should start sessions in every page.因为您应该在每个页面中开始会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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