본문 바로가기

BootStrap/Bootstrap Sample File

HOW TO CREATE A LOGIN SESSION USING PHP MYSQLI AND BOOTSTRAP

How to Create a Login Session Using PHP MySQLi and Bootstrap-In this article I will discuss how to create a login session using php mysqli and bootstrap . in building a login session , I hope you 've mastered making website theme with guide bootstrap . You can visit the official website of the bootstrap to download the plugin and learn bootstrap




After that, create a new folder on your web server with any name up to you , advised the new folder name is "loginsession". then create files that exist below the new folder.

Create a new database with the name "biodata" and the name of the table "login" along with fields like "id_login" , "name_login" , "username" and "password" . Do not forget to copy thebootstrap and jquery plugins into the new folder . 

login.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  <div class="container">
   
<?php
$username=$_POST['username'];
$password=md5($_POST['password']);
$login=$_POST['login'];
if(isset($login)){
  $mysqli = new mysqli("localhost", "root", "pidie", "biodata");
  if ($mysqli->connect_errno) {
    <span id="IL_AD11" class="IL_AD">echo</span> "Failed to connect to MySQL: " . $mysqli->connect_error;
  }
  $res = $mysqli->query("SELECT * FROM login where username='$username' and password='$password'");
  $row = $res->fetch_assoc();
  $name = $row['name_login'];
  $user = $row['username'];
  $pass = $row['password'];
  if($user==$username && $pass=$password){
    session_start();
    $_SESSION['mysesi']=$name;
    echo "<script>window.<span id="IL_AD10" class="IL_AD">location</span>.assign('index.php')</script>";
  } else{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
  <button <span id="IL_AD7" class="IL_AD">type</span>="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
  <strong>Warning!</strong> This username or password not same with database.
</div>
<?php
  }
}
?>
   
    <div class="panel panel-default">
      <div class="panel-body">
     
    <h2>Login Session</h2>
    <form role="form" method="post">
      <div class="form-group">
 <<span id="IL_AD6" class="IL_AD">label</span> for="username">Username</label>
 <input type="text" class="form-control" id="username" name="username">
      </div>
      <div class="form-group">
 <label for="password">Password</label>
 <input type="password" class="form-control" id="password" name="password">
      </div>
      <button type="submit" name="login" class="btn btn-primary">Login</button>
    </form>
       
      </div>
     </div>
     
  </div>


index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
session_start();
 
if (!isset($_SESSION['mysesi']))
{
  echo "<script>window.location.assign('login.php')</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta <span id="IL_AD9" class="IL_AD">charset</span>="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login Session</title>
 
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
 
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via <span id="IL_AD12" class="IL_AD">file</span>:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
  <p>
</p>
  <div class="container">
   
    <div class="jumbotron">
      <h1>Welcome, hi <?php echo $_SESSION['mysesi'] ?></h1>
      <p>This is <span id="IL_AD5" class="IL_AD">application</span> login session</p>
      <p><a class="btn btn-primary btn-lg" href="logout.php" role="button">Logout</a></p>
    </div>
     
  </div>
 
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html> 


logout.php

1
2
3
4
5
<?php
session_start();
echo "<script>window.location.assign('login.php')</script>";
session_destroy();
?>