的index.php
<?php
session_start();
if(isset($_SESSION['usr'])){
header("Location: browse.php");
}
?>
<!DOCTYPE html>
<html>
<body>
<form class="form col-md-12 center-block" action="login.php" method="post">
<div class="form-group">
<input type="text" class="form-control input-lg" placeholder="Email" name="mail" required="true">
</div>
<div class="form-group">
<input type="password" class="form-control input-lg" placeholder="Password" name="pass" required="true">
</div>
<div class="form-group">
<input type="submit" name="Signin" value="Sign In" class="btn btn-primary btn-lg btn-block">
</div>
</form>
</body>
</html>
的login.php
<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['Signin'])) {
$con=mysqli_connect(********);
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$mail=isset($_POST['mail']) ? $_POST['mail']:null;
$pass=isset($_POST['pass']) ? $_POST['pass']:null;
$sql2 = "SELECT mail, password FROM user_data where mail='$mail' and password='$pass'";
$result = mysqli_query($con, $sql2);
if(mysqli_num_rows($result) == 1){
$_SESSION['usr']=$mail;
mysql_close($con); // Closing Connection
header("Location: browse.php");
}
else {
header("Location: index.php");
}
}
?>
Browse.php
<?php
session_start();
if(!isset($_SESSION[‘usr’])){
header("Location: index.php");
}
?>
<html>
<!-- content here -->
<html>
在上面的代码中,index.php中的邮件值被赋予login.php中的$ _SESSION ['usr'],然后重定向到browse.php。这里,!isset($ _ SESSION ['usr'])是真的,$ _SESSION ['usr']也没有任何内容,因此加载回index.php。我可以获得上述代码的解决方案吗?
答案 0 :(得分:0)
你应该为$ _SESSION添加一个像这样的值
$_SESSION['user'] = 'yes';
然后在你的browse.php上
<?php
session_start();
if(empty($_SESSION['usr'])){
echo"<script>window.open('index.php','_self')</script>";
}
希望它有所帮助。