会话id没有显示,提示错误信息Notice: Undefined index: UserID


Session id is not showing with an error message Notice: Undefined index: UserID

会话id没有显示错误消息注意:未定义的索引:UserID也如果我重定向非登录用户,我将不会登录到主页。这是我的文件,谁能告诉我哪里出错了?

这里是主页:

<?php 
require ("insert.php"); 
session_start();
if (isset($_SESSION["UserID"])){
}
else {
    echo "session not transferring";
}
?>

<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>AMSadler</title>
    <meta charset="utf-8">
    <meta name="description" content="description of webpage">
    <meta name="keywords" content="keywords go here">
    <meta name="author" content="Anthony">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/home.css">
    <link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>
        <div class="header">
            <div id="logo">
                <a href="index.html"><img src="img/logo.png" alt="logo" title="AMSadler.com"/></a>
            </div>
            <div id="headertop">
                <div id="pagetitle">
                    <a href="home.php">HOME</a>
                </div>
                <div id="pagetitle2">
                    <a href="profile.php">PROFILE</a>
                </div>
                <div id="pagetitle3">
                    <h4>Welcome:<?php echo $_SESSION{"UserID"};?>to your page</h4>
                </div>
            </div>
        </div>
            <div class="wrapper">
                <div class="leftsidebar">
                    <ul>
                        <li><a href="home.php">blah</a></li>
                        <li><a href="#">blah</a></li>
                        <li><a href="#">blah</a></li>
                        <li><a href="#">blah</a></li>
                        <li><a href="#">blah</a></li>
                        <li><a href="#">blah</a></li>
                        <li><a href="#">blah</a></li>
                        <li><a href="#">blah</a></li>
                        <li><a href="#">blah</a></li>
                        <li><a href="#">blah</a></li>
                    </ul>
                </div>
                <div class="main">
                    <div id="maintop">
                        <div id="textboxwrap">
                            <textarea name="text" id="styled"rows="5" cols="80" placeholder="Write something here.">
                            </textarea> 
                        </div>
                        <div id="postbutton">
                            <input type="submit" name="post" value="Post" />
                        </div>
                    </div>
                    <div id="mainbottom1">
                        <div ="mainbottomwrap">
                            Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>
                            Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>
                            Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>
                            Your posts will go here.<br>Your posts will go here.<br>
                        </div>
                    </div>
                </div>
                <div class="rightsidebar">
                    <div id="profilecontainer">
                        <img src="img/profile.png" alt="Upload a profile pic" title="profile pic" />
                    </div>
                    <div id="box">
                        <a href="#">blah</a><br>
                        <a href="#">blah</a><br>
                        <a href="#">blah</a><br>
                        <a href="#">blah</a><br>
                        <a href="#">blah</a><br>
                        <a href="#">blah</a><br>
                        <a href="#">blah</a><br>
                        <a href="#">blah</a>
                    </div>
                </div>
            </div>
</body>
</html>

==================================================================这是登录页面。+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?php require ("insert.php"); ?>
<?php
    if(isset($_POST[ 'Login' ]))
    {
    $email= mysqli_real_escape_string($con, $_POST ['email']);
    $pswrd= mysqli_real_escape_string($con, $_POST ['pswrd']);
    $result = $con->query (" select * from users where email='$email' AND pswrd='$pswrd' ");
    $row = $result->fetch_array(MYSQLI_BOTH);
    session_start();
    $_SESSION["User ID"] = $row['UserID'];
    header ('Location: home.php');
    }
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>AMSadler login</title>
    <meta charset="utf-8">
    <meta name="description" content="description of webpage">
    <meta name="keywords" content="keywords go here">
    <meta name="author" content="Anthony">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/login.css">
    <link rel="index" href="index.php">
    <link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>
        <div class="header">
            <div id="logo">
                <a href="index.html"><img src="img/logo.png" alt="logo" title="AMSadler.com"/></a>
            </div>
            <div id="signup">
                <button type="button"><a href="signup.php">Sign up</a></button>
            </div>
        </div>
        <div id="login">
            <form action="home.php" method="post">
                <input type="text" name="email" placeholder="Email address">
                <br>
                <input type="password" name="password" placeholder="Password">
                <br>
                <input id="Login" type="submit" name="Login" value="Login">
            </form>
        </div>
        <footer>
            <div id="copyright">
                <p>&copy Copyright 2015</p>
            </div>
        </footer>
</body>
</html>

我当然希望你只是张贴一个例子,你不存储明文密码值…无论如何…

我没有足够的代表给@Bono添加评论,但是除了他的发现之外,您还使用了以下POST:

$_POST ['pswrd']

但是在你的表单上,你有:

<input type="password" name="password" placeholder="Password">

需要更改密码的表单名或您正在查找的POST。

在你的登录页面中,你是这样分配变量的:

$_SESSION["User ID"] = $row['UserID'];

但是在首页你要找的是UserID

改变:

$_SESSION["User ID"] = $row['UserID'];

$_SESSION["UserID"] = $row['UserID'];

你得到错误的点是你在HTML中的使用,你说:

<h4>Welcome:<?php echo $_SESSION{"UserID"};?>to your page</h4>

在第一个实例中,您正在使用isset(),但在这里,您突然假设它在那里并准备使用。无论哪种方式,您都应该编辑User ID变量,这样就可以停止抛出错误。您可能还想在这里添加一个isset()语句。

注:还要注意维克多·佩雷斯关于你的密码的回答。并且不要将它们存储为纯文本。你实际上只是在检查数据库中的POST密码,这是不好的。此外,我可能会建议您远离使用这样的查询,并开始使用准备好的语句。