944,147 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 634
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 4th, 2009
0

validation

Expand Post »
PHP Syntax (Toggle Plain Text)
  1. hi
  2. can any one tell me how validation can be done in PHP WITH OUT USINGjavascript
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
elanorejoseph is offline Offline
36 posts
since Jan 2009
Nov 4th, 2009
-1
Re: validation
yes you can validate in php only,
For example, validate against the blank field like -
if($str ==''){ echo "You have not entered anything";}
or you can use the regular expressions for checking the correct format of data like format of email id etc.
and size exceeding more than 100 char as -
if(strlen($str) >100) { echo "exceeding the limit of 100 chars"; }
etc.
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Nov 4th, 2009
0
Re: validation
Thank you so much for the reply.... you solved it
Reputation Points: 10
Solved Threads: 0
Light Poster
elanorejoseph is offline Offline
36 posts
since Jan 2009
Nov 4th, 2009
-1
Re: validation
Hey.

The isset and empty functions are also handy when validating form data. (or any data, for that matter)
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 4th, 2009
-1
Re: validation
thanks Alti.. but i didnt understand your second answer properly
Click to Expand / Collapse  Quote originally posted by Atli ...
Duplicate... The quick-reply thing keeps doing that
Reputation Points: 10
Solved Threads: 0
Light Poster
elanorejoseph is offline Offline
36 posts
since Jan 2009
Nov 4th, 2009
0
Re: validation
thanks Alti.. but i didnt understand your second answer properly
don't worry about it, it happens because of the quick reply and the reload.
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Nov 4th, 2009
1
Re: validation
Yes, sorry about that second post. When I posted my first post it got posted twice because of a bug in the forum. (I assume)
It has been removed now.

But OK, as form validation is a rather important topic, let me be a little more thorough.

There are a few steps that are necessary when validating incoming user data.
  1. First step is to eliminate the possibility that the ancient magic_quotes feature is messing up your data. (Ideally, it should be turned off. But it is always best to add this just in case.)
    A few simple lines of code can completely nullify that effect, if it is present.
    Keep in mind that this code should only be executed once per page, or you risk messing up your data even further.
    php Syntax (Toggle Plain Text)
    1. // Nullify the effects of the magic_quotes feature, if enabled.
    2. // This is targeted only at the $_POST array. You can change it to $_REQUEST if needed.
    3. if(get_magic_quotes_gpc())
    4. {
    5. foreach($_POST as &$_data)
    6. {
    7. $_data = stripslashes($_data);
    8. }
    9. }
  2. Verify that all the fields you need are present and filled in.
    In these examples I'll assume you want to verify user login data, where a user passes both his user-name and an email, as well as his password.
    php Syntax (Toggle Plain Text)
    1. // List all fields you are expecting to get
    2. $fields = array('username', 'email', 'password');
    3.  
    4. // Set up an array to contain all errors that occur during the validation.
    5. // This can then be used later on to determine whether or not the validation failed.
    6. $errors = array();
    7.  
    8. // Step through the list of expected fields and verify that they are set, and that they are not empty.
    9. foreach($fields as $_field)
    10. {
    11. if(!isset($_POST[$_field]) || empty($_POST[$_field]))
    12. {
    13. $errors[] = "Field '$_field' is missing or empty.";
    14. }
    15. }
  3. Verify the data in individual fields. As opposed to the previous code example, which only verified their existence, this one verifies that the data you are receiving is in valid format.
    php Syntax (Toggle Plain Text)
    1. // Verify the username.
    2. $usernameRegexp = '/[\w\d_-\' ]{3,32}/i';
    3. if(!preg_match($passwordRegexp, $_POST['username']))
    4. {
    5. $errors[] = "Usernames must be between 3 and 32 characters long, and they may only contain letters, numbers, spaces, and the following characters: (_), (-), (').";
    6. }
    7.  
    8. // Verify the password.
    9. if(strlen($_POST['password']) < 6)
    10. {
    11. $errors[] = "Passwords must be more than 6 characters";
    12. }
    13.  
    14. // Verify the email
    15. $emailRegexp = '/^([\w\d.-_]+?)@([\w\d.-_]+?)\.(\w{2,4})$/i';
    16. if(!preg_match($emailRegexp, $_POST['email']))
    17. {
    18. $errors[] = "The email address is invalid!";
    19. }
  4. And finally, use the $errors array to determine if the validation failed or succeeded:
    php Syntax (Toggle Plain Text)
    1. // Check if there were any errors
    2. if(count($errors) == 0)
    3. {
    4. // The data is valid.
    5. // Do with it as you please.
    6. }
    7. else
    8. {
    9. // Print all the errors.
    10. echo "<p>The following errors were found:<br><ul>"
    11. foreach($errors as $_error)
    12. {
    13. echo "<li>$_error</li>";
    14. }
    15. echo "</ul><p>";
    16. }
