通过 Dreamweaver 模板以 PHP 在整个网站的导航栏中显示活动用户名

Showing Active Username in Navbar Across the Website with PHP via A Dreamweaver Template

问题是当有人登录时能够显示当前用户名。我一直在尝试我在网上找到的一些解决方案,但无法使它们起作用。我将粘贴一些重要的文件。所有网页文件都是 PHP 并且它们由 Dreamweaver CC 模板文件支持。我试图通过将代码放入模板文件中来让活动用户名显示在导航栏中。

http://localhost/WEB182/index.php浏览器报错是: 解析错误:C:[=38 中的语法错误、意外的 '' (T_ENCAPSED_AND_WHITESPACE)、期望标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING) =].php 第 91 行

index.php 文件的第 91 行是:echo "Signed in as: '. $_SESSION['user_name']"; }

当然,这不一定是实际问题,而是浏览器生成的错误消息中反映的症状。

这是 Dreamweaver 模板文件,我试图在其中实现视觉用户名:

<!doctype html>
    <html>
<head>
        <meta charset="UTF-8">
        <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge"> -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="icon" href="../img/R-icon.png">
        <!-- TemplateBeginEditable name="EditRegion3" -->
        <title>Rob's Address Book</title>
        <!-- TemplateEndEditable --> 
        <link href="../css/bootstrap.min.css" rel="stylesheet">
        <link href="../css/addressbook.css" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Biryani:300,400,600,700,800,900" rel="stylesheet"> 
        <!-- <link href="https://fonts.googleapis.com/css?family=Modak" rel="stylesheet"> --> 
        <!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> --> 
<meta name="description" content="Rob's first database driven address book.">
<meta property="og:image" content="http://webtech-ftcc.org/users/hestonw0355/WEB182/img/c-01.png"/>
<meta property="og:description" content="This is where I am learning to input and retrieve data from a database with a website!"/>
<meta property="og:title" content="Rob's Address Book"/>
            <style>
            @font-face { font-family: Abbess; src: url('../fonts/Abbess.ttf'); } 
            f12 {font-family: 'Abbess', cursive;}
            @font-face { font-family: BenguiatStd-Book; src: url('../fonts/BenguiatStd-Book.ttf'); }
            b1 {font-family: 'BenguiatStd-Book', serif;}
            @font-face { font-family: BenguiatStd-BookItalic; src: url('../fonts/BenguiatStd-BookItalic.ttf'); }
            b2 {font-family: 'BenguiatStd-BookItalic', serif;}
            @font-face { font-family: BenguiatGothicStd-Bold; src: url('../fonts/BenguiatStd-Bold.ttf'); }
            b3 {font-family: 'BenguiatGothicStd-Bold', serif;}
            @font-face { font-family: BenguiatGothicStd-BoldObl; src: url('../fonts/BenguiatStd-BoldItalic.ttf'); }
            b4 {font-family: 'BenguiatGothicStd-BoldObl', serif;}

            y1 {font-family: 'Biryani', sans-serif; font-weight: 300;  font-style: light;}
            y2 {font-family: 'Biryani', sans-serif; font-weight: 400;  font-style: regular;}
            y3 {font-family: 'Biryani', sans-serif; font-weight: 600;  font-style: semi-bold;}
            y4 {font-family: 'Biryani', sans-serif; font-weight: 700;  font-style: bold;}
            y5 {font-family: 'Biryani', sans-serif; font-weight: 800;  font-style: extra-bold;}
            y6 {font-family: 'Biryani', sans-serif; font-weight: 900;  font-style: black;} 

            </style>
<!-- TemplateBeginEditable name="EditRegion1" -->  

<!-- TemplateEndEditable -->
</head>
<body style="background-color: #666666;">

       <div class="container"> 
            <div class="jumbotron" style="border-radius: 0px;">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" style="max-height: 200px; min-height: 150px; vert-align: middle;">
                    <tbody>
                        <tr style="">
                            <td width="20%">&nbsp;</td>
                            <td width="74%"><f12 style="font-size: 36pt; color: #f1c741; line-height: 90%;"> <br>Rob's Address Book</f12></td>
                            <td width="6%">&nbsp;</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
