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 (which is End Users' code):
php Code:
  1. <?php
  2.  
  3. ////////////////////////////////////////////
  4. // input sanitizer function - LDM 2008
  5.  
  6. function sanitize($dtype, $dlen, $data){
  7.  
  8. // dtype 1: allow numbers, space, and '-' 
  9. // dtype 2: allow alpha and spaces only
  10. // dtype 3: allow alphanumeric, spaces, period, and '-'
  11. // dtype 4: allow alphanumeric w/ all punctuation 
  12. // dtype 5: email validation chars 
  13. // dlen: data length limit, '0' = no length limit
  14. /////////////////////////////////////////////////////////////
  15. // Example- allow numbers and letters up to 50 chars in the variable "$foo"
  16. // $foo = sanitize(2, 50, $_POST['name']);
  17. /////////////////////////////////////////////////////////////
  18.  
  19.     // special cleanups
  20.     $data = preg_replace("/x1a/",'', $data);
  21.     $data = preg_replace("/x00/",'', $data);
  22.  
  23.     // the 2 tests above may not be needed due to this more complete test
  24.     $data = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $data);
  25.  
  26.     $data = preg_replace("|\.\./|",'', $data); // stop directory traversal
  27.     $data = preg_replace("/--/",' - ', $data); // stop mySQL comments
  28.     $data = preg_replace("/%3A%2F%2F/",'', $data); // stop B64 encoded '://'
  29.  
  30.      
  31. // new, added 8-31-2008 /////////////////////////////////
  32. ////////// START NEW TESTS 08-31-2008 ////////////////////////////////////////
  33.  
  34. // Remove Null Characters
  35. // This prevents sandwiching null characters
  36. // between ascii characters, like Java\0script.
  37.     $data = preg_replace('/\0+/', '', $data);
  38.     $data = preg_replace('/(\\\\0)+/', '', $data);
  39.  
  40.  
  41. // Validate standard character entities
  42. // Add a semicolon if missing.  We do this to enable
  43. // the conversion of entities to ASCII later.
  44.     $data = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u',"\\1;",$data);
  45.          
  46. // Validate UTF16 two byte encoding (x00)
  47. // Just as above, adds a semicolon if missing.
  48.     $data = preg_replace('#(&\#x*)([0-9A-F]+);*#iu',"\\1\\2;",$data);
  49.  
  50.  
  51. // URL Decode
  52. // Just in case stuff like this is submitted:
  53. // <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
  54. // Note: Normally urldecode() would be easier but it removes plus signs
  55.     $data = preg_replace("/([a-z0-9]{3})/i", "&#x\\1;", $data);
  56.     $data = preg_replace("/%([a-z0-9]{2})/i", "&#x\\1;", $data);         
  57.                  
  58.  
  59. // Convert character entities to ASCII
  60. // This permits our tests below to work reliably.
  61. // We only convert entities that are within tags since
  62. // these are the ones that will pose security problems.
  63.     if (preg_match_all("/<(.+?)>/si", $data, $matches)) {         
  64.         for ($i = 0; $i < count($matches&#91;'0']); $i++) {
  65.             $data = str_replace($matches&#91;'1'][$i],
  66.                 html_entity_decode($matches&#91;'1'][$i], ENT_COMPAT, $charset), $data);
  67.         }
  68.     }
  69.      
  70.  
  71. // Convert all tabs to spaces
  72. // This prevents strings like this: ja    vascript
  73. // Note: we deal with spaces between characters later.     
  74.     $data = preg_replace("#\t+#", " ", $data);
  75.      
  76.  
  77. // Makes PHP tags safe
  78. // Note: XML tags are inadvertently replaced too:
  79. //    <?xml
  80. // But who cares, only terrorists use XML. :)     
  81.     $data = str_replace(array('<?php', '<?PHP', '<?', '?>')array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $data);
  82.      
  83.  
  84. // Compact any exploded words
  85. // This corrects words like:  j a v a s c r i p t
  86. // These words are compacted back to their correct state.     
  87.     $words = array('javascript', 'vbscript', 'script', 'applet', 'alert', 'document', 'write', 'cookie', 'window');
  88.     foreach ($words as $word) {
  89.         $temp = '';
  90.         for ($i = 0; $i < strlen($word); $i++) {
  91.             $temp .= substr($word, $i, 1)."\s*";
  92.         }
  93.      
  94.         $temp = substr($temp, 0, -3);
  95.         $data = preg_replace('#'.$temp.'#s', $word, $data);
  96.         $data = preg_replace('#'.ucfirst($temp).'#s', ucfirst($word), $data);
  97.     }
  98.  
  99.  
  100. // Remove disallowed Javascript in links or img tags     
  101.     $data = preg_replace("#<a.+?href=.*?(alert\(|alert&\#40;|javascript\:|window\.|document\.|\.cookie|<script|<xss).*?\>.*?</a>#si", "", $data);
  102.     $data = preg_replace("#<img.+?src=.*?(alert\(|alert&\#40;|javascript\:|window\.|document\.|\.cookie|<script|<xss).*?\>#si","", $data);
  103.     $data = preg_replace("#<(script|xss).*?\>#si", "", $data);
  104.  
  105. // Remove JavaScript Event Handlers
  106. // Note: This code is a little blunt.  It removes
  107. // the event handler and anything up to the closing >,
  108. // but it's unlikely to be a problem.
  109.  
  110.     $data = preg_replace('#(<[^>]+.*?)(onabort|onactivate|onafterprint|onafterupdate|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditfocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onblur|onbounce|oncellchange|onchange|onclick|oncontextmenu|oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondblclick|ondeactivate|ondrag|ondragend|ondragenter|ondragleave|ondragover|ondragstart|ondrop|onerror|onerrorupdate|onfilterchange|onfinish|onfocus|onfocusin|onfocusout|onhelp|onkeydown|onkeypress|onkeyup|onlayoutcomplete|onload|onlosecapture|onmousedown|onmouseenter|onmouseleave|onmousemove|onmouseout|onmouseover|onmouseup|onmousewheel|onmove|onmoveend|onmovestart|onpaste|onpropertychange|onreadystatechange|onreset|onresize|onresizeend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onselect|onselectionchange|onselectstart|onstart|onstop|onsubmit|onunload)[^>]*>#iU',"\\1>",$data);
  111.  
  112.  
  113. // Sanitize naughty HTML elements
  114. // If a tag containing any of the words in the list
  115. // below is found, the tag gets converted to entities.   
  116. // So this: <blink>
  117. // Becomes: &lt;blink&gt;     
  118.     $data = preg_replace('#<(/*\s*)(alert|vbscript|javascript|applet|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|layer|link|meta|object|plaintext|style|script|textarea|title|xml|xss|lowsrc)([^>]*)>#is', "&lt;\\1\\2\\3&gt;", $data);
  119.              
  120.  
  121. // Sanitize naughty scripting elements
  122. // Similar to above, only instead of looking for
  123. // tags it looks for PHP and JavaScript commands
  124. // that are disallowed.  Rather than removing the
  125. // code, it simply converts the parenthesis to entities
  126. // rendering the code un-executable.
  127. // For example:    eval('some code')
  128. // Becomes:        eval('some code')
  129.     $data = preg_replace('#(alert|cmd|passthru|eval|exec|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2(\\3)", $data);
  130.                                              
  131. // Final clean up
  132. // This adds a bit of extra precaution in case
  133. // something got through the above filters
  134.     $bad = array(
  135.             'document.cookie'    => '',
  136.             'document.write'    => '',
  137.             'window.location'    => '',
  138.             "javascript\s*:"    => '',
  139.             "Redirect\s+302"    => '',
  140.             '<!--'            => '&lt;!--',
  141.             '-->'            => '--&gt;'
  142.     );
  143.      
  144.     foreach ($bad as $key => $val)    {
  145.             $data = preg_replace("#".$key."#i", $val, $data);
  146.     }
  147.  
  148. ////////// END NEW TESTS /////////////////////////////////////////////////////
  149.  
  150.  
  151.  
  152.     if($dlen != '0'){
  153.         $data = substr($data, 0, $dlen);
  154.     }
  155.  
  156.     if($dtype == '1'){
  157.         // allow only numeric characters, space, period, and '-' 
  158.         $data = preg_replace("/[^0-9\-\ \.]/",'', $data);
  159.     }
  160.      
  161.     if($dtype == '2'){
  162.         // allow only alpha characters, '_' and space 
  163.         $data = preg_replace("/[^a-zA-Z~\ \_]/",'', $data);
  164.     }
  165.      
  166.     if($dtype == '3'){
  167.         // allow only alphanumeric characters, space, '_', period, colon, and '-'
  168.         $data = preg_replace("/[^0-9a-zA-Z~\-\ \.\:\_]/",'', $data);
  169.     }
  170.      
  171.     if($dtype == '4'){
  172.         // allow only alphanumeric characters w/ punctuation + carriage returns
  173.         $data = preg_replace("|[^0-9a-zA-Z~@#$%=:;_, \\n\\\!\^&\*\(\)\-\+\.\?\/\'\"]|",'', $data);
  174.     }
  175.  
  176.     if($dtype == '5'){
  177.         // specifically for email validation 
  178.         $data = preg_replace("|[^0-9a-zA-Z@_\-\.]|",'', $data);
  179.     }
  180.  
  181.     $data = trim($data);
  182.  
  183.     return $data;
  184. }
  185. // end sanitize 
  186. ////////////////////////////////////////////
  187.  
  188.  
  189. ?>

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 Nico; 10-03-09 at 06:12 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #2 (permalink)  
Old 10-02-09, 11:19 PM
wirehopper's Avatar
wirehopper wirehopper is offline
Community Liaison
 
Join Date: Feb 2006
Posts: 2,046
Thanks: 9
Thanked 64 Times in 62 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 TechnoratiFurl this Post!Share 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,030
Thanks: 14
Thanked 34 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 (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share 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 TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #5 (permalink)  
Old 10-03-09, 05:40 PM
wirehopper's Avatar
wirehopper wirehopper is offline
Community Liaison
 
Join Date: Feb 2006
Posts: 2,046
Thanks: 9
Thanked 64 Times in 62 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 TechnoratiFurl this Post!Share 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 TechnoratiFurl this Post!Share 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,030
Thanks: 14
Thanked 34 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 (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share 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 TechnoratiFurl this Post!Share 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,030
Thanks: 14
Thanked 34 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 (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share 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 TechnoratiFurl this Post!Share 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 07:24 PM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.