我需要能够定位与来自mysql数据库的数据进行通信的php代码,该文件被称为“validate.php”。它的主要功能是验证登录时是否有空字段,如果用户的值为1,则分配一个配置文件,如果表“记录”中的值为0,则分配另一个配置文件在php中的会话不加载配置文件,php + mysql
想法是,“validate.php”检查用户,并根据他们的个人资料将其引导到一个页面,但我不能这样做。
我的代码是:
<?php
require('access_db.php');
session_start();
if(isset($_POST['send'])) { // We verify that the form data has been sent
//We verify that the user_name and the user_pass fields are not empty
if(empty($_POST['user_name']) || empty($_POST['user_pass'])) {
echo"
<script>
alert('Please enter your username and password correctly ');
location.replace('login.php');
</script>
";
}else {
//"Clean" the form fields of possible malicious code
$user_name = mysqli_real_escape_string($link,trim($_POST['user_name']));
$user_pass = mysqli_real_escape_string($link,trim($_POST['user_pass']));
// We verify that the data entered in the form match those of the DB
$query=mysqli_query($link,"select user_id,user_name,user_admin FROM users WHERE user_name='".$user_name."' and user_pass ='".$user_pass."'");
$row = mysqli_fetch_array($query);
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row["user_name"];
$_SESSION['user_admin'] = $row["user_admin"];
if($_SESSION['user_admin']==1){
echo "dashboard.php";
}else{
echo "dashboard2.php";
}
{
}
}else{
header("Location: login.php");
}?>
我的主要问题是在这里:
if($_SESSION['user_admin']==1){
echo "dashboard.php";
}else{
echo "dashboard2.php";
}
当我在我的页面“的login.php”我的管理员用户登录,你应该检查信息和去根据您的个人资料页面只出现在浏览器“http://localhost/proyect/validate.php”和页面上的文本“仪表板”上,但是,如果我在浏览器“http://localhost/proyect/dashboard.php”中加载了带有所有信息的页面。
我不知道我在做什么错。 有人可以帮助我,我会非常感激,我已经这么多天了。 谢谢。
为什么在屏幕上打印时想要重定向它们。 – programmingArrow
您是否将密码以明文形式存储在数据库中? **不要以明文存储密码!**只存储密码哈希值!使用PHP的['password_hash()'](http://php.net/manual/en/function.password-hash.php)和['password_verify()'](http://php.net/manual/en /function.password-verify.php)。如果您运行的PHP版本低于5.5(我希望不是),那么可以使用[password_compat库](https://github.com/ircmaxell/password_compat)来获得相同的功能。 –
您的代码易受SQL注入的影响 - 使用预准备语句而不是直接在sql – RamRaider