在管理员检查会话变量时苦苦挣扎

时间:2018-04-16 22:36:53

标签: php mysql

我有2页。我需要在“addproduct.php”上检查用户是否以管理员身份登录。我有一个登录脚本。抱歉,如果这是一个愚蠢的问题,但我是PHP的新手。

我希望到达此页面但未以管理员身份登录的用户('isadmin'是用户数据库中的一行)重定向到登录页面,并且有人以管理员身份登录要显示的页面。

的login.php;

<?php
session_start();

$un = $_POST["username"];
$pw = $_POST["password"];

$conn = new PDO ("mysql:host=localhost;dbname=assign026;", "assign026", 
"ziSietiu");
$results = $conn->query("select * from users where username='$un' and 
password='$pw'");

$row = $results->fetch();
if($row == false)
{
echo "Incorrect password!";// There were matching rows
}
else
{
$_SESSION["gatekeeper"] = $un;

$_SESSION["isadmin"] = $row["isadmin"];

header ("Location: index.php");
}

?>

和addproduct.php

<?php
session_start();

?>

<?php
// Test that the authentication session variable exists
if(!isset($_SESSION["isadmin"]) || $row["isadmin"] == 1)
{
header('Location: login.html');
exit();
}
else
{
echo ($_SESSION["isadmin"]);
}
?>

<div>

<h2>Add new product</h2>

<form method="post" action="addproductscript.php">
  <p>Insert product here</p>
  <input type="text" name="name" placeholder="name">
  <input type="text" name="manufacturer" placeholder="manufacturer">
  <input type="text" name="description" placeholder="description">
  <input type="text" name="price" placeholder="price">
  <input type="text" name="stocklevel" placeholder="stocklevel">
  <input type="text" name="agelimit" placeholder="agelimit">
  <input type="submit" value="Submit">
 </form>

 </div>

1 个答案:

答案 0 :(得分:0)

根据您的代码df = pd.DataFrame({'customer_id': ['abc','abc','xyz','xyz','xyz','xyz','thr','thr','abc','abc','urt','urt'], 'transaction_id': ['A123','A123','B345','B345','C567','C567','D678','D678','E789','E789','D903','F865'], 'product_id': [255472, 251235, 253764,257344,221577,209809,223551,290678,908354,909238,436758,346577], 'product_category': ['X','X','Y','Y','X','Y','Y','X','Y','Z','X','X']}) g = df.groupby(['customer_id', 'transaction_id'])['product_category']\ .apply(lambda x: {i for i in x if i in ('X', 'Y')}).reset_index() g2 = g.groupby('customer_id')['product_category']\ .apply(list).apply(lambda x: ({'X'} in x) and ({'Y'} in x)) print(g2) # customer_id # abc True # thr False # urt False # xyz False # Name: product_category, dtype: bool df['flag'] = df['customer_id'].isin(g2[g2].index) print(df) # customer_id product_category product_id transaction_id flag # 0 abc X 255472 A123 True # 1 abc X 251235 A123 True # 2 xyz Y 253764 B345 False # 3 xyz Y 257344 B345 False # 4 xyz X 221577 C567 False # 5 xyz Y 209809 C567 False # 6 thr Y 223551 D678 False # 7 thr X 290678 D678 False # 8 abc Y 908354 E789 True # 9 abc Z 909238 E789 True # 10 urt X 436758 D903 False # 11 urt X 346577 F865 False if(!isset($_SESSION["isadmin"]) || $row["isadmin"] == 1)未定义,因此没有任何价值。

您可以做的是$row["isadmin"]