สอบถาม php ครับ เวลาล็อคอินผ่านหน้า log in ไปแล้วเข้าหน้าหลัก ถ้าไม่ให้เข้าหน้าหลักโดยตรงต้องทำยังงัย
พอดีผมสั่งหน้า login ให้ authen user และ password จาก AD จากเซิฟเวอร์ได้แล้ว ถ้า login ผ่าน จะเข้าหน้าชื่อว่า main_menu.php
ได้แล้ว แต่ปัญหาของผมคือคนที่รู้หน้าหลักว่าชื่อ main_menu.php สามารถเรียกเข้าได้โดยตรงไม่ต้องผ่านหน้า login ผมสามารถทำให้เวลาเค้าเรียกหน้า main_menu.php แล้วเด้งไปหน้า login ก่อนได้มั้ยครับมีวิธีไหนบ้างครับมือใหม่
Code หน้า index.php
Code (PHP)
<?php
if(isset($_POST['username']) && isset($_POST['password'])){
$adServer = "192.168.1.1";
$ldap = ldap_connect($adServer);
$username = $_POST['username'];
$password = $_POST['password'];
$ldaprdn = 'lightsource' . "\\" . $username;
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = @ldap_bind($ldap, $ldaprdn, $password);
if ($bind) {
$filter="(sAMAccountName=$username)";
$result = ldap_search($ldap,"DC=ตัวอย่าง,DC=co,DC=th",$filter);
ldap_sort($ldap,$result,"sn");
$info = ldap_get_entries($ldap, $result);
for ($i=0; $i<$info["count"]; $i++)
{
echo "<meta http-equiv='refresh' content='0 ;url=main_menu.php'>" ;
}
@ldap_close($ldap);
} else {
$msg = "Invalid email address / password";
echo $msg;
}
}else{
?>
<form action="#" method="POST">
<?php } ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>:: Home</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<style type="text/css">
/* Sticky footer styles
-------------------------------------------------- */
html, body {
height: 100%;/* The html and body elements cannot have any padding or margin. */
}
/* Wrapper for page content to push down footer */
#wrap {
min-height: 100%;
height: auto !important;
height: 100%;
/* Negative indent footer by it's height */
margin: 0 auto -60px;
}
/* Set the fixed height of the footer here */
#push, #footer {
height: 60px;
}
#footer {
background-color: #f5f5f5;
}
/* Lastly, apply responsive CSS fixes as necessary */
@media (max-width: 767px) {
#footer {
margin-left: -20px;
margin-right: -20px;
padding-left: 20px;
padding-right: 20px;
}
}
/* Custom page CSS
-------------------------------------------------- */
/* Not required for template or sticky footer method. */
#wrap > .container {
padding-top: 60px;
}
.container .credit {
margin: 20px 0;
}
code {
font-size: 80%;
}
</style>
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="../assets/js/html5shiv.js"></script>
<![endif]-->
<!-- Fav and touch icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">
<link rel="shortcut icon" href="assets/ico/favicon.png">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<body background="admin/images/1180422460.gif">
<p> </p>
<p> </p>
<p><br>
</p>
<table width="100%" border="0">
<tr>
<td align="center"> </td>
</tr>
</table>
<p> </p>
<p> </p>
<form ACTION="<?php echo $loginFormAction; ?>" name="login" method="POST">
<table width="35%" border="0" align="center">
<tr>
<td height="27"><strong><i class="icon-user"></i> Username </strong></td>
<td align="right"> </td>
<td><strong><i class="icon-lock"></i> Password</strong></td>
</tr>
<tr>
<td width="45%" height="32"><input type="text" name="username" id="textfield"></td>
<td width="3%" align="right"></td>
<td width="52%"><input type="password" name="password" id="textfield2"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="3" align="left"><button type="submit" class="btn btn-success"><i class="icon-upload"></i> Login </button>
</tr>
</table>
</form>
<p><br>
</p>
</body>
</html>
Code หน้า main_menu.php
Code (PHP)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>:: Home</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<style type="text/css">
/* Sticky footer styles
-------------------------------------------------- */
html, body {
height: 100%;/* The html and body elements cannot have any padding or margin. */
}
/* Wrapper for page content to push down footer */
#wrap {
min-height: 100%;
height: auto !important;
height: 100%;
/* Negative indent footer by it's height */
margin: 0 auto -60px;
}
/* Set the fixed height of the footer here */
#push, #footer {
height: 60px;
}
#footer {
background-color: #f5f5f5;
}
/* Lastly, apply responsive CSS fixes as necessary */
@media (max-width: 767px) {
#footer {
margin-left: -20px;
margin-right: -20px;
padding-left: 20px;
padding-right: 20px;
}
}
/* Custom page CSS
-------------------------------------------------- */
/* Not required for template or sticky footer method. */
#wrap > .container {
padding-top: 60px;
}
.container .credit {
margin: 20px 0;
}
code {
font-size: 80%;
}
</style>
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="../assets/js/html5shiv.js"></script>
<![endif]-->
<!-- Fav and touch icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">
<link rel="shortcut icon" href="assets/ico/favicon.png">
<style type="text/css">
body,td,th {
color: #009933;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<body background="images/1180422460.gif">
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>
<a class="brand" href="#">IT LIGHTSOURCE</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li><a href="main_menu.php"><i class="icon-home"></i> Home</a></li>
<li><a href="education.php" target="_parent"><i class="icon-file"></i> Education</a></li>
<li><a href="http://helpdesk.lightsource.co.th" target="_blank"><i class="icon-wrench"></i> Helpdesk</a></li>
<li><a href="#"><i class="icon-barcode"></i> ลาออนไลน์</a></li>
<!--</h1><li><a href="ind.php"><i class="icon-user"></i> Administrator</a></li>-->
<li><a href="<?php echo $logoutAction ?>"><i class="icon-refresh"></i> Log out</a></li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
</div>
<p><br>
<?php
$date = date("Y-m-d");
$time = date("H:i:s");
?>
</p>
<tr></tr>
<table width="100%" border="0">
<tr>
<td width="159" height="100" align="left"> </td>
<td width="369" height="100"align="center"><p><strong> <img src="images/mainmenulogo.png" width="300" height="72"></strong></p></td>
<td width="172" align="center"></td>
</tr>
</table>
<script src="js/bootstrap.min.js"></script>
<table width="100%" align="center" border="0" >
<tr >
<td width="20%" align="center"><strong></strong></td>
<td width="11%" align="center"><a href="education.php" target="_parent"><img src="images/iconeducation.png" alt="education" longdesc="education.php"></a></td>
<td width="1%" align="center"></td>
<td width="11%" align="center"><a href="http://helpdesk.lightsource.co.th" target="_blank"><img src="images/iconhelpdesk.png" alt="helpdesk" longdesc="http://helpdesk.lightsource.co.th"></td>
<td width="1%" align="center"><strong></strong></td>
<td width="11%" align="center"><strong><img src="images/IconLaOnline.png" alt="LaOnline"></strong></td>
<td width="1%" align="center"><strong></strong></td>
<td width="11%" align="center"><a href="download.php" target="_parent"><img src="images/iconDownload.png" alt="helpdesk" longdesc="http://helpdesk.lightsource.co.th"></td>
<td width="20%" align="center"><strong></strong></td>
</tr>
</table>
<table width="100%" align="center" border="0" >
<tr >
<td width="20%" align="center"><strong></strong></td>
<td width="11%" align="center">Education</td>
<td width="1%" align="center"></td>
<td width="11%" align="center">Helpdesk</td>
<td width="1%" align="center"><strong></strong></td>
<td width="11%" align="center">ลาออนไลน์</td>
<td width="1%" align="center"><strong></strong></td>
<td width="11%" align="center">Download</td>
<td width="20%" align="center"><strong></strong></td>
</tr>
</table>
<div class="row">
<div class="span5" align="center">
<table width="38%" height="46" border="0" align="center">
<tr>
<td height="42" align="center"><?php if ($pageNum_AJ > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_AJ=%d%s", $currentPage, 0, $queryString_AJ); ?>">
<button type="button" class="btn btn-mini btn-success"> <i class="icon-fast-backward"></i> </button>
</a>
<?php } // Show if not first page ?></td>
<td align="center"><?php if ($pageNum_AJ > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_AJ=%d%s", $currentPage, max(0, $pageNum_AJ - 1), $queryString_AJ); ?>">
<button type="button" class="btn btn-mini btn-success"> <i class="icon-backward"></i> </button>
</a>
<?php } // Show if not first page ?></td>
<td align="center"><?php if ($pageNum_AJ < $totalPages_AJ) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_AJ=%d%s", $currentPage, min($totalPages_AJ, $pageNum_AJ + 1), $queryString_AJ); ?>">
<button type="button" class="btn btn-mini btn-success"> <i class=" icon-forward"></i> </button>
</a>
<?php } // Show if not last page ?></td>
<td align="center"><?php if ($pageNum_AJ < $totalPages_AJ) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_AJ=%d%s", $currentPage, $totalPages_AJ, $queryString_AJ); ?>">
<button type="button" class="btn btn-mini btn-success"> <i class="icon-fast-forward"></i> </button>
</a>
<?php } // Show if not last page ?></td>
</tr>
</table>
</div>
</div>
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="assets/js/jquery.js"></script>
<script src="assets/js/bootstrap-transition.js"></script>
<script src="assets/js/bootstrap-alert.js"></script>
<script src="assets/js/bootstrap-modal.js"></script>
<script src="assets/js/bootstrap-dropdown.js"></script>
<script src="assets/js/bootstrap-scrollspy.js"></script>
<script src="assets/js/bootstrap-tab.js"></script>
<script src="assets/js/bootstrap-tooltip.js"></script>
<script src="assets/js/bootstrap-popover.js"></script>
<script src="assets/js/bootstrap-button.js"></script>
<script src="assets/js/bootstrap-collapse.js"></script>
<script src="assets/js/bootstrap-carousel.js"></script>
<script src="assets/js/bootstrap-typeahead.js"></script>
</body>
</html>
<?php
?>
Tag : PHP
Date :
2016-02-24 10:53:27
By :
nuengbefree
View :
1131
Reply :
4
1. หน้า index.php
บรรทัดแรก <?php ให้แก้เป็น
Code (PHP)
<?php
session_start();
และบรรทัด echo "<meta http-equiv='refresh' content='0 ;url=main_menu.php'>" ;
ให้แก้เป็น
Code (PHP)
$_SESSION['sess_login'] = 'login';
header("Location: main_menu.php");
exit();
2. หน้า main_menu.php
บรรทัดบนสุดของหน้าให้เพิ่ม
Code (PHP)
<?php
session_start();
if($_SESSION['sess_login']==''){
header("Location: index.php");
exit();
}
?>
Date :
2016-02-24 12:55:49
By :
NAT
ขอบคุณมากครับผม แล้วถ้าผมจะทำปุ่ม log out ต้องใช้คำสั่งอันไหนครับผม
Date :
2016-02-25 11:50:07
By :
nuengbefree
แบบที่ผมเคยทำก็ ทุกหน้าสามารถเข้าได้เมื่อมีการ Login แล้วผมจะ Include(xxx.php)
ซึ่งใน xxx.php จะมี function ในการตรวจสอบว่ามี การ Login แล้วหรือเปล่าอ่ะครับ หากไม่มีก็จะ Redirect ไปยังหน้า Login อ่ะครับ ประมาณนี้
Code (PHP)
function require_login() {
/* this function checks to see if the user is logged in. if not, it will show
* the login screen before allowing the user to continue */
global $CFG, $_COOKIE;
if(!isset($_COOKIE['user_id'])){
header('Location:index.php');
break;
}
}
ส่วนหน้า Logout ก็จะมีคำสั่ง ทำลาย Cookie ไปอ่ะครับ
Code (PHP)
<?php session_start();
setcookie('user_id','', 0,'/');
setcookie('pos_id','', 0,'/');
setcookie('username','', 0,'/');
setcookie('fullname','', 0,'/');
setcookie('position','', 0,'/');
setcookie('email_cookie','', 0,'/');
setcookie('tel_cookie','', 0,'/');
header('Location:index.php');
?>
Date :
2016-02-26 10:54:14
By :
nut_ch31
Load balance : Server 05