Guest User

Untitled

a guest
Sep 13th, 2018
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. PHP Class Function Ignores Return Statement
  2. <?php
  3.  
  4. //Class to handle mysql
  5. class db_handler {
  6. private $db_host = 'localhost';
  7. private $db_name = 'project';
  8. private $db_user = 'project';
  9. private $db_pass = 'dbpassword';
  10. private $db_con_mysql = '';
  11. private $db_con_db = '';
  12.  
  13. public function check_em($username, $password) {
  14. $db_query = "SELECT password FROM user WHERE name='".$username."' LIMIT 1;";
  15. if($this->db_con_mysql!='') {
  16. $db_query_response = mysql_query($db_query) or die('Query failed: '.mysql_error());
  17. $db_query_return = mysql_fetch_row($db_query_response);
  18. $db_sha1_hash = $db_query_return[0];
  19. echo $db_sha1_hash."<br>";
  20. echo sha1($password)."<br>";
  21. if(sha1($password)==$db_sha1_hash) {
  22. return 'user valid'; //THIS DOESN'T WORK!?!?!?
  23. } else {
  24. return 'no good';
  25. }
  26. } else {
  27. $this->db_connect();
  28. $this->check_em($username, $password);
  29. }
  30.  
  31. }
  32.  
  33. //Connect to mysql, then database
  34. private function db_connect() {
  35. $this->db_con_mysql = mysql_connect($this->db_host, $this->db_user, $this->db_pass) || die('Connection failed: '.mysql_error());
  36. $this->db_con_db = mysql_select_db($this->db_name) || die('Could not use'.$this->db_name.'. '.mysql_error());
  37. return;
  38. }
  39.  
  40. //Disconnect from database and reset vars used to track connection.
  41. private function db_disconnect() {
  42. if($this->db_con_mysql!='') {
  43. mysql_close();
  44. $this->db_con_mysql = '';
  45. $this->db_con_db = '';
  46. return;
  47. }
  48. }
  49.  
  50. public function fake($some_val) {
  51. if($some_val<6) {
  52. return TRUE;
  53. } else {
  54. return FALSE;
  55. }
  56. }
  57. }
  58.  
  59. $db_obj = new db_handler();
  60. $val1 = $db_obj->check_em('someuser','password'); //should return 'user valid'
  61. echo "val1:".$val1."<br>";
  62. echo "<br><br>";
  63.  
  64. $val2 = $db_obj->check_em('someuser','passw0rd'); //should return 'no good'
  65. echo "val2:".$val2."<br>";
  66. echo "<br><br>";
  67.  
  68. echo "test<br>";
  69. echo $db_obj->fake(4)."<br>";
  70.  
  71. ?>
  72.  
  73. 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
  74. 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
  75. val1:
  76.  
  77.  
  78. 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
  79. 7c6a61c68ef8b9b6b061b28c348bc1ed7921cb53
  80. val2:no good
  81.  
  82.  
  83. test
  84. 1
  85.  
  86. return $this->check_em($username, $password);
  87.  
  88. ...
  89. else {
  90. $this->db_connect();
  91. return $this->check_em($username, $password);
  92. }
  93. ...
Add Comment
Please, Sign In to add comment