如何在登录后才在主页上显示用户名?

时间:2016-11-01 07:40:51

标签: javascript php html

//的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 个答案:

答案 0 :(得分: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' ) ;
?>

让我知道这个脚本是否有用