这是我开始会话的地方。
的login.php
<?php
require_once("db.php");
require_once("functions.php");
if(isset($_POST['submit'])) {
global $connection;
$username = $_POST['username'];
$safe_username = mysqli_real_escape_string($connection, $username);
$password = $_POST['password'];
$query = "SELECT * FROM admin WHERE username = '{$safe_username}'";
$result = mysqli_query($connection, $query);
if($row = mysqli_fetch_assoc($result)) {
$set_password = $row['password'];
$input_password = crypt($password, $set_password);
if($input_password == $set_password) {
session_start();
$_SESSION['username'] = $safe_username;
header("location:users-area.php");
} else {
die(header("location:index.php?loginFailure=true"));
}
} else {
die(header("location:index.php?loginFailure=true"));
}
}
?>
listall.php
这里是我检查它的地方。
<?php
session_start();
if(emtpy ($_SESSION['username'])){
header("location:../login/index.php");
}
由于某种原因,这不起作用,如果您没有登录,则会将您带回登录页面,如果您正确登录,则允许您传递到listall.php。
希望有人能找到问题。答案 0 :(得分:1)
session_start()
必须放在login.php页面的顶部。
<?php
session_start();
require_once("db.php");
require_once("functions.php");
然后将其从代码的其余部分中删除。
答案 1 :(得分:0)
将login.php更改为:
<?php
session_start();
require_once("db.php");
require_once("functions.php");
if(isset($_POST['submit'])) {
global $connection;
$username = $_POST['username'];
$safe_username = mysqli_real_escape_string($connection, $username);
$password = $_POST['password'];
$query = "SELECT * FROM admin WHERE username = '{$safe_username}'";
$result = mysqli_query($connection, $query);
if($row = mysqli_fetch_assoc($result)) {
$set_password = $row['password'];
$input_password = crypt($password, $set_password);
if($input_password == $set_password) {
$_SESSION['username'] = $safe_username;
header("location:users-area.php");
} else {
die(header("location:index.php?loginFailure=true"));
}
} else {
die(header("location:index.php?loginFailure=true"));
}
}
?>
答案 2 :(得分:0)
您必须在Login.php文件中启动会话。否则您无法在login.php文件中使用会话。