Put that all together and you will have a pretty decent user-login validation script.


Also, while I'm at it. If you were planing on using the data in that form to log a user in via a SQL query, further security measures need to be taken.
  1. One of the biggest threat to PHP security is SQL Injection.
    This is what the now deprecated magic_quotes feature was meant to prevent, but it was only ever partially effective, and it causes far to much trouble to be wort using.

    There are several ways to prevent this, most of them database-specific. MySQL databases, for example, have the mysql_real_escape_string function, which takes care of escaping the data for you.
    You could use that on the data in the previous examples like so:
    php Syntax (Toggle Plain Text)
    1. // Prepare all fields for being used in a MySQL query.
    2. $safeData = array();
    3. foreach($fields as $_field) {
    4. $safeData[$_field] = mysql_real_escape_string($_POST[$_field]);
    5. }
  2. Hash your passwords!
    A basic way to protect your user's passwords from being known by others, including you, is to use a one-way encryption algorithm on it, known as "hashing", to turn it into a long string of seemingly random characters.
    Then you store the hash in the database as the user's password. When it comes time to log the user in, you hash the password he provides in the same way you did when it was created, and compare the two. If the hashes match, it is valid.
    php Syntax (Toggle Plain Text)
    1. // Create a password hash. Never store passwords as plain text!
    2. $passwordHash = hash("sha256", $safeData['password']);
  3. And then you create and execute the query, using the now secure data.
    php Syntax (Toggle Plain Text)
    1. // Compile the query, using the verified data
    2. $sql = "SELECT `id`
    3. FROM `user`
    4. WHERE
    5. (`username` = '{$safeData['username']}')
    6. AND (`email` = '{$safeData['email']}')
    7. AND (`passwordHash` = '{$passwordHash}')";
  4. Which you would finish of like so:
    php Syntax (Toggle Plain Text)
    1. // Execut the query and check the results.
    2. $result = mysql_query($sql);
    3. if($result && mysql_num_rows($result) == 1)
    4. {
    5. echo "Login succesfull!";
    6.  
    7. // And then do stuff to actually log him in.
    8. }
    9. else
    10. {
    11. echo "Login failed! Better luck next time.";
    12. }

I'll leave it there... for now
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 5th, 2009
0

thanls

hi thanks Alti....

Click to Expand / Collapse  Quote originally posted by Atli ...
Yes, sorry about that second post. When I posted my first post it got posted twice because of a bug in the forum. (I assume)
It has been removed now.

But OK, as form validation is a rather important topic, let me be a little more thorough.

There are a few steps that are necessary when validating incoming user data.
  1. First step is to eliminate the possibility that the ancient magic_quotes feature is messing up your data. (Ideally, it should be turned off. But it is always best to add this just in case.)
    A few simple lines of code can completely nullify that effect, if it is present.
    Keep in mind that this code should only be executed once per page, or you risk messing up your data even further.
    php Syntax (Toggle Plain Text)
    1. // Nullify the effects of the magic_quotes feature, if enabled.
    2. // This is targeted only at the $_POST array. You can change it to $_REQUEST if needed.
    3. if(get_magic_quotes_gpc())
    4. {
    5. foreach($_POST as &$_data)
    6. {
    7. $_data = stripslashes($_data);
    8. }
    9. }
  2. Verify that all the fields you need are present and filled in.
    In these examples I'll assume you want to verify user login data, where a user passes both his user-name and an email, as well as his password.
    php Syntax (Toggle Plain Text)
    1. // List all fields you are expecting to get
    2. $fields = array('username', 'email', 'password');
    3.  
    4. // Set up an array to contain all errors that occur during the validation.
    5. // This can then be used later on to determine whether or not the validation failed.
    6. $errors = array();
    7.  
    8. // Step through the list of expected fields and verify that they are set, and that they are not empty.
    9. foreach($fields as $_field)
    10. {
    11. if(!isset($_POST[$_field]) || empty($_POST[$_field]))
    12. {
    13. $errors[] = "Field '$_field' is missing or empty.";
    14. }
    15. }
  3. Verify the data in individual fields. As opposed to the previous code example, which only verified their existence, this one verifies that the data you are receiving is in valid format.
    php Syntax (Toggle Plain Text)
    1. // Verify the username.
    2. $usernameRegexp = '/[\w\d_-\' ]{3,32}/i';
    3. if(!preg_match($passwordRegexp, $_POST['username']))
    4. {
    5. $errors[] = "Usernames must be between 3 and 32 characters long, and they may only contain letters, numbers, spaces, and the following characters: (_), (-), (').";
    6. }
    7.  
    8. // Verify the password.
    9. if(strlen($_POST['password']) < 6)
    10. {
    11. $errors[] = "Passwords must be more than 6 characters";
    12. }
    13.  
    14. // Verify the email
    15. $emailRegexp = '/^([\w\d.-_]+?)@([\w\d.-_]+?)\.(\w{2,4})$/i';
    16. if(!preg_match($emailRegexp, $_POST['email']))
    17. {
    18. $errors[] = "The email address is invalid!";
    19. }
  4. And finally, use the $errors array to determine if the validation failed or succeeded:
    php Syntax (Toggle Plain Text)
    1. // Check if there were any errors
    2. if(count($errors) == 0)
    3. {
    4. // The data is valid.
    5. // Do with it as you please.
    6. }
    7. else
    8. {
    9. // Print all the errors.
    10. echo "<p>The following errors were found:<br><ul>"
    11. foreach($errors as $_error)
    12. {
    13. echo "<li>$_error</li>";
    14. }
    15. echo "</ul><p>";
    16. }
