Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start(); // Sessions need to be started before any output and whitespace, to use $_SESSION variables.
- function processPassword($i){ // Once you have hashed passwords with this function, do not alter it, as it will produce different results, which will result in incorrect passwords.
- return sha1(md5($i));
- }
- $message = '';
- if($_SERVER['REQUEST_METHOD'] === 'POST'){
- $host = "localhost"; // Host name
- $username = "****"; // Mysql username
- $password = "********"; // Mysql password
- $db_name = "mammoth8_user"; // Database name
- $tbl_name = "members"; // Table name
- // Connect to server and select databse.
- $conn = mysql_connect($host, $username, $password) or die("cannot connect"); // Storing the link is needed to close the connection when the script is complete. Or the queries are complete on long scripts. Closing the connection allows for system resources to be freed for other scripts.
- mysql_select_db($db_name, $conn) or die("cannot select DB");
- // To protect MySQL injection (more detail about MySQL injection)
- if(get_magic_quotes_gpc()){ // returns true if Magic Quotes are turned on on your server
- $uname = mysql_real_escape_string(stripslashes(trim($_POST['uname']))); // Trim to get the whitespace off the ends of the input.
- $upass = processPassword(stripslashes($_POST['upass']));
- }else{
- $uname = mysql_real_escape_string(trim($_POST['uname']));
- $upass = processPassword($_POST['upass']);
- }
- if(strlen($uname) === 0 || strlen($upass) === 0){
- $message = 'A Username and Password are Required for Registration';
- }else if(mysql_num_rows(mysql_query("SELECT username FROM {$tablename} WHERE username='{$uname}' LIMIT 1")) > 0){ // Check to see if the username already exists
- $message = 'Username already Exists. Please try another selection.';
- }else{
- if(mysql_query("INSERT INTO {$tablename}(username,password) VALUES('{$uname}','{$upass}')")){
- $message = 'You have been Successfully Registered with:<br />Username: [' . $_POST['uname'] . ']<br />Password: [' . $_POST['upass'] . ']<br /><a href="login.php">Login Here</a>';
- }else{
- $message = 'Internal Server Error. Please Try Again Later';
- }
- }
- mysql_close($conn);
- }
- echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>Registration</title>
- </head>
- <body>
- <span>' . $message . '</span><br />
- <form method="POST">
- Username: <input type="text" name="uname" /><br />
- Password: <input type="password" name="upass" /><br />
- <input type="submit" value="Register" />
- </form>
- ';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment