Jump to content

How to hide buttons after user logged in


edgarasm
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi there

 

I am looking on how to hide buttons once the session has been initiated so the  user doesnt see the buttons if he is logged in until he logs out 

 

I have a div with both buttons 

 

not sure whereever i would need to hide the div 

<div class="one_sixth" style="padding-top: 0px;">
					<td><a href="../account/loginprocess.php"<input type="submit" id="login_submit" name="login_submit" style="background: light_blue;" class="button" value="Login to Account">Login</a></td>
				<td><a href="/account/register.php" id="login_submit" name="login_submit" input type="submit"   style="background: light_blue;" class="button" value="Register">Sign Up</a></td>
	</div>

Thanks in advance

Link to comment
Share on other sites

  • Solution

When the user is loggged in set a session varibal such as

 

$_SESSION['logged_in'] = true;

 

You can use a simple if statement to display the logout link if they are logged in. Otherwise display the login link

if(isset($_SESSION['logged_in')  && $_SESSION['logged_in'] == true) {
    // display logout link
} else {
    // display login link
}

Also the HTML code you have posted for the login and register links/buttons is invalid html. You appear to of merged a link and a button together.

Edited by Ch0cu3r
Link to comment
Share on other sites

 

also i couldnt get my buttons to work otherwise

That is because your HTML for those buttons is invalid. You have merged the anchor (<a> ) tag and button (<input>) tag together you cannot do that.

 

You are better of using anchors tags and then use CSS to style them to look like buttons.

 

 

 

Hi i dont really get how to use that :/where would i have to include that?

You place that code where you want the login/logout buttons to be displayed.

Link to comment
Share on other sites

Use this and edit yourself.

 

----

                   <?php
        if (isset($_SESSION['user_id'])) {
            if ($_SESSION['role'] == 'ban')
{
    echo '<meta http-equiv="refresh" content="0; url=ban.php " />';
    die();
}
            elseif ($_SESSION['role'] == 'member'){
           ?>
            
            <nav>
           <a href="home.php">Home | </a>
           <a href ="accountPanel.php">Account Panel | </a>
           <a href="logout.php">Logout</a><br />
            </nav>
            </header>
        
            <?php 
            $link = mysqli_connect($HOST, $USERNAME, $PASSWORD, $DB);
 
        //match the username and password entered with database record
        $query = ("SELECT id,role,username FROM `users` WHERE id='".$_SESSION['user_id']."'");
        $result = mysqli_query($link, $query) or die(mysqli_error($link));
            $row = mysqli_fetch_array($result);
            $_SESSION['user_id']= $row['id'];
            $_SESSION['username'] = $row['username'];
            $_SESSION['role'] = $row['role'];
            
 
            echo '<p><i>Welcome, '. $_SESSION['username'];?>
        
        <br>
               <?php echo 'You are logged in as ' . '(' . $_SESSION['role'] . ')' ?>
            <br>
            
            
            <?php
                   } else{ ?>
         
            <nav> <a href="home.php">Home | </a>
           <a href ="adminPanel.php">Admin Panel | </a>
           <a href="logout.php">Logout</a><br />
            </nav>
            </header>
 
 
        <?php }} else {
            ?>
            <nav>
           <a href="home.php">Home</a>
           <a href ="register.php">Register</a>
           <a href="login.php">Login</a><br />
           </nav>
            </header>
             
            <?php
        }
        ?>

 

--

note, the last part ^ is the condition for user it is not login.

Edited by KaiSheng
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.