rlm@0: rlm@0: <?php rlm@0: /*********************** rlm@0: Makes use of two MySQL tables. rlm@0: rlm@0: users: rlm@0: CREATE TABLE `users` ( rlm@0:   `id` int(11) NOT NULL auto_increment, rlm@0:   `username` varchar(20) default NULL, rlm@0:   `password` varchar(40) default NULL, rlm@0:   `fullname` varchar(30) default NULL, rlm@0:   PRIMARY KEY  (`id`) rlm@0: ) TYPE=MyISAM rlm@0: rlm@0: seeds: rlm@0: CREATE TABLE `seeds` ( rlm@0:   `id` int(11) NOT NULL auto_increment, rlm@0:   `seed` timestamp(14) NOT NULL, rlm@0:   PRIMARY KEY  (`id`) rlm@0: ) TYPE=MyISAM rlm@0: rlm@0: */ rlm@0: rlm@0: // connect to mysql rlm@0: $mysql mysql_connect('localhost','###USERNAME###','###PASSWORD###'); rlm@0: rlm@0: // fail on database errors rlm@0: if (!$mysql) { rlm@0:     die('false|Could not connect to MySQL'); rlm@0: } rlm@0: rlm@0: // connect to the database rlm@0: mysql_select_db('jamesdam_ajaxlogin'$mysql); rlm@0: rlm@0: // one task of the server is to provide random values to hash with rlm@0: if ($_GET['task']=='getseed') rlm@0: { rlm@0:     mysql_query('INSERT INTO seeds VALUES()'); // insert a new row with default values rlm@0:      rlm@0:     // get the values from the row back rlm@0:     $result mysql_query('SELECT id, seed FROM seeds ORDER BY id DESC LIMIT 1'); rlm@0: rlm@0:     if (!$result) { // fail on error rlm@0:         die('false|'.mysql_error()); rlm@0:     } rlm@0: rlm@0:     $row mysql_fetch_assoc($result); // only one row so take the first row rlm@0:     echo($row['id'].'|'.$row['seed']);  // write back the data in form id|random_value rlm@0: } rlm@0: rlm@0: // the other task of the server is to check a username/password combination rlm@0: rlm@0: else if ($_GET['task']=='checklogin') { rlm@0:     // formulate query for username     rlm@0:     $sql 'SELECT * FROM users WHERE username = \'' mysql_real_escape_string($_GET['username']) . '\''; rlm@0:     $result mysql_query($sql); rlm@0:      rlm@0:     // fail on sql failure rlm@0:     if (!$result)  { rlm@0:         die('false|Could not connect to login database.  Please try again'); rlm@0:     } rlm@0:      rlm@0:     // get the first user with username in the table (should only be one) rlm@0:     $user_row mysql_fetch_assoc($result); rlm@0: rlm@0:     // if there isn't one rlm@0:     if (!$user_row) rlm@0:     { rlm@0:         // then the username doesn't exist, but don't let the user know that this is the problem rlm@0:         // rather inform them more vaguely that the combination is incorrect; prevents someone from rlm@0:         // fishing for valid usernames rlm@0:         die('false|Invalid username and password combination.'); rlm@0:     } rlm@0:      rlm@0:     // formulate query for random timestamp for given id rlm@0:     $sql 'SELECT * FROM seeds WHERE id=' . (int)$_GET['id']; rlm@0:     $result =  mysql_query($sql); rlm@0: rlm@0:     // die if no value for given id rlm@0:     if (!$result) { rlm@0:         die('false|Unknown error (hacking attempt).'); rlm@0:     } rlm@0: rlm@0:     // get the first (only) seed rlm@0:     $seed_row mysql_fetch_assoc($result); rlm@0:      rlm@0:     // fail if no row rlm@0:     if (!$seed_row) { rlm@0:         die('false|Unknown error (hacking attempt).'); rlm@0:     } rlm@0:      rlm@0:     // if the md5 hashes are equal to those generated by the clientside js rlm@0:     if (md5($user_row['password'] . $seed_row['seed']) == $_GET['hash']) { rlm@0:         // logged in rlm@0:         echo('true|' .  $user_row['fullname']); rlm@0: rlm@0:         // now remove the random key that was made for this request rlm@0:         mysql_query('DELETE FROM s WHERE id=' . (int)$_GET['id']);         rlm@0:     } rlm@0:     else rlm@0:     { rlm@0:         // not logged in.. incorrect password rlm@0:         die('false|Invalid username and password combination.'); rlm@0:     } rlm@0: } rlm@0: ?> rlm@0: rlm@0: