Jump to content

How to hide buttons after user logged in


edgarasm

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

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.

 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.