ユーザーログインに基づいて別のヘッダーを表示しようとしています。ユーザーはlogin.phpを使用してログインできます。しかし、ユーザがログインしていないときやログインしていないときにheader.phpを表示することができません。ログインしているときにプロフィールフォームの内容を表示し、その逆も同様です。誰も助けることができますか?ログイン時にユーザーをリダイレクト
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
require("header.php");
?>
<div class = "container">
//form content
</div>
</body>
</html>
config.phpの
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "123";
$dbdatabase = "db";
$db = mysqli_connect($dbhost, $dbuser, $dbpassword) or die("couldn't connect to the Database");
mysqli_select_db($db, $dbdatabase) or die ("couldn't find db");
?>
header.phpの
<?php
if(!isset($_SESSION)){
session_start();
}
if (isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
session_unset();
session_regenerate_id();
}
include("config.php");
?>
!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
//if user is logged in, display navigation bar with user data
if (isset($_SESSION['SESS_LOGGEDIN']) == TRUE){ ?>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
//navigation - profile form content
</nav>
<?php}
//if user is not logged in, ask them to login
else { ?>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
//user log in here
//navigation - login form content
</nav>
<?php } ?>
</body>
</html>
login.php
<?php
session_start();
require_once("config.php");
if(isset($_SESSION['SESS_LOGGEDIN'])) {
header("Location: index.php");
die();
}
$useremail = $_POST['Lemail'];
$userpassword = $_POST['Lpass'];
if ($useremail&&$userpassword) {
$sql = mysqli_query($connect,"SELECT * FROM users WHERE userEmail='$useremail' and userpassword = $userpassword");
$numrows = mysqli_num_rows($sql);
if ($numrows!=0) {
while ($row = mysqli_fetch_assoc($sql)) {
$_SESSION['SESS_LOGGEDIN'] = 1;
$dbemail = $row['email'];
$dbpassword = $row['password'];
$dbname = $row['firs_Name'];
}
if ($useremail==$dbemail&&$userpassword==$dbpassword) {
header("Location: index.php");
$_SESSION['Lemail']= $useremail;
die();
}
else
header("Location: index.php");
die();
}
else
die("That user doesn't exist!");
}
else
die("please enter username and password");
?>
ユーザがログインしているときに 'session_unset()'を呼び出すのはなぜですか? – Barmar
'<!DOCTYPE html>'の先頭に '<'がありません。 – Barmar
'mysql_fetcxh_assoc'は' mysql_fetch_assoc'でなければなりません。さらに実行されないようにリダイレクトした後に 'die()'や 'exit()'を必ず含めてください。最後に、スクリプトを開始するときに 'session_start()'を一度呼び出すだけで、設定したい変数ごとに呼び出すことはありません。 – jpschroeder