如果用户未登录,则重定向到登录页面

use*_*779 7 php redirect

我创建了自己的网站,但我想要用户在访问某些页面之前登录.例如,我的主页是index.php,有url(www.sample.com/index.php).此页面中有一个登录按钮将重定向到login.php.在login.php页面中,我有一个编辑按钮,onclick事件需要edit.php.

现在,如果我将URL www.sample.com/edit.php粘贴到地址栏中,它会直接转到edit.php,但如果用户未登录,我希望它能够转到index.php,类似于Google要求认证.

Ali*_*lah 11

登录成功后设置会话,并在edit.php中检查它,如果未设置会话,则将其重定向到主页.

login.php:

//put this at the first line
session_start();
//if  authentication successful 
$_SESSION['login'] = true;
Run Code Online (Sandbox Code Playgroud)

edit.php:

if(!$_SESSION['login']){
   header("location:index.php");
   die;
}
Run Code Online (Sandbox Code Playgroud)