Change Login button to logout

Asked

Viewed 478 times

0

First of all I want to warn you that I am a beginner in PHP and I appreciate help because I need to deliver a work and I am in Deadline

I have a login button and I want you to switch to logout when the session is started but the condition works for multiple pages!

This is the code of my login.php page to which the login button sends the user when it is clicked!:

        <div class="inner_container">
            <label><b>Username</b></label>
            <input type="text" placeholder="Enter Username" name="username" required>
            <label><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="password" required>
            <button class="login_button" name="login" type="submit">Login</button>
                            <a href="registo.php"><button type="button" class="register_btn">Criar conta</button></a>
        </div>
    </form>

    <?php
        if(isset($_POST['login']))
        {
            @$username=$_POST['username'];
            @$password=$_POST['password'];
            $query = "select * from user where username='$username' and password='$password' ";

            $query_run = mysqli_query($con,$query);

            if($query_run)
            {
                if(mysqli_num_rows($query_run)>0)
                {
                $row = mysqli_fetch_array($query_run,MYSQLI_ASSOC);

                $_SESSION['username'] = $username;
                $_SESSION['password'] = $password;
                header( "Location: index.php");
                }
                else
                {
                    echo '<script type="text/javascript">alert("Nome de utilizador ou palavra passe inválida")</script>';
                }
            }
            else
            {
                echo '<script type="text/javascript">alert("Database Error")</script>';
            }
        }
        else
        {
        }
    ?>

</div>

  • You’ve done the routine to delete the session?

  • How so?????

  • When the person clicks on to drop, you will delete the session, right?

  • I didn’t try to do that! But I will have to make a condition to echo or not?

  • You want to put a logout button on all pages, including the login?

  • I have other pages besides index.php which is the page to which the user will log in correctly! I just need the login button that changes to logout when the user logs in correctly! And when you have the logout button and it is pressed that change to login I tried to do several conditions and I am not getting there...

  • From what I understand, you can put the logout button on the pages by checking Session with IF, and on the login page redirect to the index if SESSION is full

  • I’ve seen people do it but make a page where they have the login button and the logout button and when you log in correctly send to the page that have the logout button and when that logout sends to the page that have login but I have several pages I will not duplicate my pages to do this there must be a better way?

  • No need to duplicate anything. Using IF you can check if SESSION is full and show a button or other.

  • I’m tempted what Leonardo Duarte is trying to help me!

Show 5 more comments

1 answer

1


Put this on the pages that would like to appear the buttons, exchange the link for a button.

if(isset($_SESSION['username']))
{
   echo '<a href="index.php?logout=1">LOGOUT</a>';
}
else
{
   echo '<a href="login.php">LOGIN</a>';
}

On the index.php page put before any code

$logout = isset($_GET['logout']) ? $_GET['logout'] : "";

if ($logout == 1)
{
unset($_SESSION['username']);
}

On restricted pages, you can enter this code below to prevent access if the session variable is null.

if(!isset($_SESSION['username']))
{
    echo '
        <div class="erro">
        Não tem permissão para ver o conteúdo desta página.<br>
        <a href="index.php">Voltar</a>
        </div>';
        exit;
}
  • Yes, but then the logout button wanted to send me to the index.php page and switch to login since the user wanted to login! Thanks for the help! That’s all I need

  • So in the logout button you directed to index.php? logout=1 and do a conditional check on index - if ($_GET['logout'] == 1) unset($_SESSION['username']) so delete the session variable.

  • but they are not different buttons are echo is just a button

  • 1

    friend, see the changed code

  • Thank you!!!!!! You saved me

  • Thank you so much!!! You helped me so much!! If you can give me an explanation I think I figured out what you did but I don’t think I’d get there so fast because my knowledge doesn’t allow it yet!!

  • Right! The session variables when initialized, they have the durability until the closing of the browser, so the active user will have this variable even changing page, it will not lose the value. In the second chunk of unset code that is like expiring this variable, it loses the value. I’m inserting another code snippet, where it checks if the variable is active, otherwise it does not prevent access to the page content.

  • But this new code does not let the user access the page without login done is this?

  • Yes friend, in this last code, no login made the user can not enter the page. Receives the warning that has no permission and ends there.

Show 4 more comments

Browser other questions tagged

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