This is a little class I wrote that will allow you to do user authorization on your site. It includes the ability to mail the user first and have them follow a link back to verify their signup. This includes a small db class to support the authorize class.
By : Matt
<? class db {
var $db_type; var $db_server; var $db_name; var $db_user; var $db_pass; var $db_persistent; var $dbh;
function createUser($uname,$pword,$email) { srand(make_seed()); $randval = rand(); $query = "INSERT authorize(username,password,accesslevel,email,id) VALUES ('" . $uname . "','" . crypt($pword,$this->salt) . "',0,'" . $email ."','" . $randval . "')"; $result = $this->db->db_query($query); $message = "This message has been sent to you because you requested a login for mysite.com.\n\n"; $message .= "Please use the following URL to verify your email address and be added to the userlist.\n\n"; $message .= "http://mysite.com/newuser.php?email=" . $email . "&id=" . $randval . "\n\n"; $message .= "Please note that if you have recieved this message in error, or you do not want to sign up, you do not need to do anything.\nYou will not be added to the listing unless you use the proceeding URL.\n\n"; $message .= "Thanks for visiting our site!\n"; mail($email, "mysite.com - account confirmation", $message, "From: register@mysite.com");
}
function checkUsername($uname) { $query = "SELECT * FROM users where username='" . $uname ."'"; $result = $this->db->db_query($query); if($this->db->db_numrows($result) > 0) { return 0; } else { return 1; } }
} //end class authenticate ?> ####table structures CREATE TABLE authorize ( username varchar(15) NOT NULL default '', password varchar(20) NOT NULL default '', accesslevel tinyint(4) NOT NULL default '0', email varchar(30) NOT NULL default '', id varchar(30) NOT NULL default '', PRIMARY KEY (username) ) TYPE=MyISAM; CREATE TABLE users ( user_id int(10) unsigned NOT NULL auto_increment, username varchar(15) NOT NULL default '', password varchar(20) NOT NULL default '', accesslevel tinyint(4) NOT NULL default '0', email varchar(30) NOT NULL default '', PRIMARY KEY (username), KEY user_id (user_id) ) TYPE=MyISAM;
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.