PDA

View Full Version : PHP Registration Form help


PRodgers4284
01-30-2008, 02:52 PM
I am looking for a registration form i could use for a website to enable users to sign up to my website. I have difficulty finding a good script that validates the form properly before it can be submitted. I am looking for something similiar to this example:

http://www.x-code.com/vdaemon/samples/registration_f.php

Can anyone please help me?

Thanks

Philip

NeilT
01-30-2008, 03:05 PM
Hello PRodgers4284,

Have you looked at hotscripts.com yet? If you look hard enough you can usualy find something that you can use. Look through this list and you may find somthing: http://www.hotscripts.com/PHP/Scripts_and_Programs/User_Authentication/index.html

In order to help you further (beyond just pointing you to a list of scripts) I would like to know a little bit information. If it's not to much trouble please answer these questions. Do you know any PHP? Do you have acess to a database or will you being using files to hold user information? Do you already have a working script but it just does not validate user input to your desired level?

PRodgers4284
01-30-2008, 03:21 PM
Hello PRodgers4284,

Have you looked at hotscripts.com yet? If you look hard enough you can usualy find something that you can use. Look through this list and you may find somthing: http://www.hotscripts.com/PHP/Scripts_and_Programs/User_Authentication/index.html

In order to help you further (beyond just pointing you to a list of scripts) I would like to know a little bit information. If it's not to much trouble please answer these questions. Do you know any PHP? Do you have acess to a database or will you being using files to hold user information? Do you already have a working script but it just does not validate user input to your desired level?

I know the basics of php, havent been using it for too long, i studied it in a module i had done last year at uni.
I have access to a database, i am using wamp and i have the database setup.
I have a registration script that adds to the database and the user can log in with the data that has been entered. The script only validates when the user has not completed one of the fields but does not validate each field individually. The error message tells the user that one of the fields is missing.

Thanks

Philip

NeilT
01-30-2008, 03:47 PM
Sounds like the best way to go about this then will just be adding to your code. Do you mind sharing it with us?

PRodgers4284
01-30-2008, 04:28 PM
code is

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

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);
}

function emailTaken($email){
global $conn;
if(!get_magic_quotes_gpc()){
$email = addslashes($email);
}
$q = "select username from users where email = '$email'";
$result = mysql_query($q,$conn);
return (mysql_numrows($result) > 0);
}


function addNewUser($username, $password, $forename, $surname, $email){
global $conn;
$q = "INSERT INTO users VALUES ('$username', '$password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location')";
return mysql_query($q,$conn);
}

function displayStatus(){
$uname = $_SESSION['reguname'];

if($_SESSION['regresult']){
?>

<h1>Registered!</h1>
<p>Thank you <b><?php echo $uname;?></b>, your information has been added to the
database, you may now <a href="index.php" title="Login">log in</a>.</p>

<?php
}
else{
?>

<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>

<?php
}
unset($_SESSION['reguname']);
unset($_SESSION['regforename']);
unset($_SESSION['regsurname']);
unset($_SESSION['regemail']);
unset($_SESSION['regmobile']);
unset($_SESSION['regdob']);
unset($_SESSION['reglocation']);
unset($_SESSION['registered']);
unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])){
?>

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

<?php displayStatus(); ?>

</body>
</html>

<?php
return;
}

if(isset($_POST['subjoin'])){
/* Make sure all fields were entered */
if(!$_POST['user'] || !$_POST['pass'] || !$_POST['forename'] || !$_POST['surname'] || !$_POST['email'] || !$_POST['mobile'] || !$_POST['dob'] || !$_POST['location'] ){
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.");
}

/* Check if email is already in use */
if(emailTaken($_POST['email'])){
$use = $_POST['email'];
die("Sorry, the email: <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['regforname'] = $_POST['forename'];
$_SESSION['regsurname'] = $_POST['surname'];
$_SESSION['regemail'] = $_POST['email'];
$_SESSION['regemail'] = $_POST['mobile'];
$_SESSION['regemail'] = $_POST['dob'];
$_SESSION['regemail'] = $_POST['location'];
$_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass, $_POST['forename'], $_POST['surname'], $_POST['email'], $_POST['mobile'], $_POST['dob'], $_POST['location']);
$_SESSION['registered'] = true;
echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
return;
}
else{
?>

PRodgers4284
01-30-2008, 04:38 PM
form is

<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3" width="568" id="table15">
<tr><td width="216"><span class="navyboldtxt">Username:</span></td>
<td align="left" width="340"><input type="text" name="user" maxlength="30" size="35"></td></tr>

<tr><td width="216"><span class="navyboldtxt">Password:</span></td><td align="left" width="340">
<input type="password" name="pass" maxlength="30" size="36"></td></tr>

<tr><td width="216"><span class="navyboldtxt">Forename:</span></td><td align="left" width="340">
<input type="text" name="forename" maxlength="30" size="35"></td></tr>

<tr><td width="216"><span class="navyboldtxt">Surname:</span></td><td align="left" width="340">
<input type="text" name="surname" maxlength="30" size="35"></td></tr>

<tr><td width="216"><span class="navyboldtxt">Email</span></td><td align="left" width="340">
<input type="text" name="email" maxlength="30" size="35"></td></tr>

<tr><td width="216"><span class="navyboldtxt">Mobile</span></td><td align="left" width="340">
<input type="text" name="mobile" maxlength="30" size="35"></td></tr>

<tr><td width="216"><span class="navyboldtxt">DOB</span></td><td align="left" width="340">
<input type="text" name="dob" maxlength="30" size="35"></td></tr>

<tr><td width="216"><span class="navyboldtxt">Location</span></td><td align="left" width="340"><select name="location">
<option VALUE="1">Antrim
<option VALUE="2">Armagh
<option VALUE="3">Belfast
<option VALUE="4">Banbridge
</select></td></tr>
<tr><td colspan="2" align="right">


<input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</div>
</div>

NeilT
01-30-2008, 08:01 PM
Ok, So this is the part of the code we will want to look at:

if(isset($_POST['subjoin'])){
/* Make sure all fields were entered */
if(!$_POST['user'] || !$_POST['pass'] || !$_POST['forename'] || !$_POST['surname'] || !$_POST['email'] || !$_POST['mobile'] || !$_POST['dob'] || !$_POST['location'] ){
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.");
}

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


Basically it is saying "if the user pressed submit do these things...".
Right now it is validating the form for 4 things:
1./* Make sure all fields were entered */
2./* Spruce up username, check length */
3./* Check if username is already in use */
4./* Check if email is already in use */

You can simply add addition if statements to validate further.

I would recommend organizing it better by putting all validation for each form field into the same statements.
I will try to be of further assistance later by showing you some examples but right now I go to study for my mid-term exams (:().

PRodgers4284
01-30-2008, 09:54 PM
The code at present outputs the error message on a different page, i would like the message to appear on the same page below the form, can you help me with this?

c_and13_
01-31-2008, 11:11 PM
for stuff like this...you may find it most advantageous to hit a site like http://www.codango.com/...

i know hotscripts has already been mentioned...but i just find codango to be a nicer/easier to use site with more valuable content.

Good luck with your script search though :)

helasraizam
10-11-2008, 01:26 PM
What you need to learn is a very simple coding device called AJAX. It just combines basic javascript and html to make a dynamic php/html/javascript website.

It's really easy to learn, and doesn't take very much time at all to learn. I think you'll quite be satisfied with it.

Here is the link to a rather straightforward tutorial; though you're welcome to do more research:
http://w3schools.com/ajax/default.asp