我正在处理登录后的页面将转到仅限会员的页面。我昨晚有这个登录工作,然后我改变了一些东西,现在似乎会话没有保持设置。我今晚失去了理智。感谢。
登录表单
<div style="position: absolute; bottom: 175px">
<button class="btn btn-default btn-xs" name="submit" role="button" data-toggle="modal" data-target="#myModal">Member Login</button>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Member Login</h4>
</div>
<div class="modal-body">
<form method="post" action="" name="login_form">
<p><input type="email" class="span3" name="email" placeholder="Email"></p>
<p><input type="password" class="span3" name="password" placeholder="Password"></p>
<p><button type="submit" id="submit" name="submit" value="Login" class="btn btn-primary">Sign in</button></p>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
的login.php
<?php
session_start();
include("connect.php"); //Establishing connection with our database
$error = ""; //Variable for storing our errors.
if(isset($_POST["submit"]))
{
if(empty($_POST["email"]) || empty($_POST["password"]))
{
$error = "Both fields are required.";
}else
{
// Define $email and $password
$email=$_POST['email'];
$password=$_POST['password'];
// To protect from MySQL injection
$email = stripslashes($email);
$password = stripslashes($password);
$email = mysqli_real_escape_string($db, $email);
$password = mysqli_real_escape_string($db, $password);
$password = sha1($password);
//Check email and password from database
$sql="SELECT id FROM Members WHERE email='$email' and password='$password'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
//If email and password exist in our database then create a session.
//Otherwise echo error.
if(mysqli_num_rows($result) == 1)
{
$_SESSION['email'] = $email; // Initializing Session
$error = "Success!";
echo "<div class='alert alert-danger alert-dismissible' role='alert'>
<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
<span aria-hidden='true'>×</span></button>".$error."</div>";
//header("location: members_only"); // Redirecting To Other Page
}else
{
$error = "Incorrect email or password.";
echo "<div class='alert alert-danger alert-dismissible' role='alert'>
<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
<span aria-hidden='true'>×</span></button>".$error."</div>";
}
}
}
?>
仅限会员 这就是我在会员页面顶部的内容
<?php
session_start();
if (!isset($_SESSION['email']))
{
header('Location: index');
}
?>
当我填写登录表单并提交正确的用户名和密码时,我将被定向到索引,因为我的$ _SESSION [&#39;电子邮件&#39;]未设置。如果我删除测试,它将正确转到仅限成员页面。
答案 0 :(得分:0)
我发现了我的问题。表单中的操作必须调出php文件。