Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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_Sql_Admin_Account - Fatal编程技术网

Php 添加管理员帐户检查

Php 添加管理员帐户检查,php,sql,admin,account,Php,Sql,Admin,Account,我最近开始学习PHP,遇到了一些麻烦。我在这里看了一些其他的答案,但仍然不能解决它,所以同位语 我试图添加一个检查,看看一个用户帐户是否是管理员,然后直接到一个管理员页面,如果它是或索引页面,如果它不是管理员帐户。在我的用户表中,我有一个名为“user_type”的列,管理员被设置为“admin”。我如何让我的登录代码检查并适当重定向 <?php ob_start(); $host="localhost"; // Host name $username="root";

我最近开始学习PHP,遇到了一些麻烦。我在这里看了一些其他的答案,但仍然不能解决它,所以同位语

我试图添加一个检查,看看一个用户帐户是否是管理员,然后直接到一个管理员页面,如果它是或索引页面,如果它不是管理员帐户。在我的用户表中,我有一个名为“user_type”的列,管理员被设置为“admin”。我如何让我的登录代码检查并适当重定向

    <?php

    ob_start();
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="db_tc"; // Database name 
$tbl_name="tbl_users"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

// Password Hashing
$mypassword = hash("sha512", $mypassword);

//SQL injection protection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE email='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$ar=mysql_fetch_array($result);
if($ar['user_type']=='admin')
{
 header('Location: admin.php');
}
else
{
 header('Location: index.html');
}

// Count table rows.
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

    $_SESSION["login"] = ("usernameEntered");
    header("location:index.html");
}

else {
    echo "Wrong Username or Password";
}
ob_end_flush();
?> 
顺便说一句:请不要使用sha512。改用河豚!
河豚需要盐。Sha512没有这样做,因此您无法通过包含河豚的哈希表获得passowrd。第二件事是,blowfish需要更多的时间进行散列,因此也不可能进行暴力攻击。

Mysql函数已被弃用。您应该使用mysqli或PDO。准备好的语句可以防止sql注入。在sql查询中添加LIMIT子句可能会更有效;我假设用户名(和密码)最多只能有一行。如果您随后没有使用行数据作为@litilion答案,那么您将能够通过使用计数使其更加有效。另外,我注意到您在mysql\u real\u escape\u字符串之前使用了stripslashes;如果这是因为您已经启用了MigICHQueSugGPC,那么您可能会考虑禁用这个。啊,我没怎么查过。这样做简单吗?在学习PHP的早期,尽量不要让自己负担过重。@AlexW,相对直截了当;您可以使用
SELECT COUNT(*)
检查返回的计数,而不是对
mysql\u num\u rows
使用
SELECT COUNT(*)
。一个好的教程是非常有用的。但由于需要检查
tbl\u users.user\u type
属性,因此不能在此处使用COUNT.:)谢谢你的回复。我将调查河豚。谢谢。它仍然会重定向到同一页。如果他们不是管理员,我如何处理它进入index.html?很抱歉我还在学习,谢谢。我现在的问题是,无论输入什么,它总是指向同一页面,即使登录信息错误:扫描你,请更新页面上的脚本,以便我可以查看?也许你实施错了…哦,我的错。我很抱歉。请将if else子句放在ob_end_flush()之后;
$ar=mysql_fetch_array($result);

// Count table rows.
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

if($ar['user_type']=='admin')
{
 header('Location: adminhome.php');
}
else
{
 header('Location: index.html');
}

}
else {
    echo "Wrong Username or Password";
}
$ar=mysql_fetch_array($result);
if($ar['user_type']=='admin')
{
 header('Location:http://example.com/admin.php');
}
else
{
 header('Location:http://example.com/index.php');
}