<?php
session_start();
include("connection.php");
if(isset($_GET['submit'])) {
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('awnb') or die(mysql_erroe());
$Email=$_GET['LoginEmail'];
$password=$_GET['LoginPassword'];
if($Email!=""&&$password!="") {
$query=mysql_query("select * from users where Email='".$Email."' and Password='".$password."'") or die(mysql_error());
$res=mysql_fetch_row($query);
if($res) {
$userType = $res['Type'];
if($userType == 'user') {
$_SESSION['Email']=$Email;
header("location:profileuser.php");
exit;
} else if($userType == 'sec') {
$_SESSION['Email']=$Email;
header("location:profile.php");
exit;
}
} else {
echo "
<script type='text/javascript'>
alert('Username or Password is incorrect');
</script>";
header("location:index.php");
}
} else {
echo "
<script type='text/javascript'>
alert('Enter both Username and Passowrd');
</script>";
header("location:index.php");
}
}
?>
- 在此代码中,Type 是表中的一个属性,它表示用户的类型,有两种类型的用户称为 user 或 sec。如果类型是用户,它应该重定向到 profileuser.php,否则重定向到 profile.php,但我无法根据用户类型重定向页面。
最佳答案
为了回答您的问题,您正在获取具有 numeric index 的行:
mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.
如果想得到关联数组,需要mysql_fetch_assoc
.
也就是说,您的代码有太多错误,您可能应该重新开始:
mysql_*
函数已弃用;- 你有一个 SQL injection问题;
- 您正在存储纯文本密码;
- 您在
header
重定向之前回显输出; - 您使用 GET 而不是 POST 来发送敏感的登录数据。
您可以在 SO 上找到有关每个点的大量信息,因此我不打算重复,但正如我所说,您可能应该重新开始。
关于php - 根据用户类型重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29459814/