问题描述:

So I'm trying to do a login section in every single page of my web app

it looks like the attached pictures, but I'm having a hard time with the sessions.

if the user is logged in DON'T DISPLAY THE FORM > display dropdown list

but I always get the drop down without the registration,

I want a simple code that would do that for me coz I'm confused

is there a simple way to guide me through the process? Thanks

 <?php

/*

*Create db in mydb for users

*connect

*recieve from login forum >> check with db if exists

*start session > save the id of that user >> display the forum with details of his profile

*/

/*

if(isset($_POST["submit"])){

$user= $_POST["username"];

$pass= $_POST["password"];

//$_SESSION["username"]=$user;

echo "username is clicked";

}else{

echo "nothing is clicked";

}

*/

$signin=false; // checking the concept

$_SESSION["username"] =$signin;

if(isset($_SESSION["username"])){ #if the user is signed in

echo $_SESSION["username"];

$query= "select * from users";

$select= mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($select)){

$userid = $row["idusers"];

$username=$row["username"];

$password= $row["password"];

}

?>

<ul class="nav navbar-nav navbar-right">

<li>

<a href="help.html">Help</a>

</li>

<li class="dropdown">

<a href="#" class="dropdown-toggle" data-toggle="dropdown"><?php echo $username;?><strong class="caret"></strong></a>

<ul class="dropdown-menu">

<li>

<a href="profile.php?id=<?php echo "$userid";?>">Profile</a>

</li>

<li>

<a href="#">Contests</a>

</li>

<li class="divider">

</li>

<li>

<a href="signout.php">SignOut</a>

</li>

</ul>

</li>

</ul>

</div><?php

}else{

?>

<form class="navbar-form navbar-right" method="post" role="search">

<div class="form-group">

<input type="text" class="form-control" name="username" placeholder="Username">

</div>

<div class="form-group">

<input type="password" class="form-control" name="password" placeholder="Password">

</div>

<button type="submit" class="btn btn-default">Sign In</button>

</form>

<?php

}

?>

网友答案:

Presuming you set a variable when someone logs in and looking at the tags you've used I'm betting on $_SESSION

<?php
    if (!empty($_SESSION['user_logged_in'])) {
        echo " "; // HTML when the user is logged in
    } else {
        echo " "; // HTML when the user is logged out
    }
相关阅读:
Top