2016-11-01 147 views
-2

//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語句。

+1

您在'index.php'文件中缺少'session_start();'頂部。 – icecub

+0

將此放在索引文件的頂部'ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL);'。現在是否顯示任何錯誤? – Blinkydamo

+0

'echo'

回答

0

試試這個調試您的應用程序

的index.php

<?php 
//start header.php 
ob_start(); 
session_start(); 
$_SESSION['loggedin'] = (isset($_SESSION['loggedin'])) ? $_SESSION['loggedin'] : 0; 
if($_SESSION['loggedin']==1) 
    $_SESSION['displayname']="admin"; 
else 
    $_SESSION['displayname']="guest"; 
//end header.php 

if($_SESSION['loggedin']==1) 
{ 
    echo $_SESSION['displayname']; 
    echo "<a href='logout.php'>Logout</a>"; 
} 
else 
{ 
    echo $_SESSION['displayname']; 
    echo "<a href='login.php'>Login</a>"; 
} 
?> 

的login.php

<?php 
ob_start(); 
session_start(); 
$_SESSION['loggedin']=1; 
echo "try login...."; 
sleep(1); 
header('Location: index.php') ; 
?> 

logout.php

<?php 
ob_start(); 
session_start(); 
$_SESSION=NULL; 
session_destroy(); 
echo "try logout...."; 
sleep(1); 
header('Location: index.php') ; 
?> 

讓我知道這個腳本工作或不是

+0

使用此代碼,index.php中的$ _SESSION ['loggedin']變得未識別。 $ _SESSION ['loggedin']必須先初始化。當我們第一次訪問index.php時,$ _SESSION ['loggedin']未被初始化。 –

+0

你說得對。我會更新它 – plonknimbuzz

相關問題