使用会话在网站上显示用户名

时间:2015-10-20 15:27:31

标签: php html session

下面是主页的代码(index.php),在主页的顶部,我希望用户能够登录,我目前已经设置了表单,但我想知道我将如何在其他页面上显示。

enter image description here

以下是代码:

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Sean Coyne's Food Shop</title>
    <link href="style.css" type="text/css" rel="stylesheet"/>
    <link rel="icon" type="image/x-icon" href="images/favicon.ico" />
    <meta name="description" content="Welcome to Sean Coyne's Food Shop" />
</head>

<body>
    <div id="page">
        <div id="logo">
            <img src="images/logo.jpg" alt="Sean Coyne's Food Shop" 
            title="Sean Coyne's Food Shop" width="400px" height="70px"/>

                   <div id="login">
                        <form action="loggedin.php"     method="post">
            <label>UserName :</label>
            <input id="name" name="username" placeholder="username" type="text">
            <label>Password :</label>
            <input id="password" name="password" placeholder="**********" type="password">
            <input name="submit" type="submit" value=" Login ">
            </form>
        </div>

        </div>

        <div id="nav">
            <div id="menu">
                <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="food.php">Food</a></li>
                <li><a href="drink.php.html">Drink</a></li>
                <li><a href="about.php">About Us</a></li>
                <li><a href="findus.php">Where to find us</a></li>
                <li><a href="contact.php">Contact Us</a></li>
                </ul>
            </div>
        </div>



        <div id="main">
            <h1>Welcome to Sean Coyne's Food Shop</h1>
        </div>
    </div>
</body>
</html>

下面是网站上另一个页面(loggedin.php)的示例,它仍然是主页,但我不希望显示该表单,而是我想要用户名。这是代码:

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Sean Coyne's Food Shop</title>
    <link href="style.css" type="text/css" rel="stylesheet"/>
    <link rel="icon" type="image/x-icon" href="images/favicon.ico" />
    <meta name="description" content="Welcome to Sean Coyne's Food Shop" />
</head>

<body>
    <div id="page">
        <div id="logo">
            <img src="images/logo.jpg" alt="Sean Coyne's Food Shop" 
            title="Sean Coyne's Food Shop" width="400px" height="70px"/>

        <div id="login">
            <?php
                echo $_SESSION['username'];
            ?>
        </div>

        </div>

        <div id="nav">
            <div id="menu">
                <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="food.php">Food</a></li>
                <li><a href="drink.php.html">Drink</a></li>
                <li><a href="about.php">About Us</a></li>
                <li><a href="findus.php">Where to find us</a></li>
                <li><a href="contact.php">Contact Us</a></li>
                </ul>
            </div>
        </div>



        <div id="main">
            <h1>Welcome to Sean Coyne's Food Shop</h1>
        </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您可以使用条件来检查是否设置了会话用户名,如果是,则隐藏登录表单。类似的东西:

<?php if (isset($_SESSION['username']): ?>

    <h1>You are logged in!</h1>

<?php else: ?>

    <form>
        ...
    </form>

<?php endif; ?>