在PHP中显示用户和注销

时间:2016-02-22 17:47:14

标签: php mysql mysqli

如何让html页面显示登录的用户?所以我的意思是如何显示用于登录网站的用户名?除此之外,我想知道如何从我的网页注销?

<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "login");


if (isset($_POST['inloggen'])) {
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    $rpassword = mysqli_real_escape_string($conn, $_POST['rpassword']);

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

    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) == 1) {
        header('location: loggedin.php');
    } else {
        header('location: error.php');
    }

    echo "<br />";
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="css/stylesheet.css" type="text/css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="js/java.js"></script>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SBRM National Bank</title>
  </head>
  <body>
    <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="index.php">SBRM National Bank</a>
      </div>
      <div class="collapse navbar-collapse" id="myNavbar">
        <ul class="nav navbar-nav">
          <li><a href="index.php">Home</a></li>
          <li><a href="#">Particulier</a></li>
          <li><a href="#">Persoonlijk</a></li>
          <li><a href="#">Privé</a></li>
          <li><a href="#">Zakelijk</a></li>

        </ul>
        <ul class="nav navbar-nav navbar-right">
          <li><a href="register.php"><span class="glyphicon glyphicon-user"></span> Registeren</a></li>
          <li class="active"><a href="login.php"><span class="glyphicon glyphicon-log-in"></span>  Inloggen</a></li>
        </ul>
      </div>
    </div>
    </nav>
    <div class="container-fluid hero-slide">
      <div class="row">
    <div id="myCarousel" class="carousel slide " data-ride="carousel">



      <div class="carousel-inner" role="listbox">
        <div class="item active">
          <img src="images/login.jpg" alt="Ad">
    <div class="carousel-caption">
          <h3 class="caption">Inloggen</h3>

        </div>
      </div>
      </div>
    </div>
    </div>
  </div>

<div class="container padding-top-10">
  <div class="panel col-md-6">
    <div class="panel-heading "><h5>Inloggen</h5></div>
    <div class="panel-body">
      <form action="" method="post">
<label for="username" class="control-label">Gebruikersnaam:</label>
        <div class="row ">
          <div class="col-md-12">
            <input type="text" class="form-control" id="username" placeholder="Gebruikersnaam" name="username" requierd/>
          </div>
        </div>
<label for="password" class="control-label">Wachtwoord:</label>
        <div class="row ">
          <div class="col-md-6">
            <input type="password" class="form-control" id="password" placeholder="Wachtwoord" name="password" requierd/>
          </div>
          <div class="col-md-6">
            <input type="password" class="form-control" id="password" placeholder="Herhaal Wachtwoord" name="rpassword" requierd/>
          </div>
        </div>
        <div class="row">
          <div class="col-md-2 padding-top-10">
            <input type="submit" name="inloggen" class="btn btn-succes" value="Login">
          </div>
        </div>

        <div class="row">
          <div class="col-md-12 padding-top-10">
            <p class="pleft">Door op <b>login</b> te klikken, gaat u akkoord met de <a class="algemeen" href="#" data-toggle="modal" data-target="#t_and_c_m">Algemene Voorwaarden</a> opgesteld door deze site.</p>
          </div>
        </div>
      </form>
    </div>
</div>
</div>


  <div class="modal fade" id="t_and_c_m" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">Algemene Voorwaarden</h4>
            </div>
            <div class="modal-body">
        <p>Deze algemene voorwaarden zijn tot stand gekomen in overleg
tussen de Nederlandse Vereniging van Banken en de Consumentenbond
in het kader van de Coördinatiegroep Zelfreguleringsoverleg
van de Sociaal-Economische Raad en treden in werking
per 1 november 2009. Over overige (product)voorwaarden die
van toepassing kunnen zijn heeft geen overleg plaatsgevonden.
De Consumentenbond behoudt zich het recht tot collectieve
actie voor met betrekking tot dergelijke voorwaarden.</p>
      </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">Ik ga akkoord</button>
            </div>
        </div>
    </div>
</div>

</div>

<footer>
  <div class="container">
    <div class="row">
      <div class="col-sm-3">
        <h6>Copyright &copy; 2016</h6>
        <ul class="list-unstyled">
          <li class="boss">Sander Bakker</li>
          <li class="unstyled">Bob Lansbergen</li>
          <li class="unstyled">Ronald van den Heuvel</li>
          <li class="unstyled">Max Donck</li>
        </ul>
      </div>
      <div class="col-sm-3">
        <h6>Over Ons</h6>
        <p id="pfont">Dit is een website ontworpen om een banksysteem te simuleren met PHP en mySQL</p>
      </div>

      <div class="col-sm-2">
        <h6>Navigatie</h6>
        <ul class="list-unstyled">
          <li class="unstyled"><a href="#">Home</a></li>
          <li class="unstyled"><a href="#">Particulier</a></li>
          <li class="unstyled"><a href="#">Persoonlijk</a></li>
          <li class="unstyled"><a href="#">Privé</a></li>
          <li class="unstyled"><a href="#">Zakelijk</a></li>
        </uL>
      </div>
      <div class="col-sm-2">
        <h6>Contact</h6>
        <ul class="list-unstyled">
          <li class="unstyled"><a href="#">Google +</a></li>
          <li class="unstyled"><a href="#">Facebook</a></li>
          <li class="unstyled"><a href="#">Twitter</a></li>
          <li class="unstyled"><a href="#">YouTube</a></li>
        </uL>
      </div>
    </div>
  </div>
</footer>
  </body>
  </head>
  </html>

这是我正在使用的代码。另一个问题并不是因为他使用PDO而我使用MySQLi。我发现很难在这两个不同的PHP结构之间找到一个链接。

2 个答案:

答案 0 :(得分:0)

在此文件上,由于您已经启动了会话,请执行以下操作:

$ result = mysqli_query($ conn,$ query);

if (mysqli_num_rows($result) == 1) {
    $_SESSION['logged_user'] = $username; //Add this row
    header('location: loggedin.php');
} else {

然后,在loggedin页面上,执行以下操作:

session_start(); //must be declared before the usage of any $_SESSION variables
if(isset($_SESSION['logged_user']))
echo "Welcome, " . $_SESSION['logged_user'];

要注销,您可以:

session_destroy();

答案 1 :(得分:0)

首先在登录中创建username session。并检查用户是否已登录。检查下面的代码。:

<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "login");


if (isset($_POST['inloggen'])) {
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    $rpassword = mysqli_real_escape_string($conn, $_POST['rpassword']);

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

    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) == 1) {

        // First create one session username like this after login.
        $rowUsers = mysqli_fetch_array($result,MYSQLI_ASSOC);
        $_SESSION['username'] = $rowUsers['username'];

        header('location: loggedin.php');
    } else {
        header('location: error.php');
    }

    echo "<br />";
}
?>



<ul class="nav navbar-nav navbar-right">
  <li><a href="register.php"><span class="glyphicon glyphicon-user"></span> Registeren</a></li>
    //Check whether user is logged in or not.
    <?php
    if(isset($_SESSION['username']))
    {?>
    <li class="active"><span class="glyphicon glyphicon-user"></span>Welcome <?php echo $_SESSION['username'];?></li>
    <?php }
    else {?>
  <li class="active"><a href="login.php"><span class="glyphicon glyphicon-log-in"></span>Inloggen</a></li>
    <?php }?>
</ul>