User Registration script problem.
Posted: Sat Sep 13, 2003 2:27 pm
I have a user registration, which was working. In the working version, I was creating variables with $foo = $_POST['foo'], but because of the cumbersome nature of doing this, I used an include that turns the POST array variables back into variables like $foo. Since doing that, my registration script won't work. I'm getting a "cannot send header..." error.
Can someone take a look? the include files, var.php, is just a list/each, the common-db contains the connection script, header and footer functions, and a user access_status function, I don't think they are the problem. I'm a newb, so if the code is convoluted (and long), I apologize in advance.
thanks, Tomasz.
Can someone take a look? the include files, var.php, is just a list/each, the common-db contains the connection script, header and footer functions, and a user access_status function, I don't think they are the problem. I'm a newb, so if the code is convoluted (and long), I apologize in advance.
thanks, Tomasz.
Code: Select all
<?php
include("includes/common-db.php");
include("includes/vars.php");
function inuse($userid)
{
global $userid,$user_tablename, $error;
$query = "SELECT userid FROM user WHERE userid = '$userid'";
$result = mysql_query($query);
if(!mysql_num_rows($result)) return 0;
else return 1;
}
function confirmation_mail($userid, $username, $useremail, $userpassword)
{
$mail_to = $useremail;
$mail_subject = "Your JibberJabber Registration";
$mail_body = "$username, thank you for registering with JibberJabber.com.\n\n";
$mail_body .= "Keep this record of your User I.D. and Password.\n\n";
$mail_body .= "User I.D: $userid\n";
$mail_body .= "Password: $userpassword.\n\n";
$mail_body .= "To change your account info, go to http://localhost/beg-php/sample-db-p411/index.php and view your account.";
if(mail($mail_to, $mail_subject, $mail_body))
{
$confirmation_msg = "A confirmation message has been mailed to $useremail.";
}
else
{
echo "Failed to send confirmation message. Please <a href='contact.php'>contact</a> JibberJabber.com if you have trouble logging in on your next visit";
}
}
function register_form()
{
global $userid, $username,$userpassword,$userpassword2,$useremail,
$address1, $address2, $usercity, $userstate, $usercountry, $userprofile;
?>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="20%" height="100" align="left" valign="top" style="background-color:#cccccc">
</td>
<td width="80%" align="left" valign="top" style="background-color: #cccccc">
<?php access_status();?>
<h1 style="color:$ffffff; font-size:28px;background-color:transparent;">JibberJabber.com</h1>
</td>
</tr>
<tr>
<td height="400" valign="top">
<p class="leftNav"><a href='jibber.php'>New JibberJabber</a><br>
<a href='jabber.php'>Top 10 Jabber</a><br>
<a href='reader-jabber.php'>Speak</a><br>
<a href='contact.php'>Contact</a><br>
<a href='index.php'>Home</a></p>
</td>
<td valign="top">
<h2>User Registration</h2>
<?php echo $error;?>
<!--Registration form and table-->
<div style="margin-left: 10px;">
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="hidden" name="action" value="register">
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="left">
User I.D:
</td>
<td>
<input type="text" name="userid" value="<?php echo $userid;?>" size="24">
</td>
</tr>
<tr>
<td>
Real name:
</td>
<td>
<input type="text" name="username" value="<?php echo $username;?>" size="24">
</td>
</tr>
<tr>
<td>
Password (between 4 and 8 characters):
</td>
<td>
<input type="password" name="userpassword" size="24" maxlength="8">
</td>
</tr>
<tr>
<td>
Retype Password:
</td>
<td>
<input type="password" name="password_check" size="24" maxlength="8">
</td>
</tr>
<tr>
<td align="left">
E-mail:
</td>
<td>
<input type="text" name="useremail" value="<?php echo $useremail;?>" size="24">
</td>
</tr>
<tr>
<td align="left">
Address:
</td>
<td align="left">
<input type="text" name="address1" value="<?php echo $address1;?>" size="24">
</td>
</tr>
<tr>
<td align="left">
Address: (if you need more room)
</td>
<td align="left">
<input type="text" name="address2" size="24" value="<?php echo $address2;?>">
</td>
</tr>
<tr>
<td align="left">
City:
</td>
<td align="left">
<input type="text" name="usercity" size="24" value="<?php echo $usercity;?>">
</td>
</tr>
<tr>
<td align="left">
Province/State:
</td>
<td align="left">
<select name="userstate">
<!--snipped a big options list for ease of reading-->
</select>
</td>
</tr>
<tr>
<td align="left">
Country:
</td>
<td align="left">
<select name="usercountry">
<option>Canada</option>
<option>United States</option>
</td>
</tr>
<tr>
<td align="left">
Your profile:<br>
(Say something about yourself)
</td>
<td align="left">
<textarea name="userprofile" cols="30" rows="5"></textarea>
</td>
</tr>
<tr>
<td align="left">
</td>
<td align="left">
<input type="submit" value="Register">
</td>
</tr>
</table>
</form>
</div>
</td>
</tr>
</table>
<?php
// end function register_form
}
function create_account()
{
session_start();
header("Cache-control:private");
// these globals make the variables available to db_connect
// and inside the input values for the successful registration message.
global $userid, $username,$userpassword,$userpassword2,$useremail,
$address1, $address2, $usercity, $userstate, $usercountry, $userprofile;
global $user_tablename, $default_dbname, $error;
if(empty($userid)) $error .= "Enter your desired User I.D.";
$link_id = db_connect();
if(inuse($userid))
{
$error = "<p class='error'>Sorry, <strong class='error'>$userid is in use</strong>, please choose a different User I.D.</p>";
html_header('JibberJabber.com User Registration: Error. Choose another User I.D.', 'global.css');
register_form();
html_footer();
}
else
{
$query = "INSERT INTO user VALUES(NULL,'$userid', md5('$userpassword'),
'$username','$address1','$address2','$usercity',
'$userstate','$usercountry','$useremail','userprofile',
curdate(),NULL)";
$result = mysql_query($query);
if(!result) sql_error();
$usernumber = mysql_insert_id($link_id);
confirmation_mail($userid, $username, $useremail, $userpassword);
$_SESSION['userid'] = $userid;
header("location: myaccount.php");
}
?>
<?php
html_footer();
} // end create_account
/*the variable $action has been set in a hidden input so it is available
to perform the switch. When we first enter, the $action variable is empty
so the defaults html_header, register_form, and html_footer will run,
displaying the registration form. After the registration form is submitted
the action will equal "register" (cuz of the value of the hidden input named 'action')
thus the create_account function will run.*/
switch($action) {
case "register":
create_account();
break;
default:
html_header('JibberJabber.com User Registration', 'global.css');
register_form();
html_footer();
break;
}
?>