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