NaQiudin Fadzil NaQiudin Fadzil -4 years ago 65
PHP Question

php log in with session display

Hi I am trying to make a website prototype that will show log in username , i have successfully displayed the user session, but when there is no session it will only display error.

<h1>Dear Precious Customer</h1>
<div>
<h4> Welcome <?php echo $_SESSION['username']; ?> </h4>
</div>
<div>
<a href="logout.php">Logout</a>
</div>


Here is my code for the display, as you can see it only show when session is active , if there is no session it will only show error . Instead of that error, how do I make it display something like "welcome log in or register" thanks.

Answer Source

Change this

<div><h4>Welcome <?php echo $_SESSION['username'];  ?></h4></div>

To

<?php
 $msg = 'Welcome! <a href="login.html">Login</a> | <a href="register.html">Register</a>';
 if ($_SESSION && isset($_SESSION['username'])) {
   $msg = 'Welcome! ' .$_SESSION['username'];
}
?>
<div><h4><?php echo $msg;?></h4></div>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download