Jump to content


Photo

Need Help With Registration System(Done, but needs expanding)


  • Please log in to reply
1 reply to this topic

#1 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 20 July 2006 - 10:01 AM

Hello, I recently discovered the incredible uses of PHP and followed a tutorial for a login system, it workd great! but the registration system is limited, you only need to supply a username and password... but I want an email field and name field as well, or age, etc. as much as possible, can you edit the following code to allow such fields, and submit them into the Databse, Ive tried and I guess I was doing it wrong..If you could help thatd be great!
((NOTE: I ALREADY HAVE AN EMAIL COLUMN IN MY DATABASE, called 'email'))
~~~~~~~~~~~~~~~~~~~~~~~register.php~~~~~~~~~~~~~~~~~~~~~~~

<?
session_start(); 
include("database.php");

/**
 * Returns true if the username has been taken
 * by another user, false otherwise.
 */
function usernameTaken($username){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from users where username = '$username'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}

/**
 * Inserts the given (username, password) pair
 * into the database. Returns true on success,
 * false otherwise.
 */
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password')";
   return mysql_query($q,$conn);
}

/**
 * Displays the appropriate message to the user
 * after the registration attempt. It displays a 
 * success or failure status depending on a
 * session variable set during registration.
 */
function displayStatus(){
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']){
?>
<meta http-equiv="refresh" content="5;url=index.php">
<body bgcolor=#FOE68C>
<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you will be redirected to HOME in about 5 seconds.</p>

<?
   }
   else{
?>
<meta http-equiv="refresh" content="5;url=register.php">
<body bgcolor=#FOE68C>
<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
Please try again at a later time.</p><p><A HREF=register.php>Try Again</a> You will be redirected to REGISTER in about 5 seconds.

<?
   }
   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])){
/**
 * This is the page that will be displayed after the
 * registration has been attempted.
 */
?>

<html>
<title>Registration Page</title>
<body>

<? displayStatus(); ?>

</body>
</html>

<?
   return;
}

/**
 * Determines whether or not to show to sign-up form
 * based on whether the form has been submitted, if it
 * has, check the database for consistency and create
 * the new account.
 */
if(isset($_POST['subjoin'])){
   /* Make sure all fields were entered */
   if(!$_POST['user'] || !$_POST['pass']){
      die('You didn\'t fill in a required field.');
   }

   /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30){
      die("Sorry, the username is longer than 30 characters, please shorten it.");
   }

   /* Check if username is already in use */
   if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
      die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
   }

   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']); 
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
   $_SESSION['registered'] = true;
$ourFileName = "$_SESSION[reguname].html";
$ourFileHandle = fopen($ourFileName, 'w+') or die("can't create file file");
fclose($ourFileHandle);
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
}
else{
/**
 * This is the page with the sign-up form, the names
 * of the input fields are important and should not
 * be changed.
 */
?>

<html>
<title>Registration Page</title>
<body bgcolor=#FOE68C>
<div align=center>
<h1>Register</h1>
</div>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
<div align=center>
<A HREF=index.php>HOME</a>
</div>
</body>
</html>


<?
}
?>

Link shortener with advanced, detailed statistics:

http://tyny.us/

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 July 2006 - 11:06 AM

To add an email field, find this:
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
And add the following after it:
<tr><td>Email:</td><td><input type="text" name="email" maxlength="50"></td></tr>

Now find the following:
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password')";
   return mysql_query($q,$conn);
}
And replace with this following:
function addNewUser($username, $password, $email){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password', '$email')";
   return mysql_query($q,$conn);
}

Change this:
$_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
to this:
$_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass, $_POST['email']);

Add the following:
/**
 * Returns true if the email address is already
 * in use by another user, false otherwise.
 */
function emailUsed($email){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $email = addslashes($email);
   }
   $q = "select email from users where email = '$email'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}
AFTER this:
function usernameTaken($username){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from users where username = '$username'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}
Now add the following:
/* Check if email is already in use */
   if(emailTaken($_POST['email'])){
      $email = $_POST['email'];
      die("Sorry, the email address (<i>{$email}</i>) provided is already in use");
   }
After:
/* Check if username is already in use */
   if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
      die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
   }

That should be about it. Any trouble please post back.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users