//header.php如何僅在登錄後在主頁上顯示用戶名?
<?php
session_start();
if(isset($_SESSION['loggedin']))
$_SESSION['displayname']="notset";
else
$_SESSION['loggedin']=0;
?>
//index.php
<?php
require "header.php";
if($_SESSION['loggedin']==1) //ignores the if statement
{
echo "Welcome".$_SESSION['displayname']."!";
echo "<button id='logout_button'>
<a href='logout.php'>Logout</a>
}
else
{
echo '<a id="display_name" href="login.html">Login/ Signup</a>';
}
?>
//verify.php
<?php
session_start();
include "connect.php";
$uname=$_POST['uname'];
$pass=$_POST['pass'];
$check=mysqli_query($conn,"SELECT * FROM member_list WHERE username='$uname'");
$numrows=mysqli_num_rows($check);
if ($numrows == 1)
{
$row = mysqli_fetch_array($check);
$dbusername = $row['username'];
$dbpassword = $row['password'];
if($uname == $dbusername && $pass == $dbpassword)
{
$_SESSION['loggedin']=1;
$_SESSION['displayname'] = $uname;
header("location:index.php");
}
else
{
$msg="Invalid Password !!";
echo "<script type='text/javascript'>alert('$msg');
window.location.replace('loggedin.html')</script>";
}
}
else
{
$msg="Invalid Username !!";
echo "<script type='text/javascript'>alert('$msg');
window.location.replace('index.html')</script>";
}
?>
這是我的代碼。沒有錯誤。但是當我登錄時它不顯示用戶名。它忽略index.php上的if語句並直接轉到else語句。
您在'index.php'文件中缺少'session_start();'頂部。 – icecub
將此放在索引文件的頂部'ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL);'。現在是否顯示任何錯誤? – Blinkydamo
'echo'