Validate inputs-VBForums
Results 1 to 4 of 4

Thread: Validate inputs

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Validate inputs

    I am trying to validate 3 input fields before they are submitted. Here is my php proccessform :

    PHP Code:
    <?
    $profiles 
    = Array();
    function 
    start_element($parser$name$attrs){
        global 
    $profiles;
        if(
    $name == "profile"){
            
    array_push($profiles$attrs);
        }
    }
    function 
    end_element ($parser$name){}
    $playlist_string file_get_contents("servers.xml");
    $parser xml_parser_create();
    xml_set_element_handler($parser"start_element""end_element");
    xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
    xml_parse($parser$playlist_string) or die("Error parsing XML document.");
    print 
    "<br />";
    if(
    $_POST['action'] == "ins"){
        
    array_push($profiles, Array(
                    
    "ip" => $_POST['ip'],
                    
    "server" => $_POST['server'],
                    
    "country" => $_POST['country']));
        
    $profiles_final $profiles;
    }else if(
    $_POST['action'] == "del"){
        
    $profiles_final = Array();
        foreach(
    $profiles as $profile){
            if(
    $profile['ip'] != $_POST['ip']){
                
    array_push($profiles_final$profile);
            }
        }
    }
    $write_string "<users>";
    foreach(
    $profiles_final as $profile){
        
    $write_string .= "<profile ip=\"$profile[ip]\" server=\"$profile[server]\" country=\"$profile[country]\" />";
    }
    $write_string .= "</users>";
    $fp fopen("servers.xml""w+");
    fwrite($fp$write_string) or die("Error writing to file");
    fclose($fp);
    print 
    "<em>Server inserted or deleted successfully :)</em><br />";
    print 
    "<a href=\"serverlist.php\" ip=\"return\">Return</a>";
    ?>
    Need to check that no special characters or just 0-9 a-Z and a few other characters like "." - "-" can be added . Becoz of the inserted data writes those 3 inputs to xml and when the xml gets writen with any special character it gives error .

    Help whould be great , thank you .

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Validate inputs

    I found this :

    if ((preg_match('/^([0-9a-zA-Z+.()-])/s+$/i', $_POST['server'])))

    But chouldnt get work , I wont to allow only those characters : 0-9 a-z A-Z / , [ ] - . = and space.

  3. #3
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,630

    Re: Validate inputs

    Quote Originally Posted by deve View Post
    Becoz of the inserted data writes those 3 inputs to xml and when the xml gets writen with any special character it gives error .
    You could use the htmlspecialchars function to encode the text before inserting it into the XML document:

    PHP Code:
    htmlspecialchars($textENT_XML1

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Validate inputs

    Quote Originally Posted by penagate View Post
    You could use the htmlspecialchars function to encode the text before inserting it into the XML document:

    PHP Code:
    htmlspecialchars($textENT_XML1

    and where do I use this exaCTly on my php code ?

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.