authenticate.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. session_start();
  3. if ((empty($_SESSION['check_word'])) || (empty($_POST['captcha']))) {
  4. die("請填寫驗證碼");
  5. }else{
  6. if ($_SESSION['check_word'] != $_POST['captcha']) {
  7. die("驗證碼錯誤,請重新輸入");
  8. }
  9. }
  10. include("sql_detail.php");
  11. try {
  12. $pdo = new PDO('sqlsrv:Server='.$hostname.';Database='.$db_name,$username,$password);
  13. $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14. } catch (PDOException $e) {
  15. echo $e;
  16. echo json_encode('Error connecting to the server.');
  17. die ();
  18. }
  19. $sth = $pdo->prepare("SELECT [UserID],[UserName],[DepartmentID],[Email],[Account],[Password] FROM [User] WHERE [Account] = ?;");
  20. $sth->execute(array($_POST['username']));
  21. if (!isset($_POST['username'], $_POST['password'])) {
  22. exit('Please fill both the username and password fields!');
  23. }
  24. $i = 0;
  25. $query = $sth->fetchAll();
  26. foreach ($query as $row){
  27. $i++;
  28. if (md5($_POST['password']) == $row["Password"]) {
  29. session_regenerate_id();
  30. $_SESSION['loggedin'] = TRUE;
  31. $_SESSION['name'] = $row["UserName"];
  32. $_SESSION['UserID'] = $row["UserID"];
  33. $_SESSION['Account'] = $row["Account"];
  34. $_SESSION['Email'] = $row["Email"];
  35. $_SESSION['DepartmentID'] = $row["DepartmentID"];
  36. $_SESSION['check_word'] = '';
  37. echo 'success';
  38. } else {
  39. echo '密碼錯誤,請重新輸入';
  40. }
  41. }
  42. if ($i == 0) {
  43. echo '帳號不存在,請再次檢查';
  44. }
  45. $sth = null;
  46. $pdo = null;