它的用途是什么?:我们正在建立一个网络应用网站,您需要登录才能继续浏览所需的网页。
此文件的目标:使用connection.php
与数据库建立连接。并从用户将其凭据登录的html表单index.php
中检索数据。
在设置了变量$username
和$password
之后,如果凭据在数据库中,则脚本必须与db一起检查,如果是,则继续下一页。
问题:起初它起作用了,我没有得到这个错误,但突然在试图改进它的代码后,它打破了...现在我得到以下PHP
错误:
注意:未定义的变量:第34行的C:\ xampp \ htdocs \ PAD-Academy \ PHP \ login.php中的login_user
我已经尝试声明var但是这不起作用,我想我做了某事......某处......错了,但我找不到它。在堆栈上有类似的帖子,我试图在它的帮助下解决它,但没有成功...
这是我的代码如下:
文件名:login.php
<?php
session_start();
include("connection.php"); //Includes the connection file to get to the DB credentials.
$error = "" ; //Empty var to store errors.
if(isset($_POST["username"])){
if(empty($_POST["username"]) || empty($_POST["password"])){
header("location: ../index.php");
} else {
//Define var and inserts the data from the index.php form.
$username=$_POST['username'];
$password=$_POST['password'];
//protection agianst MYSQL injection (in adress link)
$username = stripslashes($username); //stripslashes deletes /
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username); //escape string deletes strings.
$password = mysqli_real_escape_string($db, $password);
//$password = md5($password); //For future use when passwords are "encrypted" with md5.
//Check username and password from database.
$sql="SELECT idaccount FROM pad_db.account WHERE username='$username' AND password='$password';";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
//If username and password exist in the database then create a sessoin.
//otherwise echo error.
if(mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $login_user; //Initializing Session
//header("location: ../studenthome.html"); //Redirecting To Other page.
//Insert rank check before header();.
} else {
$error = "Username or password incorrect!";
echo $error;
//header("location: ../index.php");
//Moet een error komen op login page, dit is even een tijdelijke oplossing.
}
}
}
//DATABASE TABLE:
// CREATE TABLE IF NOT EXISTS `pad_db`.`account` (
// `idaccount` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',
// `username` VARCHAR(45) NOT NULL COMMENT '',
// `password` VARCHAR(45) NOT NULL COMMENT '',
// `rank` ENUM('TEACHER','STUDENT') NOT NULL COMMENT '',
// `fname` VARCHAR(45) NULL DEFAULT NULL COMMENT '',
// `mname` VARCHAR(45) NULL DEFAULT NULL COMMENT '',
// `lname` VARCHAR(45) NULL DEFAULT NULL COMMENT '',
// `email` VARCHAR(45) NULL DEFAULT NULL COMMENT '',
// PRIMARY KEY (`idaccount`) COMMENT '',
// UNIQUE INDEX `idaccount_UNIQUE` (`idaccount` ASC) COMMENT '',
// UNIQUE INDEX `username_UNIQUE` (`username` ASC) COMMENT '',
// UNIQUE INDEX `email_UNIQUE` (`email` ASC) COMMENT '')
// ENGINE = InnoDB
// AUTO_INCREMENT = 5
// DEFAULT CHARACTER SET = utf8;
?>
答案 0 :(得分:1)
问题在于:
if(mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $login_user; //Initializing Session
//header("location: ../studenthome.html"); //Redirecting To Other page.
//Insert rank check before header();.
}
您已使用$login_user
但尚未向$ login_user分配任何内容,这就是未定义 {{1} NOTICE
的原因}。
您应该放置$login_user
。
那应该解决你的问题。 干杯