没有文本框的同一页面上的“欢迎用户”

时间:2017-04-12 11:08:36

标签: php

我是非常新的PHP,我正在尝试在右侧栏创建一个登录div。好吧,它似乎工作,但当我登录时,它显示我:

user name [     textbox         ]
password  [     textbox         ]
          [     login button    ]


welcome new2

显然,我对显示文本框和登录按钮并不感兴趣,因为用户已经登录了。

这是主页的代码(templat.php):

<?php
    session_start();
    $db=mysqli_connect("localhost","root","","mydb");
?>

<!DOCTYPE html>
<html lang="he">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><?php echo $title; ?></title>
        <link rel="stylesheet" type="text/css" href="Styles/Stylesheet2.css" />
    </head>
    <body dir="rtl">
        <div id="wrapper">
            <div id="banner">             
            </div>

            <nav id="navigation" dir="rtl">
                <ul id="nav">
                    <li><a href="index.php">home</a></li>
                    <li><a href="indexForum.php">topics</a></li>
                    <li><a href="#">on us</a></li>
                </ul>
            </nav>

            <!--
            <div id="content_area">
                <?php
                //echo $content; ?>

            </div>
            -->
            <div id="sidebar">

    <div id="main-wrapper">
    <center><h2>Login Form</h2></center>
            <div class="imgcontainer">
                            <center>
                <img src="images/avatar.png" width='60' height='60' alt="Avatar" class="avatar">
                            </center>
            </div>
        <form action="Template.php" method="post">
            <div class="inner_container">
                <label><b>Username</b></label>
                                <br/>
                <input type="text" placeholder="Enter Username" name="username" required>
                                <br/>
                <label><b>Password</b></label>
                                <br/>
                <input type="password" placeholder="Enter Password" name="password" required>
                                <br/>
                <button class="login_button" name="login" 
           type="submit">Login</button>
            </div>
        </form>
        <?php
             if(isset($_POST['login']))
            {


                $username = mysqli_real_escape_string($db, $_POST['username']);
                $password = mysqli_real_escape_string($db, $_POST['password']);
                $password=md5($password); //Remember we hashed password before storing last time


                $sql="SELECT * FROM users WHERE username='$username' AND password='$password'";

                $result=mysqli_query($db,$sql);  
                $row = mysqli_fetch_array($result);
                $num = mysqli_num_rows($result);
                if($num==1)
                {
                    $_SESSION['message']="You are now Loggged In";
                    $_SESSION['username']=$username;
                    $id=$row['id'];
                    $_SESSION['id']=$id;
                 ?>   
                  <div id="main-wrapper">
        <center><h3>Welcome <?php echo $_SESSION['username']; ?></h3></center>

        <form action="Template.php" method="post">
            <div class="imgcontainer">
                <img src="images/avatar.png" alt="Avatar"  width='60' height='60' class="avatar">
            </div>
            <div class="inner_container">
                <button class="logout_button" type="submit">Log Out</button>    
            </div>
        </form>
    </div>



                 <?php   
                }
               else
               {
                  $_SESSION['message']="Username and Password combiation incorrect";
                }
            }
            ?>

                </br>
            </div>

            </div>
            <footer>
                <p>aaa</p>
            </footer>
        </div>
    </body>
</html>

更新:logout.php:

<?php
session_start();
session_destroy();
unset($_SESSION['username']);
$_SESSION['message']="You are now logged out";
header("Location:login.php");
?>

1 个答案:

答案 0 :(得分:0)

您需要检查用户是否已登录。您可以检查$_SESSION['username']是否为空。

更新您的登录表单:

<?php if(!isset($_SESSION['username'])) { ?>

<form action="Template.php" method="post">
        <div class="inner_container">
            <label><b>Username</b></label>
            <br/>
            <input type="text" placeholder="Enter Username" name="username" required>
            <br/>
            <label><b>Password</b></label>
            <br/>
            <input type="password" placeholder="Enter Password" name="password" required>
            <br/>
            <button class="login_button" name="login"
                    type="submit">Login
            </button>
        </div>
</form>

<?php }; ?>