【发布时间】:2018-02-27 10:06:57
【问题描述】:
我从数据库中存储的变量有问题。它涉及变量$username,它通过while循环从其中的mij DB中获取数据。但是,当我尝试在我的 HTML 页面上显示这个 var 时,它变成空白我在这里做错了什么?
<?php
session_start();
require_once('connect.php');
if(isset($_POST) AND !empty($_POST)){
$emaillogin = $_POST['emaillogin'];
$passwordlogin = md5($_POST['passwordlogin']);
$sqllogin = "SELECT * FROM `login` WHERE Email = '$emaillogin' OR Username = '$emaillogin' AND Password = '$passwordlogin'";
$resultlogin = mysqli_query($connection, $sqllogin);
$count = mysqli_num_rows($resultlogin);
if($count == 1){
$_SESSION['user'] = $resultlogin;
while($row = mysqli_fetch_array($resultlogin)){
$username = $row['Username'];
}
$url = "../index.php";
$messageok = "User login succesfull!";
echo "<script type='text/javascript'>alert('$messageok');</script>";
echo '<script>window.location = "'.$url.'";</script>';
}else{
$url = "../index.php";
$messagenok = "User login failed!";
echo "<script type='text/javascript'>alert('$messagenok');</script>";
echo '<script>window.location = "'.$url.'";</script>';
}
}
?>
<div id="myLeftRow" class="leftrow" style="display: inline-block;">
<div class="leftrow-row">
<?php if(isset($_SESSION['user'])){
echo "<button class='button' id='profilebutton'>".$username."</button>";
echo "<form method='POST' action='includes/logout.php'><button type='submit 'class='button' id='logoutbutton'>Logout</button></form>";
}
else{
echo "<button onclick='logintoggle()' class='button' id='loginbutton'>Login</button>";
echo "<button onclick='registertoggle()' class='button' id='registerbutton'>Register</button>";
}
?>
【问题讨论】:
-
如果您的 html 代码在其他页面上,您需要通过查询字符串(如果重定向)或通过会话传递用户名。
-
它与 PHP 代码在同一个文件页中,并且都包含在索引页中会导致问题吗?
-
首先,您应该对条件进行分组。
something AND other OR final会给出误报。整个页面是空白的,还是只是用户名没有值? -
使用旧的密码加密方法(例如
sha1、md5)是糟糕的散列方法 - 您应该使用较新的方法来散列密码。 PHP 有一个内置的password_hash()函数,它更安全! -
您已经在使用支持 prepared statements 和有限变量输入的 API,您应该使用带有占位符的参数化查询(prepared statements)来保护您的数据库免受SQL-injection !开始使用
mysqli::prepare()和mysqli_stmt::bind_param()。