สอนทำเว็บไซต์

หน้า login/logout

login.php

<!DOCTYPE html> <html lang="en"> <head> <title>Lekjew.com</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <?php include_once("include/style.php"); ?> </head> <body class="bg-secondary"> <div class="container minbody"> <form autocomplete="off" class="contact-form" method="post" action="checkuser.php" role="form"> <div class="row mt-5 mt-30"> <div class="col-md-4"></div> <div class="col-md-4"> <div class="card m-20"> <div class="row m-20"> <div class="col-md-12"> <div class="col-md-12 text-center mb-4"><img src="img/LogoLekjew.jpg" class="img-responsive" height="40"></div> <div class="col-lg-12 mb-4"> <div class="form-floating"> <input type="text" class="form-control" id="u_username" name="user" placeholder="UserName" required onKeyDown="return nextbox(event, 'u_password');"> <label for="u_username light-300">Name</label> </div> </div><!-- End Input Name --> <div class="col-lg-12 mb-4"> <div class="form-floating"> <input type="password" class="form-control" id="u_password" name="passw" placeholder="Password" required onKeyDown="return nextbox(event, 'ASubmit');"> <label for="u_password light-300">Password</label> </div> </div><!-- End Input Name --> <div class="col-md-12 col-12 m-auto text-center"> <button type="submit" class="btn btn-secondary rounded-pill px-md-5 px-4 py-2 radius-0 text-light light-300" id="ASubmit">Login</button> </div> <br><br> </div> </div> </div> </div> </div> </form> </div> </body> </html> <?php include_once("include/js.php"); ?> <script> $('#u_username').focus(); function nextbox(e, id) { // อ่าน keycode (cross browser) var keycode = e.which || e.keyCode; // ตรวจสอบ keycode (13 คือ กด enter) if (keycode == 13) { if(id=="ASubmit"){ $('#ASubmit').click(); } // ย้ายโฟกัสไปยัง input ที่ id document.getElementById(id).focus(); // return false เพื่อยกเลิกการ submit form return false; } } </script>

checkuser.php

<?php session_start(); $user = $_POST["user"]; $pwd = $_POST["passw"]; include_once("connectDBMS.php"); $URL = "error.php"; $sqlchk = " SELECT u_code,u_username,u_password,u_userlastlogin FROM sysuser WHERE u_username = '$user' and u_password = '$pwd' "; $rs = sqlsrv_query($ConnDB, $sqlchk); while ( $row = sqlsrv_fetch_object($rs ) ){ if(!empty($row->u_username) === true && !empty($row->u_password) === true){ $_SESSION["username"] = $row->u_username; $_SESSION["ucode"] = $row->u_code; $URL = "dashboard.php"; } } echo "<script>window.location='".$URL."';</script>"; sqlsrv_close($ConnDB); ?>

checklogin.php

<?php session_start(); $ucode=$_SESSION["ucode"]; $username=$_SESSION["username"]; if($ucode==""){ // ถ้าตรวจสอบไม่พบ session ให้ไปที่หน้า error แล้วออกจากการทำงาน header( "location: error.php" ); exit(0); // ถ้าพบการ login เข้ามาเชื่อมต่อ Database เพื่อใช้งานระบบต่อไป }else{ include('include/connectDBMS.php'); } ?>

include/header_login.php

<nav id="main_nav" class="navbar navbar-expand-lg navbar-light bg-white shadow"> <div class="d-flex justify-content-between align-items-center"> <a class="ml-30 navbar-brand h1" href="index.php"> <img src="img/LogoLekjew.jpg" class="img-responsive" height="40"> </a> <?php echo $username; ?> <button class="navbar-toggler border-0" onclick="showver('navbar-toggler-success');" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-toggler-success" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> </div> </nav>

leftmenu.php

<div id="navbar-toggler-success" class="align-self-center collapse flex-fill d-lg-flex justify-content-lg-between" style="z-index:1;display:block;" > <?php $page = explode("/", $_SERVER['REQUEST_URI']); ?> <div id="menuleft" class=" bg-light left main-sidebar"> <div class="sidebar-inner leftscroll"> <div id="sidebar-menu" style=""> <ul> <li class="submenu"><a class="<?php if($page[2]=="logout.php"){ echo " active"; } ?>" href="logout.php"><i class="fa fa-fw fa-power-off" aria-hidden="true"></i><span> Logout </span> </a></li> </ul> </div> </div> <div class="clearfix"></div> </div> </div>

dashboard.php

<?php include_once("checklogin.php"); ?> <!DOCTYPE html> <html lang="en"> <head> <title>Lekjew.com</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <?php include_once("include/style.php"); ?> </head> <body class="adminbody"> <?php include_once("include/header_login.php"); ?> <div id="main" > <?php include_once("leftmenu.php"); ?> <div class="content-page"> <div class="content"> <div class="col-md-12"> <div class="card pd-10"> DashBoard <br><br> </div> </div> </div> </div> </body> </html> <?php include_once("include/js.php"); ?>

error.php

<!DOCTYPE html> <html lang="en"> <head> <title>Lekjew.com</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <?php include_once("include/style.php"); ?> </head> <body> <?php include_once("include/header_login.php"); ?> <div class="container"> <div class="card m-20"> <div class="row m-20"> <div class="col-md-12"> User หรือ Password ไม่ถูกต้อง <br><br> </div> </div> </div> </div> </body> </html> <?php include_once("include/js.php"); ?>

logout.php

<?php session_start(); session_destroy(); echo("<script>window.location='login.php';</script>"); ?>