Forum Moderators: open
<form action="index_v2.php" method="post">
<h1>Sign Up</h1>
<h2>connecting people</h2>
<table width="100%" border="0">
<tr>
<td>First Name</td>
<td><input type="text" name="first_name" size="20" maxlength="20" value="<?php echo (isset($_POST['first_name']) ? $_POST['first_name'] : ''); ?>" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="last_name" size="20" maxlength="40" value="<?php echo (isset($_POST['last_name']) ? $_POST['last_name'] : ''); ?>" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" size="30" maxlength="80" value="<?php echo (isset($_POST['email']) ? $_POST['email'] : ''); ?>" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" size="20" maxlength="20" /></td>
</tr>
<tr>
<td>Birthday</td>
<td>
<?php
include("bdayselect/DateDDLGenerator.class.php");
$ddl = new DateDDLGenerator;
$ddl_name_Change = new DateDDLGenerator;
$ddl_name_Change->setToCurrentDay();
print $ddl_name_Change->genDayDDL("day");
print $ddl_name_Change->genMonthDDL("month");
print $ddl_name_Change->genYearDDL("year");
?>
</td>
<tr>
<td>I am</td>
<td><select name="sex" class="select" id="sex" >
<option value="0">-Select-</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</td></tr>
<tr>
<td></td>
<td><su><input type="submit" name="submit" value="Sign-Up" /></su></td>
</tr>
</td>
</tr>
</table>
<input type="hidden" name="submitted" value="TRUE" /></p>
</form>
<?php
// This is the registration page for
require_once ('config.inc.php');
$page_title = 'Register';
if (isset($_POST['submitted'])) { // Handle the form.
require_once (MYSQL);
// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);
// Assume invalid values:
$fn = $ln = $e = $p = FALSE;
// Check for a first name:
if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) {
$fn = mysqli_real_escape_string ($dbc, $trimmed['first_name']);
} else {
echo '<p class="error">Please enter your first name!</p>';
}
// Check for a last name:
if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) {
$ln = mysqli_real_escape_string ($dbc, $trimmed['last_name']);
} else {
echo '<p class="error">Please enter your last name!</p>';
}
// Check for an email address:
if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
$e = mysqli_real_escape_string ($dbc, $trimmed['email']);
} else {
echo '<p class="error">Please enter a valid email address!</p>';
}
// Check for a password and match against the confirmed password:
if (preg_match ('/^\w{4,20}$/', $trimmed['password'])) {
$p = mysqli_real_escape_string ($dbc, $trimmed['password']);
} else {
echo '<p class="error">Please enter a valid password!</p>';
}
if ($fn && $ln && $e && $p) { // If everything's OK...
// Make sure the email address is available:
//$q = "SELECT user_id FROM users WHERE email='$e'"; <--original of below
$q = "SELECT `user_id` FROM `users` WHERE `email` ='".$e."' LIMIT 1";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_num_rows($r) == 0) { // Available.
// Create the activation code:
$a = md5(uniqid(rand(), true));
// Add the user to the database:
$q = "INSERT INTO `users` (`email`, `pass`, `first_name`, `last_name`, `active`, `registration_date`) VALUES ('".$e."', SHA1('".$p."'), '".$fn."', '".$ln."', '".$a."', NOW() )";
/* $q = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() )"; */
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
// Send the email:
$body = "Thank you for registering at <YOUR SITE NAME>. To activate your account, please click on this link:\n\n";
$body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
mail($trimmed['email'], 'Registration Confirmation', $body, 'From: you@youremail.com');
// Finish the page:
echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>';
//include ('includes/footer.html'); // Include the HTML footer.
exit(); // Stop the page.
} else { // If it did not run OK.
echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
}
} else { // The email address is not available.
echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>';
}
} else { // If one of the data tests failed.
/*echo '<p class="error">Please re-enter your passwords and try again.</p>'; */
}
mysqli_close($dbc);
}
// End of the main Submit conditional.
?>
Wouldn't it be better to have one field e.g. birthday and have the users birthday inserted into that field as dd-mm-yyyy?
some people suggest 3 different fields in database for birthday e.g. day month and year.
....have the users birthday inserted into that field as dd-mm-yyyy?
As for gender selection some people are saying enum should be used and some are saying char?
<?php
class DateDDLGenerator{
var $intDay;
var $intMonth;
var $intYear;
var $bolSetToCurrentDay;
function DateDDLGenerator(){
$this->bolSetToday = false;
$this->intDay = date("d");
$this->intMonth = date("m");
$this->intYear = date("Y");
}
function setToCurrentDay(){
$this->bolSetToCurrentDay = true;
}
#Generate Year range
function genYearDDL($selName = 'Year', $yearCount = 110, $year = ''){
/*
Check if the year passed in is the same as current year.
If the year got is not given or same as current year, the list
will select the current year by default. Otherwise, $yearSelect
will be set to what user entered.
*/
$yearSelect = $year == '' ? date("Y") : $year;
/*
$yearCount: it is the length of your drop down list, i.e. how many
years do you want to show. It is 50 by default, which shows 50 years
from now.
*/
$str = "<select name='$selName'>\n";
for($i = $yearSelect; $i >= ($yearSelect - $yearCount); $i--){
if($this->bolSetToCurrentDay == true){
$selected = $this->intYear == $i ? 'selected="selected"' : '';
}
$str .= "\t<option value='$i' $selected>$i</option>\n";
}
$str .= "</select>\n";
print $str;
}
#Generate month range from 1 to 12
function genMonthDDL($selName = 'Month', $date_format = 'short'){
$shortM = array(1 => "Jan", "Feb", "Mar",
"Apr", "May", "Jun",
"Jul", "Aug", "Sep",
"Oct", "Nov", "Dec");
$longM = array(1 => "January", "February", "March",
"April" , "May" , "June" ,
"July" , "Aug" , "September",
"October", "November", "December");
$str = "<select name='$selName'>\n";
if($date_format == 'short'){
for($i = 1; $i <= 12; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intMonth == $i ? 'selected="selected"' : '';
}
$str .= "\t<option value='$i' $selected>".$shortM[$i]."</option>\n";
}
}elseif($date_format == 'long'){
for($i = 1; $i <= 12; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intMonth == $i ? 'selected="selected"' : '';
}
$str .= "\t<option value='$i' $selected>".$longM[$i]."</option>\n";
}
}
$str .= "</select>\n";
print $str;
}
#Generate day range from 1 to max days of relevant month
function genDayDDL($selName = 'Day'){
$str = "<select name='$selName'>\n";
//Thanks to Peter K on this improvement and now this method support leap year
if ($this->intMonth == 2) {// February ?
$leap_day = 0;
if ($this->intYear >= 4 && $this->intYear % 4 == 0) {// Leap year ?
if ($this->intYear >= 1800 && $this->intYear % 100 == 0) {// No accurate leap centuries before that
if (($this->intYear / 100) % 4 == 0)
$leap_day = 1;
} else
$leap_day = 1;
}
$max_days = 28 + $leap_day;
} else if ($this->intMonth == 4 || $this->intMonth == 6 ||
$this->intMonth == 9 || $this->intMonth == 11)
$max_days = 30;
else
$max_days = 31;
for($i = 1; $i <= $max_days; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intDay == $i ? 'selected="selected"' : '';
}
$str .= "\t<option value='$i' $selected>$i</option>\n";
}
$str .= "</select>\n";
print $str;
}
}
?>
// Check gender is selected:
if (preg_match ('/^\w{4,20}$/', $trimmed['sex'])) {
$s = mysqli_real_escape_string ($dbc, $trimmed['sex']);
} else {
echo '<p class="error">choose sex!</p>';
}
// Add the user to the database:
$q = "INSERT INTO `users` (`email`, `pass`, `first_name`, `last_name`, `sex`, `active`, `registration_date`) VALUES ('".$e."', SHA1('".$p."'), '".$fn."', '".$ln."', '".$a."', '".$s."', NOW() )";
/* $q = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() )"; */
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
$q = "INSERT INTO `users` (`email`, `pass`, `first_name`, `last_name`, `active`, `registration_date`, `sex`, `birthday`) VALUES ('".$e."', SHA1('".$p."'), '".$fn."', '".$ln."', '".$a."', NOW(), '".$_POST['sex']."','".$_POST['$ddl']."' )"; and everything inserts correctly except for birthday which now inserts as 0000-00-00
$str = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
$q = "INSERT INTO `users` (`email`, `pass`, `first_name`, `last_name`, `active`, `registration_date`, `birthday`, `sex`) VALUES ('".$e."', SHA1('".$p."'), '".$fn."', '".$ln."', '".$a."', NOW(), '".$str."', '".$_POST['sex']."')";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); <?php
class DateDDLGenerator{
var $intDay;
var $intMonth;
var $intYear;
var $bolSetToCurrentDay;
function DateDDLGenerator(){
$this->bolSetToday = false;
$this->intDay = date("d");
$this->intMonth = date("m");
$this->intYear = date("Y");
}
function setToCurrentDay(){
$this->bolSetToCurrentDay = true;
}
#Generate Year range
function genYearDDL($selName = 'Year', $yearCount = 110, $year = ''){
/*
Check if the year passed in is the same as current year.
If the year got is not given or same as current year, the list
will select the current year by default. Otherwise, $yearSelect
will be set to what user entered.
*/
$yearSelect = $year == '' ? date("Y") : $year;
/*
$yearCount: it is the length of your drop down list, i.e. how many
years do you want to show. It is 50 by default, which shows 50 years
from now.
*/
$str = "<select name='$selName'>\n";
for($i = $yearSelect; $i >= ($yearSelect - $yearCount); $i--){
if($this->bolSetToCurrentDay == true){
$selected = $this->intYear == $i ? 'selected="selected"' : '';
}
$str .= "\t<option value='$i' $selected>$i</option>\n";
}
$str .= "</select>\n";
print $str;
}
#Generate month range from 1 to 12
function genMonthDDL($selName = 'Month', $date_format = 'short'){
$shortM = array(1 => "Jan", "Feb", "Mar",
"Apr", "May", "Jun",
"Jul", "Aug", "Sep",
"Oct", "Nov", "Dec");
$longM = array(1 => "January", "February", "March",
"April" , "May" , "June" ,
"July" , "Aug" , "September",
"October", "November", "December");
$str = "<select name='$selName'>\n";
if($date_format == 'short'){
for($i = 1; $i <= 12; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intMonth == $i ? 'selected="selected"' : '';
}
$str .= "\t<option value='$i' $selected>".$shortM[$i]."</option>\n";
}
}elseif($date_format == 'long'){
for($i = 1; $i <= 12; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intMonth == $i ? 'selected="selected"' : '';
}
$str .= "\t<option value='$i' $selected>".$longM[$i]."</option>\n";
}
}
$str .= "</select>\n";
print $str;
}
#Generate day range from 1 to max days of relevant month
function genDayDDL($selName = 'Day'){
$str = "<select name='$selName'>\n";
//on this improvement and now this method support leap year
if ($this->intMonth == 2) {// February ?
$leap_day = 0;
if ($this->intYear >= 4 && $this->intYear % 4 == 0) {// Leap year ?
if ($this->intYear >= 1800 && $this->intYear % 100 == 0) {// No accurate leap centuries before that
if (($this->intYear / 100) % 4 == 0)
$leap_day = 1;
} else
$leap_day = 1;
}
$max_days = 28 + $leap_day;
} else if ($this->intMonth == 4 || $this->intMonth == 6 ||
$this->intMonth == 9 || $this->intMonth == 11)
$max_days = 30;
else
$max_days = 31;
for($i = 1; $i <= $max_days; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intDay == $i ? 'selected="selected"' : '';
}
$str .= "\t<option value='$i' $selected>$i</option>\n";
}
$str .= "</select>\n";
print $str;
}
}
?>