简体   繁体   English

使用会话在每个页面上显示用户名

[英]Displaying users name on every page using sessions

I am currently creating a website that will allow the user to log in using a username only, no password is required. 我目前正在创建一个允许用户仅使用用户名登录的网站,不需要密码。 Once the user has typed their name into the form, their name should then be placed on all of the pages they then visit until they log out. 一旦用户在表单中键入了他们的名字,他们的名字就应该放在他们随后访问的所有页面上,直到他们退出为止。

The problem/s I am facing is that the username is not showing on the other pages once logged in. Instead I have been getting problems such as errors ( Notice: Undefined index: username in /ceri/homes1/s/sec17/public_html/cs25010/home.php on line 41 ) and the nothing showing up at all. 我面临的问题是登录后其他页面上没有显示用户名。相反,我遇到了诸如错误之类的问题( 注意:未定义索引:/ ceri / homes1 / s / sec17 / public_html /中的用户名第41行的cs25010 / home.php )并没有显示任何内容。

Here is the code for the login page: 以下是登录页面的代码:

<?php
session_save_path("/aber/sec17/public_html/cs25010/tmp");
session_start();
if (empty($_SESSION['username'])) {
if (isset($_POST['submit'])) {
    $_SESSION["username"] = $_POST["username"];
    header("Location: home.php");
}
}
?>

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

        <div id="nav">
            <div id="menu">
                <ul>
                <li><a href="home.php">Home</a></li>
                <li><a href="database.php">Products</a></li>
                <li><a href="drink.php.html">Offers</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</a></li>
                </ul>
            </div>
        </div>



        <div id="main">
            <h1>Welcome to Sean Coyne's Food Shop</h1>
            <h2>Please Log In below:</h2>
            <br></br>
            <div id="login">
                <?php
                    echo '<form action="home.php" method"post"> 
                <input type="text" name="username" text="input username"
                placeholder="Username" required> 
                <input type="submit" name="submit" value="submit" /> 
                </form>';
                ?>
            </div>

        </div>

    </div>
</body>
</html>

And here the code for the home page: ( I will not be placing the username here when its finished, this is just while im testing it to see if its working ) 这里是主页的代码:( 我完成时不会在这里放置用户名,这只是在我测试它以查看它是否正常工作时

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Home Page</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>

        <div id="nav">
            <div id="menu">
                <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="database.php">Products</a></li>
                <li><a href="drink.php.html">Offers</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</a></li>
                </ul>
            </div>
        </div>



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

                <?php
                    echo $_SESSION['username'];
                ?>

        </div>
    </div>  
</body>
</html>

Heres what it currently look like when you log in: 下面是您登录时的状态:

主页的屏幕截图

You have a typo. 你有一个错字。 Otherwise default method for form method is get. 否则表单方法的默认方法是get。

Change: 更改:

<form action="home.php" method"post">

To this: 对此:

<form action="home.php" method="post">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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