GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Aug-2008, 17:16
crazygol4 crazygol4 is offline
New Member
 
Join Date: Aug 2008
Posts: 10
crazygol4 is on a distinguished road

User input filtering


Okay so I've got a form where users input basic information (name, username, password, address, etc) and I want to filter it to make sure users aren't inserting code, including whitespace, using incorrect chars, etc.

If anyone has the script to do all of this already, I would love to have it, but I have planned to build it myself. This may be an ongoing post as theres many aspects to filtering user input. BUT, my immediate question is how do I count whitespace ONLY (no text)....

This is the code that I'm using (that isn't working):
PHP Code:

$text = "The  quick brown fox";
$textcount = preg_replace('/\s\s+\s-/', '', $text, $count);
echo $count; 



Thanks in advance for help,
crazygol4
  #2  
Old 30-Aug-2008, 10:16
crazygol4 crazygol4 is offline
New Member
 
Join Date: Aug 2008
Posts: 10
crazygol4 is on a distinguished road

Re: User input filtering


Okay I've got a fix for it:

PHP Code:

$text = "The quick brown fox";
echo "<br>";
echo $text;
echo "<br>";
echo strlen($text);
echo "<br>";
$textfix = str_replace(' ', '', $text);
echo strlen($textfix);
$iswhitespace = strlen($text) - strlen($textfix);
echo "<br>";
echo $textfix;
echo "<br>";
echo $iswhitespace; 



So that code has some test features in it, the below code will do all the work without displaying, and will redirect to a different page if the string contains whitespace.

PHP Code:

$text = $_POST[yourvar];
$textfix = str_replace(' ', '', $text);
$iswhitespace = strlen($text) - strlen($textfix);
if ($iswhitespace > 0) {
    header( "Location: sendthemtoadiffpage.php");
    exit;
} 



Hope this is helpful!
-crazygol4
  #3  
Old 02-Sep-2008, 09:54
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 801
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: User input filtering


Looks like you could get a yes or no answer in one line by using: strpos( string, find [, start] )
PHP Code:

#  some command line examples:
$  php -a
Interactive mode enabled

<?php
$s1 = "stringwithnospaces";
$s2 = "string with space";
$s3 = "string with two  spaces together";

echo strpos($s1, " ");
          # <-Note: nothing returned, (as pos 0 would indicate the first character).
echo strpos($s2, " ");
6
echo strpos($s3, "  ");
15
##########
if( strpos($s1, " ") )
  echo "yes";
else
  echo "no";
no
##########
if( strpos($s2, " ") )
  echo "yes";
else
  echo "no";
yes 


Here is a reference for all the string functions: w3schools.com/php/php_ref_string.asp
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble integrating console code into GUI Barman007 Java Forum 18 15-May-2008 14:05
Scanning dilemma (fscanf and fgets) aijazbaig1 C Programming Language 10 29-Mar-2008 05:58
Need help with validating user input. Emmy C++ Forum 4 19-Aug-2007 12:07
bit operations on a string axel2500 C++ Forum 6 21-Aug-2006 16:52
Need Help on checking user input hihellochao C Programming Language 5 27-Feb-2004 14:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:37.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.