注意:未定义的变量:用户名

Notice: Undefined variable: username

我的问题 = 为什么我尝试在 editor.php 中使用 $username 时没有为它分配值?

我有一个可用的登录系统。用户登录后,我想创建一个显示 "Welcome [INSERT USERNAME HERE]" 的欢迎行。目前,我在获取回显以检索变量“$username”时遇到问题,因为我收到此错误:

Notice: Undefined variable: username in editor.php on line 60.

这是 editor.php 中的第 60 行。请注意,editor.php 是用户成功登录后被定向到的页面

<h4>Welcome <?php  echo $username;?> , to the editor.</h4>

$username 变量在 loginAuth.php 中声明,当用户在 login.php 上提交 his/her 登录详细信息时运行该变量。

下面是loginAuth.php的相关代码:

<?php
error_reporting(E_ALL);  
ini_set('display_errors', 1); 

//Connect to DB
include_once dirname(__FILE__).'/db_connect.php';
// PHP >= 5.3
include_once __DIR__.'/db_connect.php';



//starts session
session_start();
if (isset($_POST['loginSub'])) {
//Variables declaration
$username = $_POST['user'];
$password = $_POST['password'];

//if username doesnt not equal empty space and password does not equal empty  
space, then...
if(trim($username) != '' and trim($password) != ''){

//Sanitizes whatever is entered 
$username=stripslashes($username);
$password=stripslashes($password);

$username=strip_tags($_POST['user']);
$password=strip_tags($_POST['password']);


$username=mysqli_real_escape_string($conn,$username);
$password=mysqli_real_escape_string($conn,$password);

//SQL query 
$query = mysqli_query($conn, "SELECT * FROM users WHERE user='$username' AND 
password = '$password' ")or die(mysqli_error($conn));
//applies msqli_num_rows to the $query. Counts how many rows 
$numrows=mysqli_num_rows($query);
//if there is a row that matches what has been entered in form, then...
if($numrows > 0){

//initialize session
$_SESSION['login_user']= $username; 
//take them to editor.php   
header("location: editor.php"); // Redirecting To Other Page
exit;
}else{
     echo "Username or password is incorrect.";
    }
}else{  
    echo "Please enter information";
}
}
?>

所以基本上,editor.php 页面由于某种原因无法获取 $username 变量,因为显然它没有任何价值...即使我的登录页面成功运行。

根据要求,我的 HTML 表格 login.php:

 <form id="login" method="POST" action="loginAuth.php">

                    <label for="user"><strong>Login:</strong></label>
                    <input type="text"size=20 autocorrect=off autocapitalize=words name="user">
                    <label for="password" > <strong>Password:</strong></label> 
                    <input type="password" name="password">

                    <input name="loginSub" type="submit" value="Login"> 
                </form>  

以下是我研究过但未能应用于我自己的问题的其他问题:

Reference - What does this error mean in PHP?

PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

当用户直接访问它们时,单个 .php 文件彼此完全独立,就像您重定向它们时一样。

由于您将用户重定向到 editor.php,因此您在 login 中设置的变量 $username 不存在。这是一个处理请求的不同进程。

editor.php 需要以某种形式处理用户登录,您可能需要查看 PHP 会话处理。

//initialize session
$_SESSION['login_user']= $username; 

您在此处将用户名存储在会话变量中;一种选择是通过做...

editor.php 中反映这一点
$username = $_SESSION['login_user'];

您的变量在 editor.php

中不存在

您需要像这样从您的 $_SESSION 获取:

editor.php

<?php
$username = $_SESSION['login_user'];
....
<h4>Welcome <?php  echo $username;?> , to the editor.</h4>