Page 3 of 3

Re: Help Needed- user Input Validation

Posted: Tue May 05, 2009 8:59 pm
by phphelpseeker
Is there any way I can achieve this in the following code:

Code: Select all

 
<?php
$db = mysql_connect(localhost, 'dbusername', 'password') or die('Error: ' . mysql_error());
mysql_select_db('DBname') or die('Could not select database');
$firstname = $_POST['firstname'];  
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['passowrd'];
$str = $_POST['street'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$phone = $_POST['phone'];
$email = $_POST['email'];
 
if (!empty($_POST['firstname'])) {
$firstname = $_POST['firstname'];
}else{
echo "Please enter your first name.<br/>";
die();
}
if(!empty($_POST['lastname'])){ 
$lastname = $_POST['lastname'];
}else{
echo "Please enter your lastname.<br/>";
die(); 
}
if(!empty($_POST['username'])){
$username=$_POST['username'];
}else{
echo "Please enter your username.<br/>";
die();
} 
if (strlen($_POST['username']) >= 6 && strlen($_POST['username']) <=32){
$sql = "SELECT * FROM Accounts WHERE Username='mysql_real_escape_string($username)'";
$query = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($query) < 0) 
{$username = $_POST['username'];
}else{
echo "Username exists. Please enter a different username.<br/>";
die();
}
}
if(!empty($_POST['password']) && strlen($_POST['password']) >= 6 && strlen($_POST['password']) <= 15 ){
$password = md5($_POST['password']);
}else{
echo "Please enter a valid password.<br/>";
die();  
}
if(!empty($_POST['street'])){
$str = $_POST['street'];
}else{
echo "Please enter your street.<br/>"; 
die();
}
if(!empty($_POST['city'])){
$city = $_POST['city'];
}else{
echo "Please enter your city.<br/>";
die(); 
}
if(!empty($_POST['state'])){
$state =  $_POST['state'];
}else{
echo "Please enter your state.<br/>"; 
die(); 
}
if (preg_match("/^[0-9]{5}([0-9]{4})?$/i", $zipcode)) {
$zipcode = $_POST['zipcode'];
} else {
echo "Please enter valid zipcode.<br/>";
die();
}
if(preg_match("/^[2-9]{1}[0-9]{2}-[0-9]{3}-[0-9]{4}?$/i", $phone)) {
$phone = $_POST['phone'];
} else {
echo "Please enter valid phone number.<br/>";
die();
}
if(preg_match("/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}?$/i",$email)){ 
$email = $_POST['email']; 
} else {
echo "Please enter valid email address.<br/>";
die();
}
$sqli = "INSERT INTO Accounts (Firstname, Lastname, Username, Password, Street, City, State, Zipcode, `Primary Phone`, Email) VALUES ('$firstname', '$lastname', '$username', '$password', '$str', '$city', '$state', '$zipcode', '$phone', '$email')"; 
$ins = mysql_query($sqli) or die(mysql_error());
echo "You are registered successfully.<br/>";
exit;
?>
 
Please help change the color of the text when the form is redisplayed to correct the user input. How can I do that in my code? I really need help on this one.
Thank you.
Priya

Re: Help Needed- user Input Validation

Posted: Wed May 06, 2009 7:25 am
by phphelpseeker
So should I embed the entire php code in my html or the other way round? Will that solve my problem? Somebody please help me.

Thank you.
Priya

Re: Help Needed- user Input Validation

Posted: Wed May 06, 2009 8:13 am
by phphelpseeker
Can somebody help solve this?

After successful login/registration, the user should be directed to a directory (it should be created upon successful login/registration) which is protected by their credentials (username/password saved in the DB).

Please suggest me how to solve this one.

Thank you.
Priya

Re: Help Needed- user Input Validation

Posted: Wed May 06, 2009 9:33 am
by phphelpseeker
Can I use htaccess to do this? I'm using Apache server.

Thank you.
Priya

Re: Help Needed- user Input Validation

Posted: Wed May 06, 2009 9:35 am
by phphelpseeker
McInfo please help me. All your suggestions really worked for me. Can you please help me figure this one?

Thank you.
Priya