Forum Moderators: coopster
I have a messgae board and am using preg_match to stop users posting random names or characters into the name field. I have it working a treat for words but i get the error message
Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 0
i am using the following code
elseif (preg_match ("/secret/", $name) ¦¦ preg_match ("/SECRET/", $name) ¦¦ preg_match ("/x/", $name) ¦¦ preg_match ("/?/",$name))
{
echo "Please use the message board properly and enter a valid name. Inappropriate use will result in a ban. ";
like i say everything works up until the last ? field is there something different i need to put in the last field for special characters... Looking for any replies to help
Thanks
i have a preg_match to check to see if value of $id is a digit like this:
First, is zero a valid digit in your scenario?
If it's "anything but zero" you don't need a preg. is_numeric() works, but that includes zero.
if ($var > 0) {
// simple enough
}
You can use is_numeric if zero is included, or
Do this first to make sure user hasn't errantly included a space or touched some other key:
$var = preg_replace('/[^\d]+/','',$var);
then
if (preg_match('/^\d+$/',$var)) { begins and ends with any number of digits
if (preg_match('/^\d$/',$var)) { begins and ends with exactly one digit
if (preg_match('/^\d{2}$/',$var)) { begins and ends with exactly two digits
if (preg_match('/^\d{4,7}$/',$var)) { begins and ends with anywhere from four to seven digits
i am using the following code
Try this.
// top of script for ease of use:
$bannedNames = Array (
'secret',
'x',
'spam-boy'
);
// This is anything NOT these characters; that is,
// it will throw away anything NOT in this list.
// No need for a-z, we will use case insensitive modifier.
$allowed_chars = 'A-Z0-9\-_'; // Escape dash, not a range
$isValid=1;
$name=preg_replace("/[^$allowed_chars]+/i",$_POST['new_user_name']); // Do this first!
foreach ($bannedNames as $nm) {
if (preg_match("/^$nm$/i",$name)) { $isValid=0; } // note case insensitive
if ($isValid==0) { break; }
}
if ($isValid==0) {
echo "Please use the message board properly and enter a valid name. Inappropriate use will result in a ban. ";
exit;
}
Warning: Wrong parameter count for preg_replace() in addmessage.php on line 32.
I will post here my exact script so that you can see what i have happening, very basic but works a treat. Would like to stop special characters being inserted and any numbers to the name field.:
<body>
<div align="center">
<img src="christmas/header.jpg">
</div>
Add your Christmas message below:
<p>
<div align="center">
<div class="board">
<?php
if ($_POST["submit"] != "")
{
$ip = $REMOTE_ADDR;
$name = $_POST["name"];
$message = $_POST["message"];
$postedDate = date("H:i:s D dS M Y");
if (!$name ¦¦ !$message ¦¦ $name == "n/a" ¦¦ $name == "N/A")
{
echo "<p><b>Please enter a valid name and message!</b></p>";
}
elseif ($ip == "" ¦¦ $ip == "")
{
echo "You are no longer authorised to post messages.";
exit();
}
elseif (preg_match ("/secret/", $name) ¦¦ preg_match ("/SECRET/", $name) ¦¦ preg_match ("/admirer/", $name) ¦¦ preg_match ("/ADMIRER/", $name) ¦¦ preg_match ("/Secret/", $name) ¦¦ preg_match ("/Admirer/", $name) ¦¦ preg_match ("/x/", $name) ¦¦ preg_match ("/0-9/",$name))
{
echo "Please use the message board properly and enter a valid name. Inappropriate use will result in a ban. ";
exit();
}
else {
$query = "insert into christmas (name,message,ip,postedDate) values ('$name', '$message', '$ip', '$postedDate')";
mysql_query($query, $connection) or die(mysql_error());
echo "<p>Thank you $name, your message has been added to the Christmas Message Board.</p><p>Click <a href=\"index.php?page=christmas/index.php\">here</a> to return to the message board.<hr /></p>";
exit();
}
}
?>
<form method="post" action="index.php?page=christmas/addmessage.php">
Your Name:<br>
<input type="text" name="name" size="40" class="input" value="<?php echo $_POST["name"]?>" ><p />
Your Message:</b><br>
<textarea name="message" rows="10" cols="42" class="input"><?php echo $_POST["message"]?></textarea><p>
<input type="submit" value="Ho Ho Ho" name="submit" class="button">
</form>
</div>
</div>
</body>
$name=preg_replace("/[^$allowed_chars]+/i",$_POST['new_user_name']);
should be
$name=preg_replace("/[^$allowed_chars]+/i",'',$_POST['new_user_name']);
Or works, but you will find if you want to keep changing that list you will constantly add more or's. The looking approach works fine, allowing you to place your variables in an array at the top of the script or in a config file/database.