dongyanling9248 2016-06-06 09:32
浏览 165

登录时隐藏按钮

How do I hide my buttons "Inloggen" and "Registreren" when I am logged in? I have two seperate files; I have my index file with the menu etc and a inloggen file for the php and the form. If I am logged in I want the buttons to hide and replace it for a button with logout. How do I do that with my code?

Index:

<?php
require 'dbconnectie.php';
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>PC4U</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="index.css" rel="stylesheet">
</head>

<body>
<div id="container1">
<header>
    <div id="headerFotoDiv">
        <img src="images/logo.jpg" height="100px" class="lihover" onclick="window.location='http://www.pc4u.hexodo.nl/'">
    </div>
    <div id="headerLogindiv">
        <button class="btn btn-warning" onclick="window.location='?p=i'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 12px; text-align: center;">Inloggen</button>
        <button class="btn btn-warning" onclick="window.location='?p=re'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 15px; text-align: center;">Registreren</button>
    </div>

</header>
<!-- Static navbar -->
<nav class="navbar navbar-default" style="width: 1000px; margin-bottom:0px;padding-bottom:0px;padding-top:0px;border:0px;border-bottom:1px solid #000;border-radius:0px; background-color:#FFFFFF;";>
    <div class="container-fluid" style="padding:0px;">
        <div id="navbar" class="navbar-collapse collapse" aria-expanded="true" style="height: 1px;padding:0px;">
            <ul class="nav navbar-nav">
                <li onclick="document.location.href='http://www.pc4u.hexodo.nl/'" class="lihover"><a>Home</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Computers
                        <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="?p=dp">PC</a></li>
                        <li><a href="?p=lp">Laptop</a></li>
                    </ul>
                </li>
                <li><a href="?p=r">Reparatie</a></li>
                <li><a href="?p=c">Contact</a></li>
                <li><a href="?p=w">Winkelwagen</a></li>

            </ul>
        </div>
    </div>
</nav>
<!-- Vulling van de pagina -->
<div id="vulling">
<?php
if(isset($_GET['p'])) {
    $pagina = $_GET['p'];
    switch ($pagina) {
        case "c":
            include("contact.php");
            break;
        case "r":
            include("reparatie.php");
            break;
        case "re":
            include("registreren.php");
            break;
        case "i":
            include("inloggen.php");
            break;
        case "w":
            include("shoppingcart.php");
            break;
        case "ch":
            include("computerhome.php");
            break;
        case "lp":
            include("laptop.php");
            break;
        case "dp":
            include("desktop.php");
            break;
        default:
            include("home.php");
            break;
    }
}
else
{
    include("home.php");
}
?>
</div>
</div>
</body>
</html>

Inloggen:

<?php
ob_start();
session_start();
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

//var_dump($_SESSION);
if(!isset($_POST['username']))
$_POST['username'] = '';
if(!isset($_POST['password']))
$_POST['password'] = '';

//if (isset($_SESSION['ingelogd'])&&$_SESSION ['ingelogd'] == true ) header("location: http://www.pc4u.hexodo.nl");

$dbhost = "localhost";
$dbuser = "*****";
$dbpass = "*****";
$dbname = "*****";
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

if ($conn->connect_error) die("Connection failed");
if (isset($_POST['submit'])) {

$uname = $_POST['username'];
$wwoord = $_POST['password'];

$query = 'SELECT * FROM Klanten WHERE klant_username = "' . $uname . '" && klant_wachtwoord = "' . $wwoord . '"';

$result = $conn->query($query);
if ($result->num_rows == 1) {

    $_SESSION['ingelogd'] = true;
    header("location: index.php");
} else {
    $_SESSION['ingelogd'] = false;
    $msg = "Inloggegevens incorrect.";
}
//$conn->close();
}
?>
<link href="contact.css" rel="stylesheet">
<style type="text/css">
input, td, tr {
    padding-right: 20px;
}
</style>


<form class="form-horizontal" role="form" method="post">
<div class="form-group">
    <label class="control-label col-sm-2" style="text-align: left; width: 120px; margin-left: 10px; margin-top: 10px;" for="username">Username:</label>
    <div class="col-sm-10">
        <input type="text" class="form-control" id="username" name="username" style="width: 250px; margin-top: 10px;" required placeholder="">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-sm-2" for="password" style="text-align: left; width: 120px; margin-left: 10px; margin-top: 10px;">Password:</label>
    <div class="col-sm-10">
        <input type="password" class="form-control" id="password" name="password" style="width: 250px; margin-top: 10px;" required placeholder="">
    </div>
</div>
<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" style="margin-left: 120px; margin-bottom: 10px;" class="btn btn-default" name="submit">Inloggen</button>
    </div>
</div>
</form>
  • 写回答

2条回答 默认 最新

  • dongzg2006 2016-06-06 09:39
    关注

    Maybe try something like: Just like in your login page check if the login session is set, if it is set show the logout button. If the login session is not set, display the 2 other buttons.

    <div id="headerLogindiv">
    <?php
        if($_SESSION['ingelogd']){
    ?>        
        <button class="btn btn-warning" onclick="window.location=logout.php" style="width: 130px; height:30px; margin-left: 160px; margin-top: 12px; text-align: center;">Uitloggen</button>
    <?php
        } else {
    ?>
        <button class="btn btn-warning" onclick="window.location='?p=i'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 12px; text-align: center;">Inloggen</button>
        <button class="btn btn-warning" onclick="window.location='?p=re'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 15px; text-align: center;">Registreren</button>
    }
    </div>
    

    Or you could use Jquery to check if the session has been set. First give the login / registratie button a different class then the logout button. For example: class="inloggen" for the login / registration. and class="logout" for the uitlog button. If it has been set $('.inlogen').hide() $('.uitloggen').show() visa versa

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler