I am trying to develop a login/registration function for a website that I am building, and have run into a few snags. The login portion works perfect, and I am now working on the registration part. The first thing I am trying to do is get the system to check and see if the requested username is available, and it tells me that it is not available no matter what I enter into the username text box. My code is below.
As always, any help is greatly appreciated.
Ed.
The registration form block:
<?php require_once($root_dir."membership/membership_functions.php"); ?>
<?php //<form action="<?php echo $root_dir; membership/registration_complete.php" method="post"> ?>
<form action="<?php echo $current_node; ?>" method="post">
<table>
<tr>
<td colspan="3"><h3>Registration</h3></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="reg_user" maxlength="30" value="<?php if ($_POST['submit_reg']) {echo $reg_user; } else {echo htmlentities($reg_user); } ?>" /></td>
<td><?php if(!$user_name_avail) { echo "*"; } ?></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password" maxlength="30" value="<?php echo htmlentities($reg_pass) ?>" /></td>
<td><?php if($user_name_avail) { echo "ok!"; } else { echo "bad!"; } ?></td>
</tr>
<tr>
<td>Retype Password:</td>
<td><input type="text" name="password_check" maxlength="30" value="<?php echo htmlentities($reg_pass_check) ?>" /></td>
<td><?php if($user_name_avail) { echo "ok!"; } else { echo "bad!"; } ?></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" maxlength="30" value="<?php echo htmlentities($reg_email) ?>" /></td>
<td><?php if($user_name_avail) { echo "ok!"; } else { echo "bad!"; } ?></td>
</tr>
<tr>
<td>Retype Email:</td>
<td><input type="text" name="email2" maxlength="30" value="<?php echo htmlentities($reg_email_check) ?>" /></td>
<td><?php if($user_name_avail) { echo "ok!"; } else { echo "bad!"; } ?></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="submit_reg" value="Register"/> </td>
</tr>
</table>
</form>
The membership_functions.php file.
<?php
if (!$root_dir) { $root_dir = "./"; }
require_once($root_dir . "includes/connection.php");
// Registration Functions:
function check_name_available($name) {
return 1;
}
function create_hash($value) {
return hash(whirlpool, $value);
}
function username_available($name) {
$query .= "SELECT username FROM members ";
$query .= "WHERE username = '{$name}'";
$results = mysql_query($query, $mysql_conn);
$rows = mysql_fetch_array($results);
return $rows['username'];
}
if ($_POST['login_submit']) { // Form is posted for login.
$message = "Post Submitted for Login";
// Get Submitted Username and Password.
$username = $_POST['username'];
$password = $_POST['password'];
// Get hash for password.
$hashed_pass = create_hash($password);
// Create query to see if user and password are in database.
$query = "SELECT * FROM members ";
$query .= "WHERE username = '{$username}' ";
$query .= "AND password = '{$hashed_pass}'";
// Get results of query
$results = mysql_query($query, $mysql_conn);
if(mysql_num_rows($results) > 0) { // results were found.
$row = mysql_fetch_array($results);
$_SESSION['user_id'] = $row['id'];
$_SESSION['user_name'] = $row['username'];
}
} elseif ($_POST['submit_reg']) { // This is a registration request.
$message = "Post Submitted for registration";
// Need to verify registration information.
// Check to ensure unique username.
$requested_name = $_POST['reg_user'];
$name_is_available = username_available($requested_name);
if (!$name_is_available) {
//$reg_user = $_POST['reg_user'];
$message .= "<br />Username Unavailable: " . $requested_name;
$message .= "<br />Username Unavailable: " . $name_is_available;
$user_name_avail = false;
} else {
//$reg_user = $_POST['reg_user'];
$user_name_avail = true;
$message .= "<br />Username Available: " . $requested_name;
$message .= "<br />Username Available: " . $name_is_available;
}
} else { // This is the original load.
$message = "Post Submitted for last else";
}
?>

New Topic/Question
Reply




MultiQuote





|