Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Trying to use End Users' sanitize script with form.


Trying to use End Users' sanitize script with form.

Closed Thread
  #1 (permalink)  
Old 10-02-09, 10:29 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Trying to use End Users' sanitize script with form.

I have been looking at End Users' script for sanitizing data and was wondering how to use it. I have a general idea but I'm not sure if it is correct.

I am planning on having a "Coming soon" image (which I haven't made yet) on my temporary homepage as well as some text that says "If you would like us to email you when the site is launched fill out the form below."

I also plan on adding BotScout and ReCAPTCHA to this once I get it working to help prevent spambots from attacking the site.

I intend on having the only mandatory field be their email address with their first and last name being optional. So far here is what I've come up with, although I am pretty sure I've screwed up somewhere, which is why I am asking for help with it.

The reason I don't have the image linked, or the code for ReCAPTCHA/BotScout is because I figured I should K.I.S.S. and try to get the base of it working first.

Main page:
php Code:
  1. <?php include('./includes/stuff/sanitize.php');
  2. include('./includes/stuff/constants.php');?>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <title>Coming Soon!</title>
  8. </head>
  9.  
  10. <body>
  11. The site will be launching soon!
  12. <p>If you would like us to email you when the site is launched fill out the form below.</p>
  13. <?php
  14. if (isset($_POST&#91;'submitted'])) {      
  15.         require_once('MYSQL');
  16.        
  17.         //Sanitize data
  18.         $c_fname = sanitize(3, 20, $_POST&#91;'first_name']);
  19.         $c_lname = sanitize(3, 40, $_POST&#91;'last_name']);
  20.         $c_email = sanitize(5, 50, $_POST&#91;'email']);
  21.        
  22.         if (empty($c_email)) {
  23.             echo '<p>Please enter your email address.</p>';
  24.         } elseif (!empty($c_email)) {
  25.             $access = "SELECT email FROM customers WHERE email='$c_email'"
  26.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  27.         }
  28.        
  29.         //Insert information if email is unique:
  30.         if (mysqli_num_rows($r) == 0) {
  31.             $access = "INSERT INTO customers (email) VALUES ('$c_email')";
  32.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  33. } else {
  34.     echo '<p>Your email address could not be recorded due to an error, please try again.</p>';
  35. }
  36. mysqli_close($dbc);
  37. ?>
  38. <form action="sanitize.php" method="post">
  39. <fieldset>
  40. <p><b>First name:</b> <input type="text" name="first_name" size="20" maxlength="15" value="<?php if(isset($cleaned_fname)) echo $c_fname; ?>" /></p>
  41.  
  42. <p><b>Last name:</b> <input type="text" name="last_name" size="20" maxlength="25" value="<?php if(isset($cleaned_lname)) echo $c_lname; ?>" /></p>
  43.  
  44. <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="50" value="<?php if(isset($cleaned_email)) echo $c_email; ?>" /></p>
  45.  
  46. <div class="center"><input type="submit" name="submit" value="Register" /></div>
  47. <input type="hidden" name="submitted" value="TRUE" />
  48. </form>
  49. </body>
  50. </html>

I don't have the spacing between forum fields (other than the paragraph tags) like I do above, I just did that so it would be easier to read.

Sanitize code removed.


constants.php Code:
    <?phpdefine('BASE_URL', 'http://mywebsite.com');define('MYSQL', '../SQL/mysqli_connect.php');?>

and, finally, mysqli_connect.php I edited the user information in the script below to random stuff:
php Code:
  1. <?php
  2. //Set database access information:
  3. DEFINE('DB_USER', '**********');
  4. DEFINE('DB_PASSWORD', '********');
  5. DEFINE('DB_HOST', 'localhost');
  6. DEFINE('DB_NAME', 'blah');
  7.  
  8. //Attempt to connect to MySQL
  9. $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  10. if (!$dbc) {
  11.     trigger_error('Could not connect to MySQL: ' . mysqli_connect_error() );
  12. }
  13. ?>

Again the reason I am even posting this is because I am not sure how to use End Users' script and would like to in order to help prevent my server from being hijacked.

Last edited by wirehopper; 02-26-10 at 12:42 PM. Reason: Sanitize code removed by request.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #2 (permalink)  
Old 10-02-09, 11:19 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
I use sanitize for inputs that can't be validated - like textareas or strings where the user can type anything they want to.

For other inputs, all data must be valid.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #3 (permalink)  
Old 10-03-09, 08:48 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Looks like you're using it correctly to me. What part are you having trouble with?


PHP Code:

$c_fname sanitize(320$_POST['first_name']);

$c_lname sanitize(340$_POST['last_name']);
$c_email sanitize(550$_POST['email']); 
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #4 (permalink)  
Old 10-03-09, 02:04 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by End User View Post
Looks like you're using it correctly to me. What part are you having trouble with?


PHP Code:

$c_fname sanitize(320$_POST['first_name']);

$c_lname sanitize(340$_POST['last_name']);
$c_email sanitize(550$_POST['email']); 
I haven't actually put it up on my server yet, though I will be doing that in a few hours. I just wanted to get some sort of confirmation that it was at least somewhat correct before it goes "live".

I will test it out after I upload it and if it gives me problems I'll report back.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #5 (permalink)  
Old 10-03-09, 05:40 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
It is 'somewhat correct', but you need to validate it as well as sanitize.

Sanitize will protect your server, application, and data - but it won't ensure the email address is valid. You need to test for a valid email address before letting it go live.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #6 (permalink)  
Old 10-03-09, 06:10 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
@wirehopper,

OK I will look around and see what I can find on validation around here as well as the web.

I assume that one should sanitize the input and then validate it to make sure it's still valid after it's sanitized, correct?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #7 (permalink)  
Old 10-03-09, 06:54 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by PopSmith View Post
I assume that one should sanitize the input and then validate it to make sure it's still valid after it's sanitized, correct?
Correct. Clean it and then check it to make sure it's legit data. Here's some simple email validation code:

PHP Code:

function isValidEmail$email null ){

    return 
preg_match"/^
    [\d\w\/+!=#|$?%{^&}*`'~-]
    [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
    [A-Z0-9]
    [A-Z0-9.-]{1,61}
    [A-Z0-9]\.
    [A-Z]{2,6}$/ix"
$email );
}
?> 
Or:

PHP Code:

function checkEmail($email) {

    if (
ereg("^[a-zA-Z0-9][a-zA-Z0-9_\.\-]*[@][a-zA-Z0-9\.\-]*[\.][a-zA-Z]{2,4}$"$email)) {
        return 
TRUE;
    } else {
        return 
FALSE;
    }
}
?> 
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
The Following User Says Thank You to End User For This Useful Post:
PopSmith (10-03-09)
  #8 (permalink)  
Old 10-04-09, 02:19 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks for the validation stuff, End User.

I experimented with that code and so here is my index.php file again. I just want to make sure that I'm using, and understanding, the usage of preg_match correctly.

I've gathered that preg_match checks whatever variable is listed after the comma against the specified parameters. For my $c_fname variable it would only be returned to the form if it contains only upper or lowercase A-Z as well as periods and dashes. It also must be between 1 and 15 characters as dictated by the {1,15}.

php Code:
  1. <?php include('../includes/stuff/sanitize.php');
  2. include('../includes/stuff/constants.php');
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <title>Coming Soon!</title>
  9. </head>
  10.  
  11. <body>
  12. We are hard at work getting the site up and running! We are hoping to get running by early December!
  13. <p>If you would like us to notify you when we get setup fill out the form below with your name and email address.</p>
  14. <?php
  15. if (isset($_POST&#91;'submitted'])) {
  16.         require_once('MYSQL');
  17.        
  18.         //Sanitize data before validation
  19.         $c_fname = sanitize(3, 15, $_POST&#91;'first_name']);
  20.         $c_lname = sanitize(3, 25, $_POST&#91;'last_name']);
  21.         $c_email = sanitize(5, 60, $_POST&#91;'email']);
  22.         $c_email_2 = sanitize(5, 60, $_POST&#91;'email_2']);
  23.        
  24.         //Validate the first name
  25.         return preg_match("/^
  26.         [a-zA-Z]
  27.         [a-zA-Z.-]{1,15}$/ix", $c_fname );
  28.        
  29.         //Validate last name
  30.         return preg_match("/^
  31.         [a-zA-Z]
  32.         [a-zA-Z-]{1,25}$/ix", $c_lname);
  33.        
  34.         //Is the email valid and not empty?
  35.         if (empty($c_email && $c_email_2)) {
  36.             echo '<p>Please enter your email address.</p>';
  37.         } else if ($c_email != $c_email_2) {
  38.             echo '<p>The email fields do not match. Please confirm them again.</p>';
  39.         } else {
  40.             function isValidEmail( $c_email = null ) {
  41.             return preg_match( "/^
  42.             [\d\w\/+!=#|$?%{^&}*`'~-]
  43.             [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
  44.             [A-Z0-9]
  45.             [A-Z0-9.-]{1,60}
  46.             [A-Z0-9]\.
  47.             [A-Z]{2,6}$/ix", $c_email );
  48.                 }
  49.         }
  50.  
  51.         (!empty($c_email) && $c_email == $c_email_2) {
  52.             $access = "SELECT email FROM customers WHERE email='$c_email'"
  53.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  54.         }
  55.        
  56.         //Insert information if email is unique:
  57.         if (mysqli_num_rows($r) == 0) {
  58.             $access = "INSERT INTO customers (email) VALUES ('$c_email')";
  59.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  60.         } else {
  61.             echo '<p>Your email address could not be recorded due to an error, please try again.</p>';
  62. }
  63. mysqli_close($dbc);
  64. ?>
  65. <form action="sanitize.php" method="post">
  66. <fieldset>
  67. <p><b>First name:</b> <input type="text" name="first_name" size="20" maxlength="15" value="<?php if(isset($c_fname['first_name'])) echo $c_fname['first_name']; ?>" /></p>
  68. <p><b>Last name:</b> <input type="text" name="last_name" size="20" maxlength="25" value="<?php if(isset($c_lname['last_name'])) echo $c_lname['last_name']; ?>" /></p>
  69. <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="60" value="<?php if(isset($c_email['email'])) echo $c_email['email']; ?>" /> (Required)</p>
  70. <p><b>Confirm email address:</b> <input type="text" name="email_2" size="30" maxlength="60" value="<?php if(isset($c_email_2['email_2'])) echo $c_email_2['email_2']; ?>" /> (Required)</p>
  71. <br />
  72. </fieldset>
  73. <div class="center"><input type="submit" name="submit" value="Register" /></div>
  74. <input type="hidden" name="submitted" value="TRUE" />
  75. </form>
  76. </body>
  77. </html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #9 (permalink)  
Old 10-06-09, 08:43 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by PopSmith View Post
For my $c_fname variable it would only be returned to the form if it contains only upper or lowercase A-Z as well as periods and dashes. It also must be between 1 and 15 characters as dictated by the {1,15}.
With this usage:

$c_fname = sanitize(3, 15, $_POST['first_name']);

....the '15' means it will truncate anything over 15 chars long. No minimum length is enforced.
The '3' means it'll pass numbers and letters, with a couple of other chars:

// allow only alphanumeric characters, space, '_', period, colon, and '-'
$data = preg_replace("/[^0-9a-zA-Z~\-\ \.\:\_]/",'', $data);
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #10 (permalink)  
Old 10-06-09, 10:36 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
I was looking over my previous response and realized that I didn't make any sense, compared to what I really meant to ask about.

I am actually looking for clarification on the preg_match function as I used here:

php Code:
  1. //Validate the first name
  2. return preg_match("/^
  3. [a-zA-Z]
  4. [a-zA-Z._]{1,15}$/ix", $c_fname );

I am thinking that my above use of preg_match would validate that the $c_fname variable, which is the sanitized output of the variable $first_name, contains only characters a-z in either upper or lower case as well as the period or underscore. The {1,15} would limit the preg_match to between 1 and 15 characters.

However, I could (and probably am) wrong in my thinking.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] searching through a grid view painthu ASP.NET 5 05-21-08 11:11 AM
3 Column CSS Fluid Layout (IE 6 Problem) Heidenreich12 CSS 9 10-04-06 04:22 PM
Script to create backend of form? tb582 Job Offers & Assistance 3 03-16-06 11:50 AM
Absolutely New to VB.Net and Need a Little Help nothingofvalue Windows .NET Programming 2 07-23-05 03:56 PM


All times are GMT -5. The time now is 02:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.