I've been asked to write a quick signup form for a company. The first page of the signup form asks the user to choose a username, once the username is supplied, the username is passed to a check page where the username is checked against a MySQL database and if the username is available it redirects the user to page 2 where the remaining details can be entered, but if the username is taken, it redirects to an error page and asks the user to re-enter a username before rechecking again.
Sign-up form:
<form method="post" action="check.php">
Step 1 - Username:<br>
<input name="username" type="text" size="20" maxlength="15">
<br>
<br>
<input type="submit" name="Submit" value="Step 2">
</form>
check.php form:
<?php
$mysql = mysql_connect( 'localhost', 'user', 'passwd' );
if(!$mysql)
{
echo 'Cannot connect to the database.';
exit;
}
$mysql = mysql_select_db( 'dbname' );
if(!$mysql)
{
echo 'Cannot select the database.';
exit;
}
$query = "select count(*) from usernames where username = '$username'";
$result = mysql_query( $query );
if(!$result)
{
echo 'Cannot run query.';
exit;
}
$count = mysql_result( $result, 0 );
if ( $count > 0 )
{
// Send user to error page and request username again
header("Location: http://www.domain.co.uk/user_error.php");
$user_error = "The username you chose is already in use. Please try again.";
}
else
{
// Send user to the second signup page
header("Location: http://wwwdomain.co.uk/step_2.php");
}
?>
One problem I am having is that the variable $user_error is not being passed to the second page.
Can anyone give me any pointers or does anyone have a better way of doing this check. I've looked at using cookies, but the company i'm doing this for don't want to use cookies.
Many thanks,
Stuart