在每个页面中内联登录表单php

时间:2015-03-08 20:23:44

标签: php session login

enter image description here

enter image description here

因此,我尝试在我的网络应用的每个页面中执行登录部分 它看起来像附图,但我在会话上遇到了困难。

如果用户已登录,则“显示表格”>显示下拉列表 但我总是在没有注册的情况下得到下拉,

我想要一个简单的代码,这样做对我来说因为我很困惑 有没有一种简单的方法来指导我完成整个过程?谢谢

        <?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 

                }

                ?>

1 个答案:

答案 0 :(得分:0)

假设您在有人登录并查看您使用的标记时设置了一个变量我在$_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
    }