ผมใช้ระบบล้อกอินจากที่นี่
https://www.thaicreate.com/community/php-mysql-login-form-check-username-password.html
แล้วผมเขียนไฟล์ connect.php แยกออกมาเพื่อจะได้แก้ไขง่ายๆ
connect.php
Code
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "1234";
$dbname = "sa";
if(!($conn = mysql_connect($dbhost,$dbuser,$dbpass)))
{
die("Couldn't establish connection to Mysql,database!");
}
if(!mysql_select_db($dbname))
{
die("Couldn't connect to database $dbname!");
}
mysql_query("SET NAMES UTF8");
?>
login.php
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<link rel="stylesheet" type="text/css" href="bootstrap.css" />
</head>
<body>
<form name="form1" method="post" action="check_login.php" class="form-search">
<br><center>Login</center><br>
<table border="0" style="width: 300px" align="center">
<tbody>
<tr>
<td> Username</td>
<td>
<input name="txtUsername" type="text" id="txtUsername" class="input-medium search-query">
</td>
</tr>
<tr>
<td> Password</td>
<td><input name="txtPassword" type="password" id="txtPassword" class="input-medium search-query">
</td>
</tr>
</tbody>
</table>
<br>
<center><input type="submit" name="Submit" value="Login" class="btn"></center>
</form>
</body>
</html>
check_login.php
Code
<?
session_start();
require_once("connect.php");
$strSQL = "SELECT * FROM member WHERE Username = '".trim($_POST['txtUsername'])."'
and Password = '".trim($_POST['txtPassword'])."'";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if(!$objResult)
{
echo "Username and Password Incorrect!";
}
else
{
$_SESSION["UserID"] = $objResult["UserID"];
$_SESSION["Status"] = $objResult["Status"];
session_write_close();
if($objResult["Status"] == "ADMIN")
{
header("location:admin_page.php");
}
else
{
header("location:user_page.php");
}
}
mysql_close();
?>
error ที่ขึ้น
Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\sa\connect.php:1) in C:\AppServ\www\sa\check_login.php on line 21
ไม่ทราบว่าจะแก้ยังไงดีครับ ผมงมมา 1 วันแล้วไม่ได้สักที
Tag : PHP, MySQL, CakePHP