如何隐藏我的按钮" Inloggen"和" Registreren"我什么时候登录?我有两个单独的文件;我有我的索引文件与菜单等和php和表单的inloggen文件。如果我已登录,我希望隐藏按钮并将其替换为带注销的按钮。我如何用我的代码做到这一点?
指数:
<?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>
答案 0 :(得分:0)
也许尝试类似: 就像在您的登录页面中一样,检查登录会话是否已设置,如果已设置,则显示注销按钮。如果未设置登录会话,则显示其他2个按钮。
<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>
或者您可以使用Jquery检查会话是否已设置。首先给登录/注册按钮一个不同的类,然后是注销按钮。例如:class =“inloggen”用于登录/注册。和uitlog按钮的class =“logout”。如果已设置$('.inlogen').hide()
$('.uitloggen').show()
,反之亦然
答案 1 :(得分:0)
当我登录时,如何隐藏“Inloggen”和“Registreren”按钮?
您应该检查是否为用户设置了会话。即,如果会话isset
如果设置了会话,则应显示注销按钮
否则,如果未设置会话,则应显示注销和注册按钮。
以下是您应该做的事情
<?php
if(isset($_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
{
?>
<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>
<?php
}
?>
建议:
永远不要在所有文件中都有数据库连接,你可以只有一个名为config.php
的文件,你可以把它包含在需要的地方。
提示进行调试:
打印所有会话
<?php
session_start();
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
?>