登录时隐藏登录/注册按钮,注销时隐藏注销按钮 [英] Hide login/register button when logged in and hide logout button when logged out

查看:30
本文介绍了登录时隐藏登录/注册按钮,注销时隐藏注销按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PHP 新手,我遇到了一些问题.当我登录我的网站时,我想隐藏登录"和注册"按钮,当我退出时,我想隐藏注销"按钮.

这是我的 login.php 页面:

<?phpinclude_once "check.php";$error = "";//用于存储我们的错误的变量.if(isset($_POST["提交"])){if(empty($_POST["username"]) || empty($_POST["password"])){$error = "两个字段都是必填的.";}别的{//定义 $username 和 $password$用户名=$_POST['用户名'];$password=$_POST['密码'];//防止 MySQL 注入$username = stripslashes($username);$password = stripslashes($password);$username = mysqli_real_escape_string($db, $username);$password = mysqli_real_escape_string($db, $password);$password = md5($password);//从数据库中检查用户名和密码$sql="SELECT uid FROM users WHERE username='$username' and password='$password'";$result=mysqli_query($db,$sql);$row=mysqli_fetch_array($result,MYSQLI_ASSOC);//如果我们的数据库中存在用户名和密码,则创建一个会话.//否则回显错误.if(mysqli_num_rows($result) == 1){$_SESSION['username'] = $username;header("位置:index.php");}别的{$error = "用户名或密码不正确.";}}}?><头><link rel="stylesheet" type="text/css" href="assets/css/acc.css"><link rel="stylesheet" type="text/css" href="assets/css/acc2.css"><link rel="stylesheet" type="text/css" href="assets/css/navbar.css"><title>登录 |Peerbolte-ICT</title><部分><p style="color:red;"><?php echo $error;?></p><form method="POST" action=""><div class="accPanel"><h1 style="color: black; padding-bottom: 5%;">登录</h1><form method="POST" action=""><div class="登录输入"><label class="accLabel"><b>用户名<span style="color:red;">*</span></b></label><input type="text" placeholder="输入用户名" name="username" required><label class="accLabel"><b>密码<span style="color:red;">*</span></b></label><input type="password" placeholder="输入密码" name="password" required><button class="submitButton" type="submit" name="submit">登录</button>

</表单>

</节></html>

这是我的 navbar.php 按钮所在的位置:

    <li><a href="index.php">首页</a></li><li><a href="#bannerTwo">服务</a></li>
  • <a href="javascript:void(0)" class="dropbtn">我们的工作</a><div class="dropdown-content"><a href="#">链接 1</a><a href="#">链接 2</a><a href="#">链接 3</a>

  • <a href="javascript:void(0)" class="dropbtn">账户</a><div class="dropdown-content"><a href="login.php">登录</a><a href="register.php">注册</a><a href="logout.php">注销</a>
  • 谢谢

    解决方案

    您可以根据条件显示您的链接.检查会话,如果用户登录,您会根据该逻辑获得 session 值,您可以设置如下条件:

    <?php if( isset($_SESSION['username']) && !empty($_SESSION['username']) ){?><a href="logout.php">注销</a><?php }else{ ?><a href="login.php">登录</a><a href="register.php">注册</a><?php } ?>

    注意:让您在文件中开始会话.

    i'm kinda new in PHP and I have some issues. When I'm logged in on my website I want to hide the 'Login' and 'Register' button, and when I'm logged out I want to hide the 'logout' button.

    This is my login.php page:

    <?php
        include "navbar.php";
    ?>
    <?php
        include_once "check.php";
    
        $error = ""; //Variable for storing our errors.
        if(isset($_POST["submit"]))
        {
            if(empty($_POST["username"]) || empty($_POST["password"]))
            {
                $error = "Both fields are required.";
            }else
            {
                // Define $username and $password
                $username=$_POST['username'];
                $password=$_POST['password'];
    
                // To protect from MySQL injection
                $username = stripslashes($username);
                $password = stripslashes($password);
                $username = mysqli_real_escape_string($db, $username);
                $password = mysqli_real_escape_string($db, $password);
                $password = md5($password);
    
                //Check username and password from database
                $sql="SELECT uid FROM users WHERE username='$username' and password='$password'";
                $result=mysqli_query($db,$sql);
                $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
    
                //If username and password exist in our database then create a session.
                //Otherwise echo error.
    
                if(mysqli_num_rows($result) == 1)
                {
                    $_SESSION['username'] = $username;
                    header("location: index.php");
                }else
                {
                    $error = "Incorrect username or password.";
                }
    
            }
        }
    
    ?>
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="assets/css/acc.css">
            <link rel="stylesheet" type="text/css" href="assets/css/acc2.css">
            <link rel="stylesheet" type="text/css" href="assets/css/navbar.css">
            <title>Login | Peerbolte-ICT</title>
        </head>
        </body>
            <section>
            <p style="color:red;"><?php echo $error; ?> </p>
                <form method="POST" action="">
                    <div class="accPanel">
                    <h1 style="color: black; padding-bottom: 5%;">Login</h1>
                    <form method="POST" action="">
                        <div class="login-input">
                            <label class="accLabel"><b>Username<span style="color:red;">*</span></b></label>
                            <input type="text" placeholder="Enter Username" name="username" required>
    
                            <label class="accLabel"><b>Password<span style="color:red;">*</span></b></label>
                            <input type="password" placeholder="Enter Password" name="password" required>
    
                            <button class="submitButton" type="submit" name="submit">Login</button>
                        </div>
                    </form>
                </div>
            </section>
        </body>
    </html>
    

    And this is my navbar.php where the buttons are:

    <ul>
      <li><a href="index.php">Home</a></li>
      <li><a href="#bannerTwo">Services</a></li>
      <li class="dropdown">
        <a href="javascript:void(0)" class="dropbtn">Our Work</a>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </li>
      <li class="dropdown">
        <a href="javascript:void(0)" class="dropbtn">Account</a>
        <div class="dropdown-content">
          <a href="login.php">Login</a>
          <a href="register.php">Register</a>
          <a href="logout.php">Logout</a>
        </div>
      </li>
    </ul>
    

    Thanks

    解决方案

    you can display your link based on condition. check session for that if user login you get session value on base of that logic you can put condition like below:

    <?php if( isset($_SESSION['username']) && !empty($_SESSION['username']) )
    {
    ?>
          <a href="logout.php">Logout</a>
    <?php }else{ ?>
         <a href="login.php">Login</a>
         <a href="register.php">Register</a>
    <?php } ?>
    

    Note: make your you start session in file.

    这篇关于登录时隐藏登录/注册按钮,注销时隐藏注销按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    PHP最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