-
Dec 22nd, 2012, 06:37 AM
#1
Thread Starter
Lively Member
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($parser, XML_OPTION_CASE_FOLDING, 0);
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 .
-
Dec 22nd, 2012, 01:02 PM
#2
Thread Starter
Lively Member
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.
-
Dec 29th, 2012, 10:53 PM
#3
Re: Validate inputs
Originally Posted by deve
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($text, ENT_XML1)
-
Dec 30th, 2012, 04:55 AM
#4
Thread Starter
Lively Member
Re: Validate inputs
Originally Posted by penagate
You could use the htmlspecialchars function to encode the text before inserting it into the XML document:
PHP Code:
htmlspecialchars($text, ENT_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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|