【发布时间】:2012-07-02 05:58:23
【问题描述】:
我正在尝试创建两个单独的会话 - 一个用于如果用户是管理员,另一个用于如果用户是作者。 $type 存储类型为枚举(可以是作者或管理员)。但是我的代码甚至为管理员创建了作者会话。我是 PHP 和 MySQL 的新手。谁能告诉我代码中的错误在哪里。
<?php
include("dbconnect.php");
$con= new dbconnect();
$con->connect();
//create and issue the query
$sql = "SELECT type FROM users WHERE username = '".$_POST["username"]."' AND password = PASSWORD('".$_POST["password"]."')";
$result = mysql_query($sql);
//get the number of rows in the result set; should be 1 if a match
if (mysql_num_rows($result) == 1) {
$type_num=0;
//if authorized, get the values
while ($info = mysql_fetch_array($result)) {
$type =$info['type'];
}
if($type == "admin")
{
$_SESSION['type']=1;
$u = 'welcome.php';
header('Location: '.$u);
}
else
{
$_SESSION['type']=$type_num;
$u = 'welcome.php';
header('Location: '.$u);
}
}
else {
//redirect back to loginfailed.html form if not in the table
header("Location: loginfailed.html");
exit;
}
?>
我的welcome.php如下
<?php
session_start();
?>
<html>
<body>
<h2>Welcome.</h2>
<?
if($_SESSION['type']==1){
echo "You are of the usertype Admin and your session id is ";
echo session_id();
}
else {
echo "You are of the usertype Author and your session id is ";
echo session_id();
}
?>
</body>
</html>
非常感谢您。
【问题讨论】:
-
此时您的
$_SESSION中有什么内容?如果它完全为空,我不会感到惊讶,因为我在设置变量的页面上看不到session_start()(每个访问会话的页面都需要这个调用)。 -
顺便说一句,在不知道正确密码的情况下通过简单的 SQL 盲注即可轻松登录。尝试例如用户名:admin'--
-
你真的不需要这样做。是的,您的代码中存在 SQL 注入漏洞,请参阅 stackoverflow.com/questions/332365/… 和 bobby-tables.com。
标签: php session session-variables