【发布时间】:2015-06-21 16:49:42
【问题描述】:
我想知道如何使用程序样式检查用户是否已登录以及用户权限是否正确。我是 PHP 新手。我尝试使用的代码如下,但它根本不起作用我不知道为什么:
这是登录脚本
<?php
session_start();
$local=$_POST["local"];
$locales = array('001', '002', '003', '004', '005', '006', '007', '008', '009', '010');
if (in_array($local, $locales)){include ''.$local.'/enlace.php';}
else {header('Location: index.php?error=7');}
$locatario=mysqli_real_escape_string($database,$_POST['personal']);
$seguridad=mysqli_real_escape_string($database,$_POST['clave']);
if (empty($locatario) || empty($seguridad)| empty($local)){header('Location: index.php?error=1');exit();}
if (preg_match("/[^A-Za-z0-9]/", $locatario)){header('Location: index.php?error=2');exit();}
if (preg_match("/[^A-Za-z0-9]/", $seguridad)){header('Location: index.php?error=3');exit();}
$locatarios = mysqli_query($database, "SELECT * FROM `locatarios` WHERE locatario='$locatario' LIMIT 1");
if(mysqli_num_rows($locatarios)==0){header('Location: index.php?error=4');exit;}
$informacion=mysqli_fetch_array($locatarios,MYSQL_ASSOC);
$criptologia=hash('sha256',$informacion['codificacion'].hash('sha256',$seguridad));
if($criptologia!=$informacion['seguridad']){header('Location: index.php?error=5');exit;}
$_SESSION['identificacion']=$informacion['locatario'];
if ($informacion['privilegio']=="VENTAS"){header('Location: '.$local.'/ventas/index.php?funcion=inicio');exit();}
else if($informacion['privilegio']=="ADMINISTRACION"){header('Location: '.$local.'/administracion/index.php?funcion=inicio');exit();}
else if($informacion['privilegio']=="BODEGA"){header('Location: '.$local.'/bodega/index.php?funcion=inicio');exit();}
else if($informacion['privilegio']=="SOPORTE"){header('Location: '.$local.'/soporte/index.php?funcion=inicio');exit();}
else if($informacion['privilegio']=="PROPIETARIO"){header('Location: '.$local.'/propietario/index.php?funcion=inicio');exit();}
else if($informacion['privilegio']=="CLIENTES"){header('Location: '.$local.'/clientes/index.php?funcion=inicio');exit();}
else {header('Location: index.php?error=6');exit();}
?>
用户登录后,我们检查登录会话和权限,但出现错误
警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,数组在第 10 行的 /home/local/public_html/001/propietario/index.php 中给出
<?php
session_start();
$identificar = $_SESSION['identificacion'];
include 'sistema/enlace.php';
$locatarios = mysqli_query($database, "SELECT * FROM `locatarios` WHERE locatario='$identificar' LIMIT 1");
$controlar = mysqli_fetch_array($locatarios, MYSQLI_ASSOC);
$privilegio = $controlar["privilegio"];
if ($privilegio=='PROPIETARIO'){}
else {header('Location: /no-privs.php');exit();}
while($locatario = mysqli_fetch_array($controlar))
{
?>
<html>
<head>
<title>ADMIN ZONE</title>
</head>
<body>
WELCOME <?php echo $locatario[privilegio]?>, YOU ARE ADMIN ON THIS SITE</div>
</body>
</html>
<?php
}
?>
【问题讨论】:
-
您是否在每次测试用户是否为管理员时取消设置 PHP 会话?您可能需要关闭浏览器窗口或使用 Firebug 之类的工具来销毁测试轮之间的会话变量。
-
是的,我正在使用 logout.php 取消设置,还尝试使用 5 种不同的浏览器和 3 台 PC,甚至是手机和平板电脑
-
@Fred-ii- 不错,提到吸血鬼。
-
您正在为 $controlar ($controlar = mysqli_fetch_array(..)) 分配一个数组,然后再次对其执行 mysql_fetch_array()。这就是为什么它会给你一个错误。
-
警告:使用
mysqli时,您应该使用参数化查询和bind_param将用户数据添加到您的查询中。 请勿使用字符串插值来完成此操作,因为您将创建严重的SQL injection bugs。 从不将$_POST数据直接放入查询中。
标签: php mysql session login mysqli