Rob Heston Rob Heston - 1 year ago 67
PHP Question

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

The problem is being able to show the current user name while someone is logged in. I have been trying some of the solutions I have found online and cannot get them to work. I will paste in a few of the significant files. All web page files are PHP and they are supported by a Dreamweaver CC template file. I am trying to get the active user name to show up in the navbar by putting the code in the template file.

The error reported in the browser for http://localhost/WEB182/index.php is:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\WEB182\index.php on line 91

Line 91 of the index.php file is: echo "Signed in as: '. $_SESSION['user_name']"; }

Of course, that is not necessarily the actual problem, but a symptom reflected in the browser generated error message.

This is the Dreamweaver template file and where I am trying to impliment the visual username:

<!doctype html>
<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=",400,600,700,800,900" rel="stylesheet">
<!-- <link href="" rel="stylesheet"> -->
<!-- <script src=""></script> -->
<meta name="description" content="Rob's first database driven address book.">
<meta property="og:image" content=""/>
<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"/>
@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;}

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

<!-- TemplateEndEditable -->
<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;">
<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>
<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>
<a class="navbar-brand" href="index.php"><y6>HOME</y6></a>
<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>
<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 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>
<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 -->
</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 -->
</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=""></a></b1>
</div><!-- End Footer -->

<script src=""></script>
<script src="../js/bootstrap.min.js"></script>
<script src=""></script>
<!-- <script src=""></script> -->


This is the editable part of the index.php file. The rest is the same as the template file, above:

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

<div class="main-content" style="padding-left: 20px;"><!-- Begin main content div -->
<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>
<!-- End main content div -->
<!-- InstanceEndEditable -->

This is the editable part of the login.php file:

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

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

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

<!-- InstanceEndEditable -->

That is probably more than enough as the only issue is the code for the username in the navbar found in the template file above.

Answer Source

Your syntax error is right here:

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

Convenience hint: Syntax highlighting helps spot these things a lot - on StackOverflow, the string is red; Can you see how $_SESSION is red too? Compare to the corrected version:

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

You had a stray quote (") at the end and mixed " with ' too.

Whenever working with strings in PHP, you can use either " or ' but not both at the same time:

  • "Hello" (Ok)
  • 'Hello' (Ok)
  • 'Hello" (Nope!)

Note that there's also differences between the two; using 'single quotes' is slightly faster than "doubles", but PHP takes everything in single quotes literally. Compare "\n" to '\n'.

What is the error actually saying?

So, text inside "double quotes" isn't taken literally. This is why you can do things like "Hello $name". In short, PHP spotted $_SESSION in your string but failed to go any further when it hit the square bracket.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download