我正在使用PHP创建一个应用程序,我正在尝试使用$ _SESSION超全局传递信息,但由于某些原因,其中一个页面$ _SESSION无效。我有一个注册表,一个登录表和一个管理页面。当用户注册成功后,他将被重定向到登录页面以添加他的凭据。当用户添加他的凭据时,他将重定向到管理页面。在管理页面中,我有一条欢迎信息。消息为<?php echo "Welcome ". $_SESSION['name']; ?>
此外,当用户转到管理页面时,消息为空。它不会读取$_SESSION['name']
。我有print_r($_SESSION)
但数组是空的。
有人能告诉我我做错了吗?
感谢。
CODE:
报名表:
<?php include "includes/header.php"; ?>
<?php include "includes/navigation.php"; ?>
<?php session_start(); ?>
<div class="container">
<div class="row">
<div class="col-xs-6 col-xs-offset-3">
<div class="well">
<h4>Registration Form</h4>
<form method="post" action="">
<?php
if(isset($_POST['register'])) {
$name = mysqli_real_escape_string($connection, $_POST['firstname']);
$lastname = mysqli_real_escape_string($connection, $_POST['lastname']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is not a valid email address<br>");
}
$password = mysqli_real_escape_string($connection, $_POST['password']);
$dob = mysqli_real_escape_string($connection, $_POST['dob']);
$gender = mysqli_real_escape_string($connection, $_POST['gender']);
$password = password_hash($password, PASSWORD_BCRYPT);
$check_if_user_exist = "SELECT email FROM users WHERE email = '$email' ";
$check_if_user_exist_result = mysqli_query($connection, $check_if_user_exist);
if(mysqli_num_rows($check_if_user_exist_result) >= 1) {
echo "User Exist... <br>Please try another email";
}else {
$add_user_query = "INSERT INTO users (firstname, lastname, email, password, dob, gender) ";
$add_user_query .= "VALUES ('$name', '$lastname', '$email', '$password', '$dob', '$gender' ) ";
$add_user_query_result = mysqli_query($connection, $add_user_query);
$_SESSION['name'] = $name;
$_SESSION['lastname'] = $lastname;
$_SESSION['email'] = $email;
header("Location: login.php");
}
}
?>
<div class="form-group">
<label for="fistname">Name</label>
<input type="text" name="firstname" id="fistname" class="form-control">
</div>
<div class="form-group">
<label for="lastname">Lastname</label>
<input type="text" name="lastname" id="lastname" class="form-control">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="text" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<label for="dob">Date of Birth</label>
<input type="date" name="dob" id="dob" class="form-control">
</div>
<div class="form-group">
<label for="gender">Male of Female</label>
<div class="radio">
<label><input type="radio" name="gender" value="male">Male</label>
</div>
<div class="radio">
<label><input type="radio" name="gender" value="female">Female</label>
</div>
</div>
<div class="form-group">
<input type="submit" name="register" class="btn btn-primary" value="Register">
</div>
</form>
</div>
</div>
</div>
</div>
<?php include "includes/footer.php"; ?>
登录表格代码:
<?php include "includes/header.php"; ?>
<?php include "includes/navigation.php"; ?>
<?php session_start(); ?>
<div class="container">
<div class="row">
<div class="col-xs-6 col-xs-offset-3">
<div class="well">
<h4>Login Form</h4>
<form method="post" action="">
<?php
if(isset($_POST['login'])) {
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, $_POST['password']);
$collect_users_info = "SELECT * FROM users WHERE email ='$email' ";
$collect_users_info_results = mysqli_query($connection, $collect_users_info);
$db_password = "";
while($row = mysqli_fetch_assoc($collect_users_info_results)) {
$db_name = mysqli_real_escape_string($connection, $row['name']);
$db_email = mysqli_real_escape_string($connection, $row['email']);
$db_password = mysqli_real_escape_string($connection, $row['password']);
}
if(password_verify($password, $db_password) && $email == $db_email) {
header("Location: admin/index.php");
} else {
echo "<h5>Email or Password is Incorect</h5>";
}
}
?>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="text" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<input type="submit" name="login" class="btn btn-primary" value="Login">
</div>
</form>
</div>
</div>
</div>
</div>
<?php include "includes/footer.php"; ?>
管理页面:
<?php include "includes/admin_header.php"; ?>
<?php session_start(); ?>
<div id="wrapper">
<?php include "includes/admin_navigation.php"; ?>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
<?php echo "Welcome ". $_SESSION['name']; ?>
</h1>
</div>
</div>
</div>
</div>
</div>
<?php include "includes/admin_footer.php"; ?>
答案 0 :(得分:-1)
确保包含每个其他php页面中包含数据库连接代码的php文件。