php session logout/login button

Security issues related to php and mysql usage. How to make your code secure? Security measures and configurations? It's all in here!

Moderators: egami, macek, gesf

Post Reply
juulphp
New php-forum User
New php-forum User
Posts: 18
Joined: Tue Jan 24, 2017 5:41 am

Wed Apr 12, 2017 5:57 am

want to see if a user is not logged in can not see certain things on the website. If the user is logged the login button changes to logout. Or if a user is not logged in, can not see anything on the page, and if he is logged. I tried to session_start above but the entire page is protected and I do not. I also tried this:

Code: Select all

<? Php if (isset ($ _ SESSION [ 'username'])):?>
<a href="secret/logout.php" class="btn btn-info btn-sm" role="button"> <? php echo $ _SESSION [ 'username']?; ?> </a>
<? Php else:?>
<? Php endif?; ?> 
But that does not work. Is there anyone who can help me?

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 304
Joined: Sat Mar 23, 2013 8:24 am

Wed Apr 12, 2017 7:14 am

Here's how I do it:

Code: Select all

 <?php
if (isset($_SESSION['user']->id) && ( $_SESSION['user']->security === 'member' || $_SESSION['user']->security === 'admin' )) {
  echo '<li><a href="addTrivia.php">Add Trivia</a></li>';
  echo '<li><a href="logout.php">Logout</a></li>';
} else {
  echo '<li><a href="login.php">Login</a></li>';
  echo '<li><a href="register.php">Register</a></li>';
}
?>
When I'm force to intermingle HTML and PHP, I try to do it all in PHP as much as possible. That way it's easier to see what is going on and easier to debug.

HTH John

P.S. I know this hasn't anything to do with this thread, but this is how I had logging out the user.

Code: Select all

    unset($_SESSION['user']);
    session_destroy();
    $_SESSION['user'] = NULL;
    header("Location: index.php");
    exit();

Post Reply