authenticate.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /*$DATABASE_HOST = '10.1.1.202:8080';
  11. $DATABASE_USER = 'maabim';
  12. $DATABASE_PASS = 'maabim001';
  13. $DATABASE_NAME = 'phplogin';*/
  14. $hostname = "GMGIS\SQLEXPRESS"; //test server 201
  15. $username = 'BIMuser';
  16. $password = 'Component3444';
  17. $db_name = 'BIMComponents';
  18. try {
  19. $pdo = new PDO('sqlsrv:Server='.$hostname.';Database='.$db_name,$username,$password);
  20. $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  21. } catch (PDOException $e) {
  22. echo $e;
  23. echo json_encode('Error connecting to the server.');
  24. die ();
  25. }
  26. $sth = $pdo->prepare("SELECT * FROM [BIMComponents].[dbo].[User_Account] WHERE [account] = ?;");
  27. $sth->execute(array($_POST['username']));
  28. if (!isset($_POST['username'], $_POST['password'])) {
  29. exit('Please fill both the username and password fields!');
  30. }
  31. $i = 0;
  32. $query = $sth->fetchAll();
  33. foreach ($query as $row){
  34. $i++;
  35. if (md5($_POST['password']) == $row["userPassword"]) {
  36. session_regenerate_id();
  37. $_SESSION['loggedin'] = TRUE;
  38. $_SESSION['name'] = $row["account"];
  39. $_SESSION['id'] = $row["userID"];
  40. $_SESSION['check_word'] = '';
  41. echo 'success';
  42. } else {
  43. echo '密碼錯誤,請重新輸入';
  44. }
  45. }
  46. if ($i == 0) {
  47. echo '帳號不存在,請再次檢查';
  48. }
  49. $sth = null;
  50. $pdo = null;