วันพุธที่ 18 ธันวาคม พ.ศ. 2562

Login php by Ajax

Login php by Ajax

ສະບາຍດີ ຈາກທີ່ຫ່າງຫາຍໄປດົນ ກັບມາຄັ້ງນີ້ ມີບົດຄວາມ ການ Login ແບບໃຊ້ Ajax ໂດຍ PHP
ຊຶ່ງຈະມີຂັ້ນຕອນດັ່ງນີ້


1. Databases code mysql
==============================================================
-- phpMyAdmin SQL Dump
-- version 4.9.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 18, 2019 at 05:40 AM
-- Server version: 10.3.16-MariaDB
-- PHP Version: 7.1.30

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `mydatabase`
--

-- --------------------------------------------------------

--
-- Table structure for table `account`
--

CREATE TABLE `account` (
  `ID` int(10) NOT NULL,
  `Username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `Pass` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `code` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `account`
--

INSERT INTO `account` (`ID`, `Username`, `Pass`, `code`) VALUES
(1, 'beh', '1234', 1);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `account`
--
ALTER TABLE `account`
  ADD PRIMARY KEY (`ID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `account`
--
ALTER TABLE `account`
  MODIFY `ID` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

==================================================================
2. AjaxPHPLoginForm1.php


<html>
<head>
<title>Test Ajax Tutorial</title>
</head>
<script language="JavaScript">
   var HttPRequest = false;
   function doCallAjax() {
  HttPRequest = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
  } else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
  }

  if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
  }

  var url = 'AjaxPHPLoginForm2.php';
  var pmeters = "tUsername=" + encodeURI( document.getElementById("txtUsername").value) +
"&tPassword=" + encodeURI( document.getElementById("txtPassword").value );

HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);


HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3)  // Loading Request
{
document.getElementById("mySpan").innerHTML = "Now is Loading...";
}

if(HttPRequest.readyState == 4) // Return Request
{
if(HttPRequest.responseText == 'Y')
{
window.location = 'AjaxPHPLoginForm3.php';
}
else
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}

}

   }
</script>
<body align="center">
<h1>Login Form</h1>
<form name="frmMain">
<span id="mySpan"></span>
<table width="274" border="1" align="center">
  <tr>
    <th width="117">
      <div align="left">Username</div></th>
    <th><input type="text" name="txtUsername" id="txtUsername" size="20"></th>
  </tr>
  <tr>
    <th width="117">
      <div align="left">Password</div></th>
    <th><input type="password" name="txtPassword" id="txtPassword" size="20"></th>
  </tr>
</table>
<br>
<input name="btnLogin" type="button" id="btnLogin" OnClick="JavaScript:doCallAjax();" value="Login">
</form>
</body>
</html>
==================================================================

3. AjaxPHPLoginForm2.php

<?php

session_start();
$strUsername = trim($_POST["tUsername"]);
$strPassword = trim($_POST["tPassword"]);
//*** Check Username ***//
if(trim($strUsername) == "")
{
echo "<font color=red>**</font> Plase input [Username]";
exit();
}
//*** Check Password ***//
if(trim($strPassword) == "")
{
echo "<font color=red>**</font> Plase input [Password]";
exit();
}