Put that all together and you will have a pretty decent user-login validation script.


Also, while I'm at it. If you were planing on using the data in that form to log a user in via a SQL query, further security measures need to be taken.
  1. One of the biggest threat to PHP security is SQL Injection.
    This is what the now deprecated magic_quotes feature was meant to prevent, but it was only ever partially effective, and it causes far to much trouble to be wort using.

    There are several ways to prevent this, most of them database-specific. MySQL databases, for example, have the mysql_real_escape_string function, which takes care of escaping the data for you.
    You could use that on the data in the previous examples like so:
    php Syntax (Toggle Plain Text)
    1. // Prepare all fields for being used in a MySQL query.
    2. $safeData = array();
    3. foreach($fields as $_field) {
    4. $safeData[$_field] = mysql_real_escape_string($_POST[$_field]);
    5. }
  2. Hash your passwords!
    A basic way to protect your user's passwords from being known by others, including you, is to use a one-way encryption algorithm on it, known as "hashing", to turn it into a long string of seemingly random characters.
    Then you store the hash in the database as the user's password. When it comes time to log the user in, you hash the password he provides in the same way you did when it was created, and compare the two. If the hashes match, it is valid.
    php Syntax (Toggle Plain Text)
    1. // Create a password hash. Never store passwords as plain text!
    2. $passwordHash = hash("sha256", $safeData['password']);
  3. And then you create and execute the query, using the now secure data.
    php Syntax (Toggle Plain Text)
    1. // Compile the query, using the verified data
    2. $sql = "SELECT `id`
    3. FROM `user`
    4. WHERE
    5. (`username` = '{$safeData['username']}')
    6. AND (`email` = '{$safeData['email']}')
    7. AND (`passwordHash` = '{$passwordHash}')";
  4. Which you would finish of like so:
    php Syntax (Toggle Plain Text)
    1. // Execut the query and check the results.
    2. $result = mysql_query($sql);
    3. if($result && mysql_num_rows($result) == 1)
    4. {
    5. echo "Login succesfull!";
    6.  
    7. // And then do stuff to actually log him in.
    8. }
    9. else
    10. {
    11. echo "Login failed! Better luck next time.";
    12. }

I'll leave it there... for now
Reputation Points: 10
Solved Threads: 0
Light Poster
elanorejoseph is offline Offline
36 posts
since Jan 2009
Nov 5th, 2009
0
Re: validation
That was nice Atli... I need to learn how to create that custom regex codes. May be a link to some tutorial can be helpful..

Click to Expand / Collapse  Quote originally posted by Atli ...
Yes, sorry about that second post. When I posted my first post it got posted twice because of a bug in the forum. (I assume)
It has been removed now.

But OK, as form validation is a rather important topic, let me be a little more thorough.

There are a few steps that are necessary when validating incoming user data.
  1. First step is to eliminate the possibility that the ancient magic_quotes feature is messing up your data. (Ideally, it should be turned off. But it is always best to add this just in case.)
    A few simple lines of code can completely nullify that effect, if it is present.
    Keep in mind that this code should only be executed once per page, or you risk messing up your data even further.
    php Syntax (Toggle Plain Text)
    1. // Nullify the effects of the magic_quotes feature, if enabled.
    2. // This is targeted only at the $_POST array. You can change it to $_REQUEST if needed.
    3. if(get_magic_quotes_gpc())
    4. {
    5. foreach($_POST as &$_data)
    6. {
    7. $_data = stripslashes($_data);
    8. }
    9. }
  2. Verify that all the fields you need are present and filled in.
    In these examples I'll assume you want to verify user login data, where a user passes both his user-name and an email, as well as his password.
    php Syntax (Toggle Plain Text)
    1. // List all fields you are expecting to get
    2. $fields = array('username', 'email', 'password');
    3.  
    4. // Set up an array to contain all errors that occur during the validation.
    5. // This can then be used later on to determine whether or not the validation failed.
    6. $errors = array();
    7.  
    8. // Step through the list of expected fields and verify that they are set, and that they are not empty.
    9. foreach($fields as $_field)
    10. {
    11. if(!isset($_POST[$_field]) || empty($_POST[$_field]))
    12. {
    13. $errors[] = "Field '$_field' is missing or empty.";
    14. }
    15. }
  3. Verify the data in individual fields. As opposed to the previous code example, which only verified their existence, this one verifies that the data you are receiving is in valid format.
    php Syntax (Toggle Plain Text)
    1. // Verify the username.
    2. $usernameRegexp = '/[\w\d_-\' ]{3,32}/i';
    3. if(!preg_match($passwordRegexp, $_POST['username']))
    4. {
    5. $errors[] = "Usernames must be between 3 and 32 characters long, and they may only contain letters, numbers, spaces, and the following characters: (_), (-), (').";
    6. }
    7.  
    8. // Verify the password.
    9. if(strlen($_POST['password']) < 6)
    10. {
    11. $errors[] = "Passwords must be more than 6 characters";
    12. }
    13.  
    14. // Verify the email
    15. $emailRegexp = '/^([\w\d.-_]+?)@([\w\d.-_]+?)\.(\w{2,4})$/i';
    16. if(!preg_match($emailRegexp, $_POST['email']))
    17. {
    18. $errors[] = "The email address is invalid!";
    19. }
  4. And finally, use the $errors array to determine if the validation failed or succeeded:
    php Syntax (Toggle Plain Text)
    1. // Check if there were any errors
    2. if(count($errors) == 0)
    3. {
    4. // The data is valid.
    5. // Do with it as you please.
    6. }
    7. else
    8. {
    9. // Print all the errors.
    10. echo "<p>The following errors were found:<br><ul>"
    11. foreach($errors as $_error)
    12. {
    13. echo "<li>$_error</li>";
    14. }
    15. echo "</ul><p>";
    16. }
Put that all together and you will have a pretty decent user-login validation script.


Also, while I'm at it. If you were planing on using the data in that form to log a user in via a SQL query, further security measures need to be taken.
  1. One of the biggest threat to PHP security is SQL Injection.
    This is what the now deprecated magic_quotes feature was meant to prevent, but it was only ever partially effective, and it causes far to much trouble to be wort using.

    There are several ways to prevent this, most of them database-specific. MySQL databases, for example, have the mysql_real_escape_string function, which takes care of escaping the data for you.
    You could use that on the data in the previous examples like so:
    php Syntax (Toggle Plain Text)
    1. // Prepare all fields for being used in a MySQL query.
    2. $safeData = array();
    3. foreach($fields as $_field) {
    4. $safeData[$_field] = mysql_real_escape_string($_POST[$_field]);
    5. }
  2. Hash your passwords!
    A basic way to protect your user's passwords from being known by others, including you, is to use a one-way encryption algorithm on it, known as "hashing", to turn it into a long string of seemingly random characters.
    Then you store the hash in the database as the user's password. When it comes time to log the user in, you hash the password he provides in the same way you did when it was created, and compare the two. If the hashes match, it is valid.
    php Syntax (Toggle Plain Text)
    1. // Create a password hash. Never store passwords as plain text!
    2. $passwordHash = hash("sha256", $safeData['password']);
  3. And then you create and execute the query, using the now secure data.
    php Syntax (Toggle Plain Text)
    1. // Compile the query, using the verified data
    2. $sql = "SELECT `id`
    3. FROM `user`
    4. WHERE
    5. (`username` = '{$safeData['username']}')
    6. AND (`email` = '{$safeData['email']}')
    7. AND (`passwordHash` = '{$passwordHash}')";
  4. Which you would finish of like so:
    php Syntax (Toggle Plain Text)
    1. // Execut the query and check the results.
    2. $result = mysql_query($sql);
    3. if($result && mysql_num_rows($result) == 1)
    4. {
    5. echo "Login succesfull!";
    6.  
    7. // And then do stuff to actually log him in.
    8. }
    9. else
    10. {
    11. echo "Login failed! Better luck next time.";
    12. }

I'll leave it there... for now
Reputation Points: 13
Solved Threads: 21
Junior Poster
venkat0904 is offline Offline
186 posts
since Oct 2009
Nov 5th, 2009
0
Re: validation
Click to Expand / Collapse  Quote originally posted by venkat0904 ...
That was nice Atli... I need to learn how to create that custom regex codes. May be a link to some tutorial can be helpful..
Sure. Try this one. It's what I used when I was first starting out with regular expressions.
And you can check out the details on how PHP uses them here.

Regular expressions can be a bit tough to work with tho, even when you know all about them, and they are not the easiest thing to learn. It's worth it tho. They are an extremely powerful tool.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Multiple file upload through an array
Next Thread in PHP Forum Timeline: upload file and save the content to specific fields in mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC