· 8 years ago · Aug 30, 2018, 01:16 PM
1class problem in php
2<?php
3
4 try {
5
6 require_once('./lib/testdb.php');
7
8 $login = new Login();
9
10 }
11
12 catch(Exception $error) {
13
14 print $error->getMessage();
15
16 }
17
18
19
20 /* Include the HTML for the form */
21
22 require_once('./lib/form.php');
23
24?>
25
26<?php
27 define("DB_HOST", "localhost");
28
29 define("DB_USER", "root");
30
31 define("DB_PASS", "root");
32 class Login{
33 private $username;
34 private $password;
35 public function getUsername() {
36
37 return $this->username;
38
39 }
40
41 public function getPassword() {
42
43 return $this->password;
44
45 }
46
47 public function getEncryptedPassword() {
48
49 return sha1($this->password);
50
51 }
52
53 public function connectToMySQL() {
54
55 @mysql_connect(DB_HOST, DB_USER, DB_PASS) OR die("Cannot connect to MySQL server!");
56
57 mysql_select_db("dd") OR die("Cannot select database!");
58
59 }
60
61
62 public function verifyLogin($username, $password) {
63
64 $this->username = $username;
65
66 if(empty($username) || empty($password)) {
67
68 throw new Exception(Login::ERROR_EMPTY_LOGIN);
69
70 }
71
72 else {
73
74 $query = sprintf("SELECT * FROM 'users' WHERE username = '%s' AND
75
76 userpass = sha1('%s') ");
77
78 $this->clean($username),
79
80 $this->clean($password));
81
82
83
84 $result = mysql_query($query) OR die('Cannot perform query!');
85 while($info = mysql_fetch_array( $result ))
86 {
87 echo $info['username'];
88 }
89}
90?>
91
92--
93-- Database: `login`
94--
95
96-- --------------------------------------------------------
97
98--
99-- Table structure for table `login`
100--
101
102CREATE TABLE IF NOT EXISTS `login` (
103 `id` int(11) NOT NULL AUTO_INCREMENT,
104 `username` varchar(80) NOT NULL,
105 `password` varchar(80) NOT NULL,
106 PRIMARY KEY (`id`)
107) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
108
109--
110-- Dumping data for table `login`
111--
112
113INSERT INTO `login` (`id`, `username`, `password`) VALUES
114(1, 'ajay', 'password');
115
116$query = sprintf("SELECT * FROM 'users' WHERE username = '%s' AND
117 userpass = sha1('%s') "); // here
118 $this->clean($username),
119 $this->clean($password));
120
121$query = sprintf("SELECT * FROM 'users' WHERE username = '%s' AND
122 userpass = sha1('%s') ", // corrected
123 $this->clean($username),
124 $this->clean($password));
125
126<?php
127
128 try {
129
130 require_once('./lib/testdb.php');
131
132 $login = new Login();
133
134 }
135
136$result = mysql_query($query) OR die('Cannot perform query: ' . mysql_error());
137
138echo $query;