Guest User

Untitled

a guest
Jan 15th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. <?php
  2. session_start();
  3. class Mysql {
  4. private $conn;
  5.  
  6. function __construct() {
  7. $this->conn = new PDO('mysql:host=***;dbname=***;charset=UTF-8','***','***') or
  8. die('There was a problem connecting to the database.');
  9. }
  10.  
  11. function verify_Username_and_Pass($un, $pwd) {
  12. $query = "SELECT Username
  13. FROM Conference
  14. WHERE Username = :un AND Password = :pwd";
  15.  
  16. $stmt = $this->conn->prepare($query);
  17.  
  18. $stmt->bindParam(':un', $un);
  19. $stmt->bindParam(':pwd', $pwd);
  20. $stmt->execute();
  21. if ($stmt->rowCount() > 0) {
  22. // User exist
  23. $stmt->bindColumn('First Name', $firstName);
  24. $_SESSION["FirstName"] = $firstName;
  25. die($_SESSION["FirstName"]);
  26. return true;
  27. $stmt->close();
  28. }
  29. else {
  30. // User doesn't exist
  31. //die("failure");
  32. return false;
  33. $stmt->close();
  34. }
  35. }
  36. }
  37. ?>
  38.  
  39. class Mysql
  40. {
  41. private $conn;
  42. public $error;
  43. public $username;
  44.  
  45. function __construct()
  46. {
  47. try {
  48. $this->conn = new PDO( 'mysql:host=localhost;dbname=****', 'root', '****' );
  49. $this->conn->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
  50.  
  51. }
  52. catch ( Exception $e ) {
  53. $this->error = $e->getMessage();
  54. }
  55. }
  56.  
  57. function verify_Username_and_Pass( $un, $pwd )
  58. {
  59. $query = "SELECT Username
  60. FROM Conference
  61. WHERE Username = :un AND Password = :pwd";
  62.  
  63. $stmt = $this->conn->prepare( $query );
  64. if( !$stmt ) {
  65. $this->error = $this->conn->errorInfo();
  66. return false;
  67. }
  68.  
  69. $stmt->bindParam( ':un', $un );
  70. $stmt->bindParam( ':pwd', $pwd );
  71. $stmt->execute();
  72. if ( $stmt->rowCount() > 0 ) {
  73. // User exist
  74. $this->username = $stmt->fetchColumn();
  75. return true;
  76. }
  77. else {
  78. // User doesn't exist
  79. return false;
  80. }
  81. }
  82.  
  83. }
  84.  
  85. session_start();
  86.  
  87. $db = new Mysql();
  88. if( !$db->error ) {
  89. if( $db->verify_Username_and_Pass ( 'user', 'test' )) {
  90. $_SESSION["FirstName"] = $db->username;
  91. }
  92. else
  93. echo 'Unknown user';
  94. }
  95.  
  96. var_dump( $db );
  97.  
  98. Unknown user
  99.  
  100. object(Mysql)#1 (3) {
  101. ["conn":"Mysql":private]=> object(PDO)#2 (0) { }
  102. ["error"]=> array(3)
  103. { [0]=> string(5) "42S02"
  104. [1]=> int(1146)
  105. [2]=> string(39) "Table 'xxxx.Conference' doesn't exist" }
  106. ["username"]=> NULL }
Add Comment
Please, Sign In to add comment