登录时显示未定义索引错误?

时间:2018-07-05 05:10:34

标签: php

所以我在登录门户网站上工作,我希望用户不要直接进入链接-

  

http://localhost/working/HomePage.php

所以我在HomePage.php中添加了这段代码-

   session_start();
if($_SESSION["user"] == null)
{
    include 'Login.php';
}

我正在另一个文件AccessControl.php中设置会话变量

if ($_POST['password'] == "password" and $row["Emp_Password"] == "password")
                {
                    include 'ChangePassword.php';
                }
                elseif ($row["Emp_Password"] == $_POST['password'])
                {
                    $_SESSION["user_pwd"] = $_POST['password'];
                    $_SESSION["user"] = $_SESSION["user_id"];
                    header('Location: HomePage.php');
                }
                else
                {
                    echo "<script type='text/javascript'>alert('Invalid Password!');</script>";
                    include 'Login.php';
                }

当我尝试访问HomePage.php时,它会将我重定向到登录页面,但出现错误-

  

注意:未定义索引:C:\ xampp \ htdocs \ Working \ HomePage.php中的用户   在第8行

他们是否可以将我重定向到首页,并且也不会显示此错误? 同样,在我重定向到Login.php之后,我也无法登录。 预先感谢。

2 个答案:

答案 0 :(得分:2)

您可以执行类似下面的操作,而不是推荐include,而不是header。也请按照上述答案中的说明使用empty

session_start();
if (empty($_SESSION["user"]))
{
    header("Location:Login.php");
}

答案 1 :(得分:1)

您可以进行如下检查:

with cte as 
(
Select distinct(id) as Id from table1 union Select distinct(ParentId) as Id from table1
)
Select cte.Id, CASE WHEN table2.Id is null THEN 'N' ELSE 'Y' END As IsMatched, table3.TID
from cte
left join table2
on table2.Id = cte.Id
left join table3 
on table3.TID = table2.CID

详细了解empty