$localhost = "localhost";
$root = "root";
$pass = "";
$db_staffdata = "mydatabase";
$con = mysqli_connect($localhost,$root,$pass,$db_staffdata);
mysqli_query($con,"set names utf8");
if(!$con){
die("Connect Faile :".mysqli_connect_error());
}
//*** Check Username & Password ***//
$strSQL = "SELECT * FROM account WHERE Username = '".$strUsername."' and Pass = '".$strPassword."' ";
$objQuery = mysqli_query($con,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
if($objResult)
{
//*** Session ***//
$_SESSION["ID"] = $objResult["ID"];
$_SESSION["Username"] = $objResult["Username"];
$_SESSION["Pass"] = $objResult["Pass"];
$_SESSION["code"] = $objResult["code"];
echo "<p align='center'>";
echo "<BR>";
echo "<span style='color:#99ebff';>";
echo "ຍິນດີຕ້ອນຮັບທ່ານ &nbsp; $objResult[Username] &nbsp;ເຂົ້າສູ່ລະບົບ !!!";
echo "</span>";
echo "<br>";
echo "<meta http-equiv='refresh' content='2;url=./AjaxPHPLoginForm3.php'>";
echo "</p>";


}else{
echo "<font color=red>**</font> Username & Password is wrong";
echo "<p align='center'>";
echo "<span style='color:#ff6666;'>";
echo "ບໍ່ສາມາດເຂົ້າສູ່ລະບົບໄດ້";
echo "ກະລຸນາລອງໃໝ່ອີກຄັ້ງ";
echo "</span>";
echo "<meta http-equiv='refresh' content='2;url=./AjaxPHPLoginForm1.php'>";
echo "</p>";
}

mysqli_close($con);
?>

==================================================================
4. AjaxPHPLoginForm3.php

<?php
session_start();
if($_SESSION["Username"] == "")
{
header("location:AjaxLoginForm.php");
exit();
}

$localhost = "localhost";
$root = "root";
$pass = "";
$db_staffdata = "mydatabase";
$con = mysqli_connect($localhost,$root,$pass,$db_staffdata);
mysqli_query($con,"set names utf8");
if(!$con){
die("Connect Faile :".mysqli_connect_error());
}
$strSQL = "SELECT * FROM account WHERE Username = '".$_SESSION["Username"]."'  ";
$objQuery = mysqli_query($con,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
?>
<html>
<head>
<title>test Ajax Tutorial</title>
</head>
<body align="center">
<h1>Welcome [<?php echo $objResult["Username"];?>] to My Website</h1>
</body>
</html>

==================================================================
ຈົບໄປແລ້ວ Code ທັ້ງໝົດທີ່ມີ ຕໍ່ໄປຈະເປັນຂອງສ່ວນ ສະແດງຜົນ 
==================================================================
from login
Main login true

==================================================================
ຈົບໄປແລ້ວສຳຫລັບບົດຄວາມນີ້ ມີຂໍ້ສົງໃສໃດຖາມໄດ້ ຝາກົດຕິດຕາມ ແນ່ເດິ້













วันศุกร์ที่ 25 มกราคม พ.ศ. 2562

How to Connection to Databases PHP by PDO

Hi! Every One ສະບາຍດີມືນີ້ມາພົບກັບການ ຂຽນ File Connect to DB ແບບ PDO ຊຶ່ງຈະມີຂັ້ນຕອນທີ່ງ່າຍດາຍ ແຕ່ຈະຂຽນໃນຮູບແບບ Mysql
1. phpinfo ເພື່ອຫາ Extension ໃນ ເຄື່ອງເຮົາວ່າຮອງຮັບການຂຽນແບບ PDO ໄດ້ບໍ
=======================================
<?php
phpinfo();
?>
=======================================
Run ຜ່ານ localhost/PDO(PDO ແມ່ນຊື່ໄຟລ໌ Server ຈຳລອງຂອງເຮົາ ໃຫ້ຕັ້ງຊື່ຕາມເລີຍ)/phpinfo.php

Extension to PDO
ຖ້າເຄື່ອງໃຜບໍ່ມີ ໃຫ້ໄປຫາ Download Key Word : download pdo extension php ແລ້ວໄປຕິດຕັ້ງ
ເມື່ອຕິດຕັ້ງແລ້ວ ມາເຂົ້າສູ່ການຂຽນ ຕິດຕໍ່ກັບຖານຂໍ້ມູນເລີຍ

con.php
=======================================
<?php
  $serverName = "localhost";
  $userName = "root";
  $userPassword = "44@44";
  $dbName = "mydatabase";
  $conn = new PDO('mysql:host=localhost;dbname=mydatabase', $userName, $userPassword);
  $conn->exec("set names utf8");

  if($conn){
    echo "Successfully to Connection ";
  }else{
    echo "Can't to Successfully Connection";
  }

?>
=======================================
references Read More : http://php.net/manual/en/book.pdo.php