HTML & PHP -Make a log in button log out

Asked

Viewed 845 times

0

I have my PHP code created for both log in and log out, I confess that I have read several articles on google how to turn a log in button into log out but I confess that the read did not help at all, I am here to ask if someone can enlighten me how to do. My index page (VIEW PHOTO) has a button called Member area that I would like to become a button called log out when the user logs in. MEMBER AREA Updated code:

[![<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">    
  <link rel="stylesheet" type="text/css" href="index.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body>

    <nav class="navigation">
  <ul class="mainmenu
             ">
      <li><a href="index.html"><i class="material-icons">home</i></a></li>
    <li><a href="">Books Categories</a>
      <ul class="submenu">
        <li><a href="bookcomputing.php">Computing</a></li>
        <li><a href="bookromance.php">Romance</a></li>
        <li><a href="bookfiction.php">Fiction</a></li>
         <li><a href="booknonfiction.php">Non-Fiction</a></li>  
      </ul>
    </li>
    <li><a href="contact.html">Contact us</a></li>
  </ul>
</nav>
    <div id="memberarea">
      <?php

if (!isset($_SESSION\['email'\])) {
    echo "<a href='memberarea.html'>Member area</a>";
} else {
    echo "<a href='logout.php'>Log out</a>"; 
} 

?>
      <a href="adminarea.php"><span class="glyphicon glyphicon-lock"></span> Admin area</a>
</div>
    <form action="search.php" method="GET" id="search">
        <input type="text" name="query" placeholder="author's name, title">
        <input type="submit" value="Search">
    </form>
<p> Welcome to Galaxy Book Store website, this website was created for people who really enjoy reading all sort of books.</p> 
<p>Here you can find a huge amount of books.</p>
</body>
</html>
<footer>
    <p> &#174; Galaxy Books Store</p>

</footer>][2]][2]

2 answers

3


Exchange the "Member Area" for something like:

<?php

if (isset($_SESSION['email'])) {
    echo "<a href='logout.php'>Log out</a>";
} else {
    echo "<a href='login.php'>Member Area</a>"; 
} 

?>

Functioning

If the session email exist, it will display "Log Out". If it doesn’t exist, it will display "Member Area".

  • Anderson I solution that Voce presents, Oce suggests that I put this information in login.php or in memberarea.html (I just edited my comment to put the password memberarea.html.)

  • The solution is not mine, I just improved the formatting. But this code should go on the page where you display the link Member Area that you wish me to change to Logout.

  • @Dianamadeira in either. You will put this code in your menu.

  • what menu VME? sorry but I’m not getting it

  • Instead of this "Member Area" button put the code I said

  • <a href="memberarea.html"><span class="glyphicon glyphicon-user"></span> Member area </a> <br> this is the button that I have at the moment, already replaces and did not work very well

  • Report what has happened

  • updated the code where I put the code Voce suggested and added a photo where it shows the result.

  • You changed all the code I sent. I ask you to put it exactly as I told you. At the beginning of the page, add a <?php session_start(); ?>

Show 4 more comments

1

Try using the PHP session scheme.

When the user is logged in, you should create a session more or less like this:

<?php
    session_start();
    $_SESSION['login'] = 'login do usuário'
?>

This will let the browser know that the user has logged in.

After that, you should put a session check on the location where you want to change the button:

<div id="memberarea">
    <?php
        if(isset($_SESSION['login'])) {
            echo "<a href='logout.php'>Log out</a>";
        }else {
            echo "<a href='login.php'>Login</a>"; 
        }
    ?>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.