robert@0
|
1 <code><font color="#000000">
|
robert@0
|
2 <font color="#0000BB"><?php
|
robert@0
|
3 </font><font color="#FF8000">/***********************
|
robert@0
|
4 Makes use of two MySQL tables.
|
robert@0
|
5
|
robert@0
|
6 users:
|
robert@0
|
7 CREATE TABLE `users` (
|
robert@0
|
8 `id` int(11) NOT NULL auto_increment,
|
robert@0
|
9 `username` varchar(20) default NULL,
|
robert@0
|
10 `password` varchar(40) default NULL,
|
robert@0
|
11 `fullname` varchar(30) default NULL,
|
robert@0
|
12 PRIMARY KEY (`id`)
|
robert@0
|
13 ) TYPE=MyISAM
|
robert@0
|
14
|
robert@0
|
15 seeds:
|
robert@0
|
16 CREATE TABLE `seeds` (
|
robert@0
|
17 `id` int(11) NOT NULL auto_increment,
|
robert@0
|
18 `seed` timestamp(14) NOT NULL,
|
robert@0
|
19 PRIMARY KEY (`id`)
|
robert@0
|
20 ) TYPE=MyISAM
|
robert@0
|
21
|
robert@0
|
22 */
|
robert@0
|
23
|
robert@0
|
24 // connect to mysql
|
robert@0
|
25 </font><font color="#0000BB">$mysql </font><font color="#007700">= </font><font color="#0000BB">mysql_connect</font><font color="#007700">(</font><font color="#DD0000">'localhost'</font><font color="#007700">,</font><font color="#DD0000">'###USERNAME###'</font><font color="#007700">,</font><font color="#DD0000">'###PASSWORD###'</font><font color="#007700">);
|
robert@0
|
26
|
robert@0
|
27 </font><font color="#FF8000">// fail on database errors
|
robert@0
|
28 </font><font color="#007700">if (!</font><font color="#0000BB">$mysql</font><font color="#007700">) {
|
robert@0
|
29 die(</font><font color="#DD0000">'false|Could not connect to MySQL'</font><font color="#007700">);
|
robert@0
|
30 }
|
robert@0
|
31
|
robert@0
|
32 </font><font color="#FF8000">// connect to the database
|
robert@0
|
33 </font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">'jamesdam_ajaxlogin'</font><font color="#007700">, </font><font color="#0000BB">$mysql</font><font color="#007700">);
|
robert@0
|
34
|
robert@0
|
35 </font><font color="#FF8000">// one task of the server is to provide random values to hash with
|
robert@0
|
36 </font><font color="#007700">if (</font><font color="#0000BB">$_GET</font><font color="#007700">[</font><font color="#DD0000">'task'</font><font color="#007700">]==</font><font color="#DD0000">'getseed'</font><font color="#007700">)
|
robert@0
|
37 {
|
robert@0
|
38 </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">'INSERT INTO seeds VALUES()'</font><font color="#007700">); </font><font color="#FF8000">// insert a new row with default values
|
robert@0
|
39
|
robert@0
|
40 // get the values from the row back
|
robert@0
|
41 </font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">'SELECT id, seed FROM seeds ORDER BY id DESC LIMIT 1'</font><font color="#007700">);
|
robert@0
|
42
|
robert@0
|
43 if (!</font><font color="#0000BB">$result</font><font color="#007700">) { </font><font color="#FF8000">// fail on error
|
robert@0
|
44 </font><font color="#007700">die(</font><font color="#DD0000">'false|'</font><font color="#007700">.</font><font color="#0000BB">mysql_error</font><font color="#007700">());
|
robert@0
|
45 }
|
robert@0
|
46
|
robert@0
|
47 </font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_assoc</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">); </font><font color="#FF8000">// only one row so take the first row
|
robert@0
|
48 </font><font color="#007700">echo(</font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'id'</font><font color="#007700">].</font><font color="#DD0000">'|'</font><font color="#007700">.</font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'seed'</font><font color="#007700">]); </font><font color="#FF8000">// write back the data in form id|random_value
|
robert@0
|
49 </font><font color="#007700">}
|
robert@0
|
50
|
robert@0
|
51 </font><font color="#FF8000">// the other task of the server is to check a username/password combination
|
robert@0
|
52
|
robert@0
|
53 </font><font color="#007700">else if (</font><font color="#0000BB">$_GET</font><font color="#007700">[</font><font color="#DD0000">'task'</font><font color="#007700">]==</font><font color="#DD0000">'checklogin'</font><font color="#007700">) {
|
robert@0
|
54 </font><font color="#FF8000">// formulate query for username
|
robert@0
|
55 </font><font color="#0000BB">$sql </font><font color="#007700">= </font><font color="#DD0000">'SELECT * FROM users WHERE username = \'' </font><font color="#007700">. </font><font color="#0000BB">mysql_real_escape_string</font><font color="#007700">(</font><font color="#0000BB">$_GET</font><font color="#007700">[</font><font color="#DD0000">'username'</font><font color="#007700">]) . </font><font color="#DD0000">'\''</font><font color="#007700">;
|
robert@0
|
56 </font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#0000BB">$sql</font><font color="#007700">);
|
robert@0
|
57
|
robert@0
|
58 </font><font color="#FF8000">// fail on sql failure
|
robert@0
|
59 </font><font color="#007700">if (!</font><font color="#0000BB">$result</font><font color="#007700">) {
|
robert@0
|
60 die(</font><font color="#DD0000">'false|Could not connect to login database. Please try again'</font><font color="#007700">);
|
robert@0
|
61 }
|
robert@0
|
62
|
robert@0
|
63 </font><font color="#FF8000">// get the first user with username in the table (should only be one)
|
robert@0
|
64 </font><font color="#0000BB">$user_row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_assoc</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">);
|
robert@0
|
65
|
robert@0
|
66 </font><font color="#FF8000">// if there isn't one
|
robert@0
|
67 </font><font color="#007700">if (!</font><font color="#0000BB">$user_row</font><font color="#007700">)
|
robert@0
|
68 {
|
robert@0
|
69 </font><font color="#FF8000">// then the username doesn't exist, but don't let the user know that this is the problem
|
robert@0
|
70 // rather inform them more vaguely that the combination is incorrect; prevents someone from
|
robert@0
|
71 // fishing for valid usernames
|
robert@0
|
72 </font><font color="#007700">die(</font><font color="#DD0000">'false|Invalid username and password combination.'</font><font color="#007700">);
|
robert@0
|
73 }
|
robert@0
|
74
|
robert@0
|
75 </font><font color="#FF8000">// formulate query for random timestamp for given id
|
robert@0
|
76 </font><font color="#0000BB">$sql </font><font color="#007700">= </font><font color="#DD0000">'SELECT * FROM seeds WHERE id=' </font><font color="#007700">. (int)</font><font color="#0000BB">$_GET</font><font color="#007700">[</font><font color="#DD0000">'id'</font><font color="#007700">];
|
robert@0
|
77 </font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#0000BB">$sql</font><font color="#007700">);
|
robert@0
|
78
|
robert@0
|
79 </font><font color="#FF8000">// die if no value for given id
|
robert@0
|
80 </font><font color="#007700">if (!</font><font color="#0000BB">$result</font><font color="#007700">) {
|
robert@0
|
81 die(</font><font color="#DD0000">'false|Unknown error (hacking attempt).'</font><font color="#007700">);
|
robert@0
|
82 }
|
robert@0
|
83
|
robert@0
|
84 </font><font color="#FF8000">// get the first (only) seed
|
robert@0
|
85 </font><font color="#0000BB">$seed_row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_assoc</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">);
|
robert@0
|
86
|
robert@0
|
87 </font><font color="#FF8000">// fail if no row
|
robert@0
|
88 </font><font color="#007700">if (!</font><font color="#0000BB">$seed_row</font><font color="#007700">) {
|
robert@0
|
89 die(</font><font color="#DD0000">'false|Unknown error (hacking attempt).'</font><font color="#007700">);
|
robert@0
|
90 }
|
robert@0
|
91
|
robert@0
|
92 </font><font color="#FF8000">// if the md5 hashes are equal to those generated by the clientside js
|
robert@0
|
93 </font><font color="#007700">if (</font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">$user_row</font><font color="#007700">[</font><font color="#DD0000">'password'</font><font color="#007700">] . </font><font color="#0000BB">$seed_row</font><font color="#007700">[</font><font color="#DD0000">'seed'</font><font color="#007700">]) == </font><font color="#0000BB">$_GET</font><font color="#007700">[</font><font color="#DD0000">'hash'</font><font color="#007700">]) {
|
robert@0
|
94 </font><font color="#FF8000">// logged in
|
robert@0
|
95 </font><font color="#007700">echo(</font><font color="#DD0000">'true|' </font><font color="#007700">. </font><font color="#0000BB">$user_row</font><font color="#007700">[</font><font color="#DD0000">'fullname'</font><font color="#007700">]);
|
robert@0
|
96
|
robert@0
|
97 </font><font color="#FF8000">// now remove the random key that was made for this request
|
robert@0
|
98 </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">'DELETE FROM s WHERE id=' </font><font color="#007700">. (int)</font><font color="#0000BB">$_GET</font><font color="#007700">[</font><font color="#DD0000">'id'</font><font color="#007700">]);
|
robert@0
|
99 }
|
robert@0
|
100 else
|
robert@0
|
101 {
|
robert@0
|
102 </font><font color="#FF8000">// not logged in.. incorrect password
|
robert@0
|
103 </font><font color="#007700">die(</font><font color="#DD0000">'false|Invalid username and password combination.'</font><font color="#007700">);
|
robert@0
|
104 }
|
robert@0
|
105 }
|
robert@0
|
106 </font><font color="#0000BB">?></font>
|
robert@0
|
107 </font>
|
robert@0
|
108 </code> |