Jump to content

Recommended Posts

if($_POST['username'] != preg_replace(“/[^a-zA-Z0-9\s]/”, “”, $_POST['username'])) {
// bad name
}else{
// good name
}

 

Parse error: syntax error, unexpected '['

its referring to this

[^a-zA-Z0-9\s]

 

how can i make it work?

hmm ok i changedc it to ' instead of " and i registered under -=][\[';/. or something along those lines and it came up with You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near './.'' at line 1 hmm i wanted to make it say username invalid, instead of that.. =\

Sometimes the error says a line, but it could be the surrounding. Post 5 lines before that line and 5 after to give us a better idea of what is going on.

 

As an alternative test, you can assign the preg_replace section to its own variable and test it that way and if the error is still thrown on the line the preg is on, then yea. Something is up. However, it does not seem that it is there, given I did ran this code locally and it went through just fine and echo'd bad.

 

$username = 'test_';
if ($username != preg_replace("/[^a-zA-Z0-9\s]/", "", $username)) echo 'bad';

 

What would be a better idea, is post the username you are testing plus the 5 lines before and after the offending line.

well i edited it abit


if($_POST['username'] != preg_replace('/[^a-zA-Z0-9\s]/', '', $_POST['username'])) {
	die('Invalid characters <a href="register.php">Please try again.');
	}else{
	echo "Username valid.";
	}

hmm ok i changedc it to ' instead of " and i registered under -=][\[';/. or something along those lines and it came up with You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near './.'' at line 1 hmm i wanted to make it say username invalid, instead of that.. =\

 

This has nothing to do with the code you posted.  Are you even reading the error? You need to put more effort into this.  That error is telling you that there is an error in a sql query you are trying to perform. Do you see ANYTHING in the code you posted here that has ANYTHING to do with performing a database query?

And I told you that error has nothing to do with the code you posted. I told you it has to do with a database query you are attempting to perform.  But instead of posting relevant code, you just tell me what you want to do.  Do you expect me to bust out with a crystal ball and divine what your code is?  Use some common sense.

well i edited it abit


if($_POST['username'] != preg_replace('/[^a-zA-Z0-9\s]/', '', $_POST['username'])) {
	die('Invalid characters <a href="register.php">Please try again.');
	}else{
	echo "Username valid.";
	}

 

Why would you put a die?

If it replaces then they no longer exist.

 

Wouldn't it be something like

 

<?php
$replace = $_POST['username'] != preg_replace('/[^a-zA-Z0-9\s]/', '', $_POST['username'];
if($replace == true){
die("Invalid characters in name!");
} else {
dostuffhere
}
?>

 

 

Try this:

 

$string = $_POST['username'];

function wordsExist(&$string, $words) {
    foreach($words as &$word) {
        if(stripos($string, $word) !== false) {
            return true;
        }
    }
    return false;
}   //this function was written by someone else from PHPFreaks - Sorry if this is your code

if (wordsExist($string, array('character1','character2','character3','character4','character5',))) {
    die("Your username contains a blocked character!");
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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