会话ID未显示错误消息提示:未定义索引:UserID如果我重定向未登录的用户我将不会登录主页iether ..这是我的文件可以有人请告诉我我要去哪里错。
这是主页:
<?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>© Copyright 2015</p>
</div>
</footer>
</body>
</html>
答案 0 :(得分:2)
我当然希望您只是发布一个示例,并且您没有存储明文密码值...无论如何......
我没有足够的代表向@Bono添加评论,但除了他的调查结果外,您还使用了以下POST:
$_POST ['pswrd']
但是在你的表格上,你有:
<input type="password" name="password" placeholder="Password">
密码的表单名称需要更改,或者您正在寻找的POST。
答案 1 :(得分:1)
在您的登录页面中,您将变量分配为:
$_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()
语句。
P.S。另请注意Victor Perez的回答,并提供密码。并且不要将它们存储为纯文本。您只是在数据库中检查密码的POST
密码,这没什么用。另外,我建议您不要使用此类查询并开始使用prepared statements。