会话变量给出错误。以前工作过

时间:2015-03-11 18:00:45

标签: php session indexing undefined

我似乎在我的php文件中收到错误。它工作得更早,但现在它似乎没有工作

不确定为什么它会给我这个错误

会话“login_user”在登录页面中创建,然后在目录页面中调用。并且出于某些奇怪的原因,每当我点击“空车”链接时它似乎会触发

然而,如果我删除上面的行,错误似乎消失了。但我需要该行供用户注销。

任何想法代码都错了

这是会话“login_user”的代码:

<?php
session_start(); // Starting Session
include_once('config.php');

$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['user']) || empty($_POST['pass'])) {
$error = "Please complete both fields";
}
else
{
// Define $username and $password
$user=$_POST['user'];
$pass=md5($_POST['pass']);
// To protect MySQL injection for Security purpose
$user = stripslashes($user);
$pass = stripslashes($pass);
$user = mysqli_real_escape_string($mysqli, $user);
$pass = mysqli_real_escape_string($mysqli, $pass);
// SQL query to fetch information of registered users and finds user match.
$query = mysqli_query($mysqli, "SELECT * FROM users where Username='$user' AND Password='$pass'");
$rows = mysqli_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$user; // Initializing Session
header("location: home.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($mysqli); // Closing mysqlinection
}
}
?>

我得到的错误是:注意:第28行的C:\ wamp \ www \ catalogue.php中的未定义索引:login_user

和第28行是:

Welcome, <?=$_SESSION['login_user'];?>! <br> <a href="logout.php">Logout</a> 

第28行被普通的html包围:

<div id="content">
    <!-- This is the actual menu --> 
    <ul id="darkmenu">
          <li><a href="home.php">Home</a></li>
          <li><a href="catalogue.php">Catalogue</a></li>
          <li><a href="search.php">Search</a></li>
          <li><a href= "view_cart.php">Cart</a></li>
          <li><a href="#">Orders</a></li>
    </ul>   

    <div id = "welcome" >
    Welcome, <?=$_SESSION['login_user'];?>! <br> <a href="logout.php">Logout</a>
    </div>

</div>

1 个答案:

答案 0 :(得分:0)

通知消息Notice: Undefined index: login_user in C:\wamp\www\catalogue.php on line 28只是意味着您应该在之前初始化变量,例如

$user = $_SESSION['login_user'];

听起来你好像忘记了session_start();在php文件之上。