<div class="container">
                <nav class="navbar navbar-inverse">
                  <div class="container-fluid">
                    <div class="navbar-header">
                      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                      </button>
                      <a class="navbar-brand" href="index.php"><y6>HOME</y6></a> 
                    </div>
                    <div class="collapse navbar-collapse" id="myNavbar">
                      <ul class="nav navbar-nav">

                        <li class="dropdown">
                          <a class="dropdown-toggle" data-toggle="dropdown" href="#"><y4>Address Book</y4><span class="caret"></span></a>
                          <ul class="dropdown-menu">
                            <li><a href="../index-adbk.php"><y3>Enter Addresses</y3></a></li> 
                            <li><a href="view-adbk.php"><y3>View Address Book</y3></a></li>
                            <li><a href="#"><y3>Link 1-C</y3></a></li>
                            <li><a href="#"><y3>Link 1-D</y3></a></li>
                            <li><a href="#"><y3>Link 1-E</y3></a></li> 
                          </ul>
                        </li>
                        <li><a href="index-adbk.php"><span class="glyphicon glyphicon-list"></span><y4> Enter Addresses</y5></a></li>
                        <li><a href="view-adbk.php"><span class="glyphicon glyphicon-list-alt"></span><y4> View All Records</y5></a></li> 
                       </ul>
                    <ul class="nav navbar-nav navbar-right">
                        **<li><?php if(isset($_SESSION["usr_id"])){ 
                            echo "Signed in as: '. $_SESSION['user_name']"; } 
                            else { echo 'Not Logged In!'; } ?></li>**
                        <li><?php if(isset($_SESSION['usr_id'])){ 
                            <a href="logout.php"><span class="glyphicon glyphicon-log-out"></span><b3> Logout</b3></a></li>
                        <li><?php }else { ?>
                            <a href="login.php"><span class="glyphicon glyphicon-log-in"></span><b3> Login</b3></a></li>
                        <li><a href="register.php"><span class="glyphicon glyphicon-user"></span><b3> Register</b3></a> } ?></li>
                    </ul>
                    </div>
                  </div>
                </nav>
    <div class="row"><!-- Begin Row -->
            <div class="col-lg-7 col-md-9 col-sm-12 left-column "><!-- Begin main content (left) column -->

            <!-- TemplateBeginEditable name="EditRegion2" --> 

                <div class="main-content"><!-- Begin main content div -->
                    <br><br><br><br><br><br><br><br><br><br><br><br>
                </div><!-- End main content div -->

                <!-- TemplateEndEditable -->

            </div><!-- End main content column -->

            <div class="col-md-5 col-sm-9 right-column" style="height: cover;"><!-- Begin Right Column --> 
                    <br><br><br>
            </div><!-- End Right Column --> 
    </div><!-- End Row -->

</div><!-- End Container -->

<div class="container"> 
    <div class="footer"><!-- Begin Footer -->
            <b1 style="font-size: 12pt; color: white;">Copyright &copy; Sungraffix - All rights reserved - Published since 2016 - Winston-Salem, NC, USA - Contact: 
            <a href="mailto:rob@sungraffix.net">rob@sungraffix.net</a></b1>
    </div><!-- End Footer -->
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> -->                

    </body>
    </html>

这是 index.php 文件的可编辑部分。其余同模板文件,上图:

<!-- InstanceBeginEditable name="EditRegion2" --> 

<div class="main-content" style="padding-left: 20px;"><!-- Begin main content div -->
    <br><br>
    <p><b3 style="font-size: 16pt;">Welcome to the website for Robs Address Book!<br>
    If you would like to access the address book,<br>please login to begin.</b3></p>
    <p style="text-align: center;"><img src="img/c-01.png" width="227" height="350" alt=""/><br><br></p>
</div>
<!-- End main content div -->
<!-- InstanceEndEditable -->

这是 login.php 文件的可编辑部分:

        <!-- InstanceBeginEditable name="EditRegion2" --> 

        <div class="form-reg"><!-- Begin div to contain form -->
        <table width="50%" style="padding-left: 20px;">
            <tr>
                <td style="">
    <form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform">
            <fieldset>
                <legend class="legend-01"><b3>Please Login</b3></legend>
                </td>
            </tr>
            <tr>
                <td>
                <div style="margin-top: 0px; margin-bottom: 5px;">
                    <label for="name"><b1>Email</b1></label>
                    <input type="text" name="email" placeholder="Enter Your Email" required class="form-control" />
                </div>
                </td>
             </tr>
             <tr>
                <td>   
                <div style="margin-top: 10px; margin-bottom: 5px;">
                    <label for="name"><b1>Password</b1></label>
                    <input type="password" name="password" placeholder="Your Password" required class="form-control" />
                </div>
                </td>
             </tr>
                <td>
                <div style="margin-top: 10px; margin-bottom: 5px;">
                    <input type="submit" name="login" value="Login" class="btn btn-primary" />
                </div>
            </fieldset>
        </form>
                </td>
             </tr>
             <tr><td>
                <div style="margin-top: 10px; margin-bottom: 5px;">    
                    <b1>New User? <a href="register.php">Sign Up Here</a></b1>
                </div>
             </td></tr>
        </table>
        <span class="text-danger"><?php if (isset($errormsg)) { echo $errormsg; } ?></span>

        </div><!-- End div to contain form -->

            <!-- InstanceEndEditable -->

这可能绰绰有余,因为唯一的问题是上面模板文件中导航栏中的用户名代码。

你的语法错误就在这里:

    echo "Signed in as: '. $_SESSION['user_name']"; }

方便提示:语法高亮有助于发现这些东西 - 在 Whosebug 上,字符串是红色的;你能看到 $_SESSION 也是红色的吗?与修正后的版本比较:

    echo "Signed in as: ". $_SESSION['user_name'];
} // tidier down here

您在末尾有一个杂散的引号 ("),并且也将 " 与 ' 混在一起。

在 PHP 中使用字符串时,您可以使用 " 或 ',但不能同时使用:

  • "Hello"(好的)
  • 'Hello'(好的)
  • '你好"(不!)

请注意,两者之间也存在差异;使用 'single quotes' 比 "doubles" 稍快,但 PHP 将所有内容都用单引号括起来。比较“\n”和“\n”。

错误实际上在说什么?

因此,"double quotes" 中的文字并非字面意思。这就是为什么你可以做 "Hello $name" 这样的事情。简而言之,PHP 在您的字符串中发现了 $_SESSION,但在到达方括号时未能继续前进。