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
  #11 (permalink)  
Old 10-07-09, 09:52 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
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.
return preg_match("/^[a-zA-Z\._]{1,15}/")

I believe that's correct, although make sure you escape the period as shown above since it's a meta character. I'm not sure if you want the dollar-sign meta in there- it indicates matching at the end of the line (or before newline at the end) and I don't know if that's what you really want to do or not.

You might grab a copy of Regex Buddy and give it a try, I find it invaluable in creating and working out regex strings.
__________________
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]

Last edited by End User; 10-07-09 at 09:54 AM.
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-07-09)
  #12 (permalink)  
Old 10-30-09, 05:31 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
I was experimenting with this again and I can't get it to post any of the information to the database. I have it posting back to the same page and when I hit "Submit" it shows the page minus the forum. I don't have a problem there, from what I know.

However, the information isn't being recorded into the database. I modified the code a bit since I last posted so I'm posting it again. I'll keep playing with it and if I get it to work I'll report back.

The index.php file:
php Code:
  1. <?php
  2. include('../includes/constants.php');
  3. include('../includes/sanitize.php');
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Coming Soon!</title>
  10. </head>
  11.  
  12. <body>
  13. We are trying to get things running, please pardon the virtual dust!
  14. <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>
  15. <?php
  16. if (isset($_POST&#91;'submitted'])) {
  17.         require_once(MYSQL);
  18.        
  19.         //Sanitize data before validation
  20.         $c_fname = sanitize(3, 15, $_POST&#91;'first_name']);
  21.         $c_lname = sanitize(3, 25, $_POST&#91;'last_name']);
  22.         $c_email = sanitize(5, 60, $_POST&#91;'email']);
  23.         $c_email_2 = sanitize(5, 60, $_POST&#91;'email_2']);
  24.        
  25.         //Validate the first name
  26.         return preg_match("/^
  27.         [a-zA-Z]
  28.         [a-zA-Z.-]{1,15}/", $c_fname);
  29.        
  30.         //Validate last name
  31.         return preg_match("/^
  32.         [a-zA-Z]
  33.         [a-zA-Z-]{1,25}/", $c_lname);
  34.        
  35.         //Is the email valid and not empty?
  36.         if (empty($c_email_2)) {
  37.             echo '<p>Please enter your email address in both fields.</p>';
  38.         } else if ($c_email != $c_email_2) {
  39.             echo '<p>The email fields do not match. Please confirm them again.</p>';
  40.         } else {
  41.             function isValidEmail( $c_email = null ) {
  42.             return preg_match( "/^
  43.             [\d\w\/+!=#|$?%{^&}*`'~-]
  44.             [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
  45.             [A-Z0-9]
  46.             [A-Z0-9.-]{1,60}
  47.             [A-Z0-9]\.
  48.             [A-Z]{2,6}/", $c_email);
  49.                 }
  50.         }
  51.  
  52.         if (!empty($c_email) && $c_email == $c_email_2) {
  53.             $access = "SELECT Email FROM customers WHERE Email='$c_email'";
  54.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  55.         }
  56.        
  57.         //Insert information if email is unique:
  58.         if (mysqli_num_rows($r) == 0) {
  59.             $access = "INSERT INTO customers (email, first, last) VALUES ('$c_email', '$c_fname', '$c_lname')";
  60.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  61.         } else {
  62.             echo '<p>Your email address could not be recorded due to an error, please try again.</p>';
  63. }
  64. mysqli_close($dbc);
  65. } // End Submit IF.
  66. ?>
  67. <form action="index.php" method="post">
  68. <fieldset>
  69. <p><b>First name:</b> <input type="text" name="first_name" size="20" maxlength="15" value="<?php if(isset($c_fname)) echo $c_fname; ?>" /></p>
  70. <p><b>Last name:</b> <input type="text" name="last_name" size="20" maxlength="25" value="<?php if(isset($c_lname)) echo $c_lname; ?>" /></p>
  71. <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="60" value="<?php if(isset($c_email)) echo $c_email; ?>" /> (Required)</p>
  72. <p><b>Confirm email address:</b> <input type="text" name="email_2" size="30" maxlength="60" value="<?php if(isset($c_email_2)) echo $c_email_2; ?>" /> (Required)</p>
  73. <br />
  74. </fieldset>
  75. <div class="center"><input type="submit" name="submit" value="Register" /></div>
  76. <input type="hidden" name="submitted" value="TRUE" />
  77. </form>
  78. </body>
  79. </html>

Constants.php:
php Code:
  1. <?php
  2. define('BASE_URL', 'http://mysite.com');
  3. define('MYSQL', '../SQL/mysqli_connect.php');
  4. ?>

php Code:
  1. <?php
  2. //Set database access information:
  3. DEFINE('DB_USER', '********');
  4. DEFINE('DB_PASSWORD', '********');
  5. DEFINE('DB_HOST', '*****');
  6. DEFINE('DB_NAME', '******');
  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. ?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #13 (permalink)  
Old 11-01-09, 11:01 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Sorry for the bump, it seems the forum has a time limit before you can't edit a post anymore which is why I am posting this instead of editing my previous post:

I was debugging my form earlier and decided to add echoes due to it's strange behavior to see if I could tell where it's getting stuck. I added echoes after specific events so I could tell where the code stops processing.


The code appears to be getting stuck on the preg_matches, here is the php part of the code. Other than the echoes I haven't changed any code from my previous post. If I fix the problem I'll report back:

php Code:
  1. <?php
  2. if (isset($_POST&#91;'submitted'])) {
  3.         echo "Successful submit!";
  4.         require_once('../includes/sanitize.php');
  5.    
  6.         //Sanitize data before validation
  7.         $c_fname = sanitize(3, 15, $_POST&#91;'first_name']);
  8.         $c_lname = sanitize(3, 25, $_POST&#91;'last_name']);
  9.         $c_email = sanitize(5, 60, $_POST&#91;'email']);
  10.         $c_email_2 = sanitize(5, 60, $_POST&#91;'email_2']);
  11.        
  12.         echo "Sanitization complete!";
  13.        
  14.         require_once(MYSQL);
  15.        
  16.         echo "MySQL included!";
  17.        
  18.         //Validate the first name
  19.         return preg_match("/^[a-zA-Z\.]{1,15}/", $c_fname);
  20.        
  21.         echo "Preg_match for first name should have worked!";
  22.        
  23.         //Validate last name
  24.         return preg_match("/^
  25.         [a-zA-Z\-.]{1,25}/", $c_lname);
  26.        
  27.         echo "Name(s) validated!";
  28.        
  29.         //Is the email valid and not empty?
  30.         if (empty($c_email_2)) {
  31.             echo '<p>Please enter your email address in both fields.</p>';
  32.         } else if ($c_email != $c_email_2) {
  33.             echo '<p>The email fields do not match. Please confirm them again.</p>';
  34.         } else {
  35.             function isValidEmail( $c_email = null ) {
  36.             return preg_match( "/^
  37.             [\d\w\/+!=#|$?%{^&}*`'~-]
  38.             [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
  39.             [A-Z0-9]
  40.             [A-Z0-9.-]{1,60}
  41.             [A-Z0-9]\.
  42.             [A-Z]{2,6}/", $c_email);
  43.                 }
  44.         }
  45.        
  46.         echo "Emails are valid!";
  47.  
  48.         if (!empty($c_email) && $c_email == $c_email_2) {
  49.             $access = "SELECT Email FROM customers WHERE Email='$c_email'";
  50.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  51.         }
  52.        
  53.         //Insert information if email is unique:
  54.         if (mysqli_num_rows($r) == 0) {
  55.             $access = "INSERT INTO customers (Email, First, Last) VALUES ('$c_email', '$c_fname', '$c_lname')";
  56.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  57.         } else {
  58.             echo '<p>Your email address could not be recorded due to an error, please try again.</p>';
  59. }
  60. mysqli_close($dbc);
  61. } // End Submit IF.
  62. ?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #14 (permalink)  
Old 11-01-09, 11:48 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
You might want to change the 'return preg_match' lines to:

PHP Code:

$bResult=preg_match( ... );

if (!
$bResult)  /* If it didn't match okay */
  
return false;

/* Match okay, continue processing */ 
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 wirehopper For This Useful Post:
PopSmith (11-02-09)
  #15 (permalink)  
Old 11-02-09, 12:46 AM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks wirehopper, that did the trick and now the form is fully processing.

However, it looks like either the preg_match or the sanitizer script is inserting the letter "x" into the beginning (and other places) in the fields. I'll see if I can figure out the problem but here is what it's doing:

As a test I entered the following into the forum; all without the quotes and the output was placed successfully into the MySQL database:

First name: "Franky"
Output: "xFranky"

Last name: "Miller"
Output: "xMilxer"

Email (same in both fields): "franky@mailinator.com"
Output: "xfraxnky@xmaixlinxator.xcom"

If I figure out a solution I'll report back.

EDIT: I used echo so I could see the variables pre- and post-sanitize by simply echoing both the variables and it looks like the sanitizer is placing the "x" into the variable, just FYI.

Last edited by PopSmith; 11-02-09 at 01:05 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #16 (permalink)  
Old 11-02-09, 08:01 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
However, it looks like either the preg_match or the sanitizer script is inserting the letter "x" into the beginning (and other places) in the fields.
Mighty weird....I've not see this issue before so I'm not sure what to say. I took a quick look but didn't see anywhere that the sanitizer should do this, but maybe I missed something. Did you modify it at all, add anything, remove anything, etc?

There are a lot of 'x' chars used in the sanitizer matching items like 'x00' (the null character) and 'x20' (a space), but I can't think of any reason it would substitute just the 'x'.

Try commenting out the sanitizer lines and see if it passes the data normally. (Or comment out the preg_match() lines and see what happens.)
__________________
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
  #17 (permalink)  
Old 11-02-09, 08:06 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Check the number of backslashes in the related preg calls.

Sometimes, when code is posted here, backslashes get removed.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #18 (permalink)  
Old 11-02-09, 05:38 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
Mighty weird....I've not see this issue before so I'm not sure what to say. I took a quick look but didn't see anywhere that the sanitizer should do this, but maybe I missed something. Did you modify it at all, add anything, remove anything, etc?
I only added one modification to the script. I did it so I could remember how to use the script in case I ever forget.

php Code:
  1. /////////////////////////////////////////////////////////////
  2. // Example- allow numbers and letters up to 50 chars in the variable "$foo"
  3. // $foo = sanitize(2, 50, $_POST['name']);
  4. /////////////////////////////////////////////////////////////
  5.  

I added that directly below the dtype explanations.

Quote:
Originally Posted by End User View Post
There are a lot of 'x' chars used in the sanitizer matching items like 'x00' (the null character) and 'x20' (a space), but I can't think of any reason it would substitute just the 'x'.

Try commenting out the sanitizer lines and see if it passes the data normally. (Or comment out the preg_match() lines and see what happens.)
I commented out the sanitizer lines in my code and by doing so the script now prints and passes everything to the MySQL database correctly. I'm wondering if something got screwed up when I copied the sanitize script originally.

Quote:
Originally Posted by wirehopper View Post
Check the number of backslashes in the related preg calls.

Sometimes, when code is posted here, backslashes get removed.
Everything there looks OK. However, I'll go over the sanitizer script again just to double check it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #19 (permalink)  
Old 11-02-09, 06:01 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 wirehopper View Post
Check the number of backslashes in the related preg calls.
Sometimes, when code is posted here, backslashes get removed.
Hmmm, I'm betting that's probably it.
__________________
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
  #20 (permalink)  
Old 11-02-09, 11:00 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
I believe I found and fixed the error. I wasn't quite sure how to best go about finding it so I went line by line, commenting each one out and uploading it.

Turns out the error was in this section:

php Code:
  1. // URL Decode
  2. // Just in case stuff like this is submitted:
  3. // <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
  4. // Note: Normally urldecode() would be easier but it removes plus signs
  5.     $data = preg_replace("/([a-z0-9]{3})/i", "&#x\\1;", $data);
  6.     $data = preg_replace("/%([a-z0-9]{2})/i", "&#x\\1;", $data);

When I commented out $data = preg_replace("/([a-z0-9]{3})/i", "&#x\\1;", $data); the error went away. By looking at the next line I figured "Put a % in that same spot and try it". That fixed the problem!

Basically it's supposed to be:
$data = preg_replace("/%([a-z0-9]{3})/i", "&#x\\1;", $data);

Instead of:
$data = preg_replace("/([a-z0-9]{3})/i", "&#x\\1;", $data); <-- Missing % after the forward slash.

However, I don't see how that line was causing an "x" to be added to the data.
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 01:20 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.