我似乎无法获得$ _SESSION项来往/从不同页面传送。我已验证PHP.ini文件中的“ use_only_cookies = 1”。最初加载站点时,第一页是“ index.php”。该页面加载主页,菜单等。当用户按下“ Login”菜单选项时,代码将启动“ login.php”文件,并且在该文件中,我设置了两个$ _SESSION字段,以指示登录成功另一个保留用户名。然后,使用header()函数的代码将调用“ index.php”文件,其中的会话字段现在显然为NULL,并且无法正确评估。下面是这两个文件以及HTML头文件,如下所示。我也看过http://stackoverflow.com/questions/5849898/php-sessions-not-working。任何帮助,将不胜感激。谢谢。
文件:Index.php
<?php
session_start();
$page_title='Welcome to this Site!';
include('php/vmenus.php');
include('html/header.html');
?>
<!-- Image Slider -->
<div id="slideshow">
<div>
<img src="images/test1.jpg" width="1000" height="600">
</div>
<div>
<img src="images/test2.jpg" width="1000" height="600">
</div>
<div>
<img src="images/test3.jpg" width="1000" height="600">
</div>
</div>
<!-- Content Area -->
<div id="content">
<div class="row">
<div class="column2left">
<h1>Content Header</h1>
<p>
Paragraph of text
</p>
</div>
<div class="column2right">
<p> The Right Side</p><br>
<?php
vmenustart();
vmenuitem("#","Calendar");
vmenuitem("#","Public Records");
vmenuitem("#","Menu 3");
vmenuitem("#","Menu 4");
vmenuend();
?>
</div>
</div>
<?php
include('html/footer.html');
?>
文件:Login.php
<?php
$page_title="Login";
// if user is already logged in to the website, then redirect them to the home page
// and terminated this script
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
header("location: index.php");
exit;
}
include('php/vmenus.php');
include('php/forms.php');
// open the master database code
require_once "php/dbconfig.php";
$username = "";
$password = "";
$username_err = "";
$password_err = "";
// Process FORM data when the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//Check if username is empty
if (empty(trim($_POST["username"]))) {
$username_err = "Please enter a valid username.";
}
else {
$username = trim($_POST["username"]);
}
// Check if password is empty
if(empty(trim($_POST["password"]))){
$password_err = "Please enter your password.";
} else{
$password = trim($_POST["password"]);
}
// Validate credentials
if (empty($username_err) && empty($password_err)){
// Prepare a select statement
$sql = "SELECT UserID, Passwrd FROM Users WHERE UserID = ?";
if ($stmt = mysqli_prepare($link, $sql)){
// Set parameters
$param_username = $username;
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);
// Check if username exists, if yes then verify password
if (mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $username, $hashed_password);
if (mysqli_stmt_fetch($stmt)){
if (SHA1($password) == $hashed_password) {
// Password is correct
// Store data in session variables
$_SESSION["loggedin"] = true;
$_SESSION["username"] = $username;
// Redirect user to welcome page
header("location: index.php");
}
else{
// Display an error message if password is not valid
$password_err = "The password you entered was not valid.";
}
}
} else{
// Display an error message if username doesn't exist
$username_err = "No account found with that username.";
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
include('html/header.html');
// start the page, check for errors
echo '<div class="page" style="font-family:arial">';
formStart("login.php","","wrapper");
formFieldSetStart("");
echo '<h2 class="hdr-contactus">User Login</h2>';
//check for errors, if errors then display them
if (!empty($username_err) || !empty($password_err)) {
echo '<h1>Error(s):</h1>';
if (!empty($username_err)) {
echo $username_err.'<br>';
}
if (!empty($password_err)) {
echo $password_err.'<br>';
}
}
formDivStart("fields");
formParaStart("row");
fieldAdd("text","username","User Name","Y","field-large","");
formParaEnd();
formParaStart("row");
fieldAdd("password","password","Password","Y","field-large","");
formParaEnd();
formDivEnd();
formFieldSetEnd();
fieldAddSubmit("Login");
formEnd();
echo '</div>';
include('html/footer.html');
文件:Header.html
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $page_title; ?></title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0" />
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="js/jquery-3.4.0.js"></script>
</head>
<body>
<div class="headerimg">
<img src="images/test1.jpg" width="150" height="125">
</div>
<div class="header">
<h1>Title</h1>
<h2>Sub-Title</h2>
</div>
<?php
hmenustart();
hmenuitem("#","Home");
hmenudropstart("About");
hmenudropitem("aboutus.php","About Us");
hmenudropitem("#","Our History");
hmenudropend();
hmenudropstart("Board");
hmenudropitem("#","Board Members");
hmenudropitem("#","Committees");
hmenudropitem("#","Documents");
hmenudropitem("#","Message Baord");
hmenudropend();
hmenudropstart("Members");
hmenudropitem("#","Calendar");
hmenudropitem("#","FAQ");
hmenudropitem("#","Member Directory");
hmenudropitem("#","Member Documents");
hmenudropitem("requestmgr.php","Member Requests");
hmenudropitem("#","Questionaire");
hmenudropend();
hmenuitem("#","Photos");
hmenuitem("contactus.php","Contact Us");
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
hmenuitemfloatright("logout.php","Logout");
}
else {
hmenuitemfloatright("login.php","Login");
}
hmenuend();
?>
<!-- Start of Page specific content --!>
答案 0 :(得分:3)
您忘记在每个.php文件中使用session_start()
。
请注意,这应该是网页上的第一件事。在调用该函数之前输出HTML会导致错误。