[英]How to fix failed redirects after login in PHP
I added login to my site, everything works except one thing: if a user who is not logged in is not redirected to login.php, I tried several things please help me, Thanks. 我将登录名添加到我的站点,除以下几点外,其他所有工作都有效:如果未登录的用户未重定向到login.php,我尝试了几项操作,请帮帮我,谢谢。
process.php (login process): process.php(登录过程):
if ($row['username'] == $username && $row['password'] == $password && ("" !== $username || "" !== $password)){
$_SESSION["users"] = $row['username'];
$_SESSION['login'] = true;
header("Location: https://**********/inde.php");
} else {
header("Location: error.php");
}
logout.php: logout.php:
session_start();
$_SESSION['users'] = NULL;
$_SESSION['login'] = false;
header("location: https://**********/login.php");
exit();
On all pages of the website I added: 在网站的所有页面上,我添加了:
include("content/login_verif.php");
login_verif.php: login_verif.php:
session_start();
if $_SESSION['login'] != true;
{
header('Location: https://**********/login.php');
exit();
}
Simply put a safeguard in your home page. 只需在您的主页上放置一个保护措施即可。 Check whether both
$_SESSION['users']
and $_SESSION['login']
is set or not. 检查
$_SESSION['users']
和$_SESSION['login']
是否$_SESSION['login']
设置。 If either of them is not set, then redirect the user to login page. 如果未设置任何一个,则将用户重定向到登录页面。
login_verif.php login_verif.php
session_start();
if(!isset($_SESSION['users']) || !isset($_SESSION['login'])){
// redirect the user to login page
header('Location: https://ferapps.altervista.org/tia/content/login/login.php');
exit();
}
and include this login_verif.php page in the following way, 并通过以下方式包含该login_verif.php页面,
require_once("content/login_verif.php");
And that's not how you should logout a user. 那不是您应该注销用户的方式。 You need to properly clear the cookies and destroy the sessions, so your logout.php page should be like this:
您需要正确清除cookie并销毁会话,因此您的logout.php页面应如下所示:
logout.php logout.php
<?php
session_start();
if(!isset($_SESSION['users']) || !isset($_SESSION['login'])){
// redirect the user to login page
header('Location: https://ferapps.altervista.org/tia/content/login/login.php');
exit();
}
$_SESSION = array();
if(isset($_COOKIE[session_name()])){
setcookie(session_name(),'',time()-42000,'/');
}
session_destroy();
// redirect the user to login page
header('Location: https://ferapps.altervista.org/tia/content/login/login.php');
exit();
?>
file_get_contents()
is used to output the contents of a file as a string. 用于将文件内容输出为字符串。
You want 你要
include("content/login_verif.php");
instead. 代替。 And
和
if $_SESSION['login'] != true;
should be 应该
if ($_SESSION['login'] != true)
The issue here is the incorrect use of "file_get_contents". 这里的问题是“ file_get_contents”的错误使用。
file_get_contents as explained at http://php.net/manual/en/function.file-get-contents.php is a way to fetch the content of another file and return it in a string format. 如http://php.net/manual/en/function.file-get-contents.php所述,file_get_contents是获取另一个文件的内容并以字符串格式返回的一种方法。
If you wanna extend a file with another files code you should look into require and/or include. 如果要用另一个文件代码扩展文件,则应查看require和/或include。
For your current code, swap out 对于您当前的代码,换出
file_get_contents("content/login_verif.php");
For 对于
require("content/login_verif.php");
Information regarding include: http://php.net/manual/en/function.include.php 有关的信息包括: http : //php.net/manual/en/function.include.php
Information regarding require: http://php.net/manual/en/function.require.php 有关需求的信息: http : //php.net/manual/zh/function.require.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.