如果未设置会话,则打印默认消息

时间:2019-02-10 06:30:34

标签: php session

我有一个书店网站,该网站目前具有登录功能,该功能可以在成功登录后设置会话。我试图在我的index.php文件上实现一个标头,如果用户未登录,该标头会显示“ Guest”,并在成功登录后用其名称替换“ Guest”。目前,当用户登录时,我将我的网页打印为“欢迎使用书店,(名称)”。但是,如果用户未登录且未设置任何会话,则仅打印“欢迎使用书店”。这是我的代码:

<?php
    session_start();
    include "connection.php";

    $greeting = "";

    if(!isset($_SESSION)) {
        $greeting = "Guest";
    }
    if(isset($_SESSION)) {
        $greeting = $_SESSION['name'];
    }



 ?>



<!DOCTYPE html>
 <html>
    <head>
        <title>UND Bookstore</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
        <meta name="author" content="Sai Peri"></meta>
        <meta name="description" content="CSCI 457 Assignment 1"></meta>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        <!-- Latest jQuery libraries -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <nav id="nav" class="navbar navbar-inverse text-center">
                        <ul class="nav navbar-nav">
                            <li class="active"><a href="index.php">Home</a></li>
                            <li><a href="login.php">Login</a></li>
                            <li><a href="search.php">Search</a></li>
                            <li><a href="check.php">Add/Delete/Edit</a></li>
                        </ul>
                    </nav>
                </div> <!-- End col-md-12 -->
            </div>  <!-- End row (nav)-->
            <div class="row" id="body">
                <div class="col-md-12">
                    <div class="page-header">
                        <h3 class="text-center">Welcome to the Bookstore, <?php echo $greeting; ?></h3>
                    </div> <!-- End of page-header -->
                    <p class="text-center">In order to get the full functionalty of the website you need to login. If you do not have an account you can register on the same "Login" webpage. You do not need to be logged in to use the search functionality, however. It should be noted that only the administrator can access the "Add/Delete/Edit" page.</p>
                </div> <!-- End col-md-12 -->
            </div> <!-- End row (nav)-->
            <div class="row" id="footer">
                <div class="col-md-12 text-center">
                    <footer>Created 2019 by Sai Peri</footer>
                </div> <!-- End col-md-12 -->
            </div> <!-- End row (nav)-->
        </div> <!-- End container -->
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

除了我的评论外,检查用户是否未登录甚至还有什么意义?为什么不只是假设是这种情况,并在找到问候语时将用户名添加到问候语中。

类似的东西:

<?php
    session_start();
    include "connection.php";

    $greeting = "Guest";

    if(isset($_SESSION['name'])) {
        $greeting = htmlspecialchars($_SESSION['name']);
        // added special chars to avoid xss attacks
    }



 ?>