Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2002
    Location
    Arcadia, CA
    Posts
    148

    validate usernames and/or filenames in PHP

    Hello guys. I did a search, and I'm surprised I didn't find any results for this.

    Has anyone got an easy way to validate a username on a signup form (ie only numbers/letters)? The way I do it just seems to me to be about the longest way to do an easy job. Someone must have a good script and/or some command in php I haven't found.

    Also, I run a site in which you can store files, and I want to validate the filenames to be (obviously) something valid in linux.

    Two similar ideas. Thanks in advance for the help.

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    94
    if (!eregi ("^[[:alnum:]_]{6,16}$", stripslashes(trim($_GET["username"])))) { $errors = "{$errors}Please enter a valid username!<br />\n"; }
    Please, call me RoF

    Cheap - Affordable Resellers - http://www.gazzin.com

  3. #3
    Join Date
    Jun 2004
    Location
    Digital Texas
    Posts
    55
    PHP Code:
    $valid = array('php','html','exe','xhtml');

    $file 'myfile.php';

    list(
    $file[EXT]) = array_pop(explode('.',$file));

    if (
    in_array($valid,$file[EXT])) {
    echo 
    'This is a valid file';
    } else {
    echo 
    'Not a valid file';


  4. #4
    Join Date
    Oct 2003
    Location
    Georgetown, Ontario
    Posts
    1,761
    Originally posted by Nyture
    PHP Code:
    $valid = array('php','html','exe','xhtml');

    $file 'myfile.php';

    list(
    $file[EXT]) = array_pop(explode('.',$file));

    if (
    in_array($valid,$file[EXT])) {
    echo 
    'This is a valid file';
    } else {
    echo 
    'Not a valid file';


    What if the extension has 2 dots? ie. *.tar.gz?
    ии Repeat after me... ProSupport is the best... Prosupport is... ии
    ProSupport Host Support System - OUT NOW! Grab a copy yourself and see what the hype is about!
    VertiHost Inc. - We run a quality business. Do you?

  5. #5
    Join Date
    Jun 2004
    Location
    Digital Texas
    Posts
    55
    renders it null and void. It only checks for the part after the last dot, because thats the actual extension.

  6. #6
    Join Date
    Oct 2003
    Location
    Georgetown, Ontario
    Posts
    1,761
    Originally posted by Nyture
    renders it null and void. It only checks for the part after the last dot, because thats the actual extension.
    Yes, it does. But you really should check for the mime type of the file since you can easily change the extension to whatever you want.
    ии Repeat after me... ProSupport is the best... Prosupport is... ии
    ProSupport Host Support System - OUT NOW! Grab a copy yourself and see what the hype is about!
    VertiHost Inc. - We run a quality business. Do you?

  7. #7
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Actually, mime-types are not reliable. Instead, a command like 'file' that tells you actually what the file contains would be more appropriate.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  8. #8
    Join Date
    May 2004
    Location
    Singapore
    Posts
    262
    Has anyone got an easy way to validate a username on a signup form (ie only numbers/letters)?
    In this special case, use ctype_alnum(), possibly with strlen(). For more complex requirements, use regular expressions, such as what RangerOfFire suggested.
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  9. #9
    Join Date
    Jun 2002
    Location
    Arcadia, CA
    Posts
    148
    Thanks so much for your suggestions so far everyone. You've been lots of help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •