Jump to content

Password conformation


cfgcjm

Recommended Posts

I have a registration script and i'd like to add a confirm password field. I added the field but i'm not sure where & how to add the Password=password2 code as a have quite a few else statements. I would like to redirect the user to another page if the 2 passwords do not match. If possible i would like to also unhide a layer containing a green checkmark aside of the password confirm box (if they match) as soon as the confirm password is entered. Dont know if thats possible...

 

Here's my code

<?php
/* Registration Script*/ 
require ('mysql.php');

if (isset ($_POST['submit'])) { // Check to see if the form has been submitted
$username = $_POST['username'];
$password = md5 ($_POST['password']); // MD5 encrypt the password so it is more secure
$regkey   = $_POST['regkey'];

// See if the key is valid
$sql = "SELECT * FROM reg_keys WHERE regkey='$regkey' LIMIT 1";
if ($r = mysql_query ($sql)) {
	$num = mysql_num_rows ($r);

	if ($num > 0) { // if there is a row with that key, the key is valid
		// The key is valid, add user to the database

		$add_sql = "INSERT INTO users (users_id, username, password, regkey) VALUES (0, '$username', '$password', '$regkey')";
		// We make the first value (for users_id) 0 because it is set to auto increment, 
		// so the users_id will be assigned to the next available number

		if ($add_r = mysql_query ($add_sql)) {
			// The user successfully registered

			print 'Thank you for registering!';

			// Delete the key from the key database, so it cannot be used again

			mysql_query ("DELETE FROM reg_keys WHERE regkey='$regkey'");
		} else {
			// The user did not successfully register

			print 'Error:' . mysql_error(); // print the MySQL error
		}

	} else {
		// The key is not valid

		print 'The registration key you entered was not valid!';
	}
} else {
	// The regkey check query failed

	print 'Error:' . mysql_error(); // print the MySQL error
}
} else { // The form was not submitted
// Display the form
print '<table height="100%" align="center" valign="center">
<td>
<form action="register.php" method="post">
<table border="0">
	<tr>
	  <td align="right" style="color: #fff;">Username:</td>
	  <td align="center"><input type="text" name="username" /></td>
	</tr>
	<tr>
	  <td align="right" style="color: #fff;">Password:</td>
	  <td align="center"><input type="password" name="password" /></td>
	</tr>
	<tr>
	  <td align="right" style="color: #fff;">Confirm Password:</td>
	  <td align="center"><input type="password" name="password2" /></td>
	</tr>

	<tr>
	 <td align="right" style="color: #fff;">Registration Key:</td>
	  <td align="center"><input type="text" name="regkey" /></td>
	</tr>
	<tr>
	  <td></td>
	  <td align="center"><input type="submit" name="submit" value="Register" /></td>
	</tr>
</table>
</form>';
}
?>

Link to comment
Share on other sites

<?php
/* Registration Script*/ 
require ('mysql.php');

if (isset ($_POST['submit'])) { // Check to see if the form has been submitted
$errorhandler = '';
if ($_POST['username'] == '') {
$errorhandler .= 'username is required.<br />';
}
if ($_POST['password'] == '') {
$errorhandler .= 'password is required.<br />';
}
if ($_POST['password'] != $_POST['password2']) {
$errorhandler .= 'Passwords must match';
}
if ($errorhandler != '') {
echo '<span>';
echo $errorhandler;
echo '</span>';
}
if ($errorhandler == '') {
$username = $_POST['username'];
$password = md5 ($_POST['password']); // MD5 encrypt the password so it is more secure
$regkey   = $_POST['regkey'];

// See if the key is valid
$sql = "SELECT * FROM reg_keys WHERE regkey='$regkey' LIMIT 1";
if ($r = mysql_query ($sql)) {
	$num = mysql_num_rows ($r);

	if ($num > 0) { // if there is a row with that key, the key is valid
		// The key is valid, add user to the database

		$add_sql = "INSERT INTO users (users_id, username, password, regkey) VALUES (0, '$username', '$password', '$regkey')";
		// We make the first value (for users_id) 0 because it is set to auto increment, 
		// so the users_id will be assigned to the next available number

		if ($add_r = mysql_query ($add_sql)) {
			// The user successfully registered

			print 'Thank you for registering!';

			// Delete the key from the key database, so it cannot be used again

			mysql_query ("DELETE FROM reg_keys WHERE regkey='$regkey'");
		} else {
			// The user did not successfully register

			print 'Error:' . mysql_error(); // print the MySQL error
		}

	} else {
		// The key is not valid

		print 'The registration key you entered was not valid!';
	}
} else {
	// The regkey check query failed

	print 'Error:' . mysql_error(); // print the MySQL error
}
}
} else { // The form was not submitted
// Display the form
print '<table height="100%" align="center" valign="center">
<td>
<form action="register.php" method="post">
<table border="0">
	<tr>
	  <td align="right" style="color: #fff;">Username:</td>
	  <td align="center"><input type="text" name="username" /></td>
	</tr>
	<tr>
	  <td align="right" style="color: #fff;">Password:</td>
	  <td align="center"><input type="password" name="password" /></td>
	</tr>
	<tr>
	  <td align="right" style="color: #fff;">Confirm Password:</td>
	  <td align="center"><input type="password" name="password2" /></td>
	</tr>

	<tr>
	 <td align="right" style="color: #fff;">Registration Key:</td>
	  <td align="center"><input type="text" name="regkey" /></td>
	</tr>
	<tr>
	  <td></td>
	  <td align="center"><input type="submit" name="submit" value="Register" /></td>
	</tr>
</table>
</form>';
}
?>

I put 2 seconds into that to give you an example, base it off of that.

Just reformat it, and clean it up some

I was in a hurry,so I just provided a basic example.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.