Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 验证用户时重定向页面_Php_Html_Session_Verify - Fatal编程技术网

Php 验证用户时重定向页面

Php 验证用户时重定向页面,php,html,session,verify,Php,Html,Session,Verify,我有这段代码来验证用户是否有我网站后台的管理员帐户,但若用户并没有,就不要将用户重定向到..index.php。他留在这个页面,但没有显示任何内容 验证代码 <?php $Usuario = isset($_SESSION["Usuario"]) ? $_SESSION["Usuario"]: ''; $Rank = isset($_SESSION['Rank']) ? $_SESSION['Rank'] : ''; if ($Usuario != '' &&am

我有这段代码来验证用户是否有我网站后台的管理员帐户,但若用户并没有,就不要将用户重定向到..index.php。他留在这个页面,但没有显示任何内容

验证代码

<?php
$Usuario = isset($_SESSION["Usuario"]) ? $_SESSION["Usuario"]: '';
$Rank    = isset($_SESSION['Rank'])    ? $_SESSION['Rank']   : '';

if ($Usuario != '' && $Rank == 'Administrador'){  
} 
else 
{
    echo "<script>alert(\"Area Restrita\");</scrpit>";
    header("Location: ../index.php");
}
?>
在这个页面中,我调用这个文件来验证会话

<?php
session_start(); 
require_once "../config.php";
require "verificar.php";

?>

<div id="header">
        <a href="index_logged.php?page=index"><img src="img/logo.png"></a>
</div>
您的索引文件:

<?php
session_start(); 
require_once "../config.php";
require "verificar.php";

?>

<div id="header">
        <a href="index_logged.php?page=index"><img src="img/logo.png"></a>
</div>
现在,受限制的页面将从以下代码开始:

<?
    $isAdministrator = $_SESSION['isAdministrator'];
    if(!$isAdministrator) {
        ban_ban_ban();
        die('bye bye');
    }

    // content for administrator
?>
注意:这只是一个例子,别忘了在所有地方添加一些检查!!!!!十一,

但是,如您所愿:希望,这将对您有所帮助。

标题位置:../index.php;不会阻止其余代码的运行-如果你只是想重定向他,你应该死;或退出;在您发送位置标题之后

位置标题之前的警报部分也是不必要的,因为浏览器将在用户能够看到警报之前重定向用户。此外,还禁止在向输出发送内容后调用header函数,例如,像echo那样

您应该考虑的另一个问题是,通过查看$AY会话中的值来提高用户的安全性,这意味着如果有人登录了,直到会话到期

,您就无法将他注销出去。
更好的方法是在$_会话中保留一些令牌,并将用户的状态保存在数据库中-这样,您可以直接从数据库更改其状态,而不依赖会话/更改代码

这是什么意思,但如果用户没有访问权限,请不要将用户重定向到..index.phpIf user is Member,他不会将用户重定向到../index.php。只有管理员才能访问此页面。为什么要执行PHP重定向和JS重定向?PHP就足够了。还添加一个“出口;”在标题“位置:…”之后;为了良好的实践。如果您在标题位置之前输出脚本标记,您将收到警告:警告:标题已发送到。。。使PHP重定向无法正常工作
<?
    $username = $_POST['username'];
    $password = $_POST['password'];

    // here is some query which will check if this user with this password exists and get the role of the user
    // if exists $userExists = true; else $userExists = false;
    if($userExists) {
        $_SESSION['userLoggedIn'] = true;
        if($role == 'administrator') {
            $_SESSION['isAdministrator'] = true;
        }
        else
        {
            $_SESSION['isAdministrator'] = false;
        }
        header('Location: index.php');
            exit(); // <-- don't forget this
    }
    else
    {
        // handler for bad user/password
    }
?>

<form action='' method='post'>
    <input type='text' name='username' />
    <input type='password' name='password' />
</form>
<?
    $isAdministrator = $_SESSION['isAdministrator'];
    if(!$isAdministrator) {
        ban_ban_ban();
        die('bye bye');
    }

    // content for administrator
?>