Jump to content

Checking user input.


bobleny

Recommended Posts

So strlen() ill give me the number of characters. like

$minname = strlen($_POST['name'])

if ($minname => 3)
{
$good = TRUE
}

So that should work, I haven’t tested it. But then how do I say, for instance, name cannot contain spaces or @s or periods and what not?
Link to comment
Share on other sites

[!--quoteo(post=383599:date=Jun 13 2006, 08:28 PM:name=Bob Leny)--][div class=\'quotetop\']QUOTE(Bob Leny @ Jun 13 2006, 08:28 PM) [snapback]383599[/snapback][/div][div class=\'quotemain\'][!--quotec--]So that should work, I haven’t tested it. But then how do I say, for instance, name cannot contain spaces or @s or periods and what not?
[/quote]

Regular Expressions.
[a href=\"http://www.php.net/pcre\" target=\"_blank\"]http://www.php.net/pcre[/a]

[code]if (preg_match("/[@.]/", $str) {
   // @ or . found
}[/code]
Link to comment
Share on other sites

That will be good for sertant feilds but is there a previuslly made script or function or something that only allows letters and numbers?

Actually I have this,

[code]    if (strpos('@', $_POST['forumsignupname']) !== false || strpos('.', $_POST['forumsignupname']) !== false ||  strpos(' ', $_POST['forumsignupname']) !== false || strpos('/', $_POST['forumsignupname']) !== false || strpos('\', $_POST['forumsignupname']) !== false || strpos('#', $_POST['forumsignupname']) !== false || strpos('!', $_POST['forumsignupname']) !== false || strpos('%', $_POST['forumsignupname']) !== false || strpos('^', $_POST['forumsignupname']) !== false || strpos('&', $_POST['forumsignupname']) !== false || strpos('*', $_POST['forumsignupname']) !== false || strpos('(', $_POST['forumsignupname']) !== false || strpos(')', $_POST['forumsignupname']) !== false || strpos('~', $_POST['forumsignupname']) !== false || strpos('`', $_POST['forumsignupname']) !== false || strpos('-', $_POST['forumsignupname']) !== false || strpos('+', $_POST['forumsignupname']) !== false || strpos('=', $_POST['forumsignupname']) !== false || strpos('?', $_POST['forumsignupname']) !== false || strpos('<', $_POST['forumsignupname']) !== false || strpos('>', $_POST['forumsignupname']) !== false || strpos(',', $_POST['forumsignupname']) !== false || strpos('{', $_POST['forumsignupname']) !== false || strpos('}', $_POST['forumsignupname']) !== false || strpos('[', $_POST['forumsignupname']) !== false || strpos(']', $_POST['forumsignupname']) !== false || strpos('|', $_POST['forumsignupname']) !== false || strpos(';', $_POST['forumsignupname']) !== false || strpos(':', $_POST['forumsignupname']) !== false || strpos('"', $_POST['forumsignupname']) !== false)[/code]

But that wont work! I don't even have to try that to know it wont work! Im useing that coloring thing with PHP designer and half of its orange! lol so that will work for some stuff. Is there something I can use to only allow letters and numbers?
Link to comment
Share on other sites

Do yourself a favor and use regex:

[code]$name = trim($_POST['forumsignupname']);
if (preg_match("/\W/", $name)) {
   // error, characters other than letters, numbers and the underscore "_" were found;
}[/code]

Because if you use your current code, besides the terrible ugliness, you will have to add each symbol you don't want manually. And there are lots of them.
Link to comment
Share on other sites

i would only use regexp if i'm trying to match a pattern and not something definite.

for example, if you know that the user can only input the following words (perhaps he's limited to these choices because it's input via a select menu, ignoring the fact that forms can be spoofed):
* VISA
* Mastercard
* American Express

now since you know for sure that he will only enter those 3 words, it's ok to do an explicit check with an if condition.


however, if your user is allowed to enter an email address, you would simply have to make sure the email conforms to the standard format of an email which is where regexp comes in because it does matching by a sequence of patterns. Apart from this being the most sensible way to do it, regexp is also fractionally slower than most other string functions because of it's nature.




back to the previous case, if you know exactly what to expect, do an explicit string match. use [a href=\"http://www.php.net/ctype\" target=\"_blank\"]ctype functions[/a]. your version of PHP probably has this feature by default unless you were once neighbours with Fred and Wilma :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.