Jump to content

prevent xss attack in textfield


MDanz

Recommended Posts

i have this form

 

 <form action="search.php" method="get">
<center>
   <input name="search" type="text" value="" size="25" />
   <input type="submit" name="submit" value="search">

</center>

</form>

 

i want only text and numbers to be input .... no html

 

how to do this?

 

$search = eregi_replace("([A-Z0-9]+)","",$_GET['search']);

If you want only Alpha-Numeric characters, then try the strip_tags() function. You can also do something like this if you find Regular-Expressions too inefficient/confusing:

 

<?php
$_GET['search'] = 'This string contains only Alphabetic characters.';
if(ctype_alnum($str) === false) exit('Invalid characters were detected in your search-query');
else {
	//Execute your code used for valid search-queries
}
?>

 

See ctype_alnum() in the PHP Manual.

 

Don't forget to encode your $_GET['search'] variable with something such as urlencode() before passing it through the URL-Query-String.

i have this form

 

 <form action="search.php" method="get">
<center>
   <input name="search" type="text" value="" size="25" />
   <input type="submit" name="submit" value="search">

</center>

</form>

 

i want only text and numbers to be input .... no html

 

how to do this?

 

Just use the real_escape_string() function.

 

Internet cut out sorry for late reply.

i have this form

 

 <form action="search.php" method="get">
<center>
   <input name="search" type="text" value="" size="25" />
   <input type="submit" name="submit" value="search">

</center>

</form>

 

i want only text and numbers to be input .... no html

 

how to do this?

 

Just use the real_escape_string() function.

 

Internet cut out sorry for late reply.

 

Using mysql_real_escape_string() won't restrict the query to only Alpha-Numeric characters. It will only escape appropriate characters with a back-slash ('\'). mysql_real_escape_string() should only be used for MySQL-Queries.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.