In order to accomplish the task of adding new control input element we will modified theses files:
[*] register.php
[*] process.php
[*] session.php
[*] database.php
Also we will need to alter the current users table to accept the new field created.
1- Altering the sql table
Usually, to accomplish that task I use phpmyadmin, very simple to use...
Just execute this query.
- Code:
ALTER TABLE `users` ADD `firstname` VARCHAR( 32 ) NOT NULL ;
2- Edit register.php and add the new control input element into the form.
- Code:
<!-- this is the new part -->
<tr>
<td>Firstname:</td>
<td><input type="text" name="firstname" maxlength="32" value="<? echo $form->value("firstname"); ?>"></td>
<td><? echo $form->error("firstname"); ?></td>
</tr>
<!-- end new part -->
- Code:
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>Username:</td>
<td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td>
<td><? echo $form->error("user"); ?></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td>
<td><? echo $form->error("pass"); ?></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" name="email" maxlength="50" value="<? echo $form->value("email"); ?>"></td>
<td><? echo $form->error("email"); ?></td>
</tr>
<!-- this is the new part -->
<tr>
<td>Firstname:</td><td><input type="text" name="firstname" maxlength="32" value="<? echo $form->value("firstname"); ?>"></td>
<td><? echo $form->error("firstname"); ?></td>
</tr>
<tr>
<!-- end new part -->
<td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1">
<input type="submit" value="Join!">
</td>
</tr>
<tr>
<td colspan="2" align="left"><a href="main.php">Back to Main</a>
</td>
</tr>
</table>
</form>
3- Edit process.php and find the function call procRegister().
Add the posted value $_POST[‘firstname’] as a new argument to the $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['firstname']);.
- Code:
/* Registration attempt */
$retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['firstname']);
4.1- Edit session.php and find the function call register()
Make sure to add the new $argument to that function
- Code:
function register($subuser, $subpass, $subemail, $subfirstname){
4.2- Somewhere in the function you will need to check the for value error.
- Code:
/*Firstname error checking */
$field = "firstname";
if(!$subfirstname || strlen($subfirstname = trim($subfirstname)) == 0) $form->setError($field, "* firstname not entered");
$subfirstname = stripslashes($subfirstname);
4.3- Then, if no error is found in the form you will send the info to the DB using $database->addnewuser() class function.
- Code:
$database->addNewUser($subuser, md5($subpass), $subemail, $subfirstname))
5- Edit database.php and find the function call addNewUser().
- Code:
/**
* addNewUser - Inserts the given (username, password, email)
* info into the database. Appropriate user level is set.
* Returns true on success, false otherwise.
*/
function addNewUser($username, $password, $email, $firstname){
$time = time();
/* If admin sign up, give admin user level */
if(strcasecmp($username, ADMIN_NAME) == 0){
$ulevel = ADMIN_LEVEL;
}else{
$ulevel = USER_LEVEL;
}
$q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '0', $ulevel, '$email', $time, '$firstname')";
return mysql_query($q, $this->connection);
}
that's it your done...
Last edited by Fred-Eric on Sat Mar 14, 2009 7:47 pm; edited 5 times in total