简体   繁体   中英

Using sessions to display a users name on website

Below is the code for the home page (index.php), at the top of the home page, I want the user to be able to login, I have currently got the form set up but I am wondering how I will display this on other pages.

在此处输入图片说明

Here is the code:

<?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>

Below is an example of another page (loggedin.php) on the website, it is still the homepage but I don't want the form to be displayed, instead I want the users name. Here is the code:

<?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>

You can use a conditional to check is the session username is set, and if it is, then you hide the login form. Something like:

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

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

<?php else: ?>

    <form>
        ...
    </form>

<?php endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM