Jump to content
MakeWebGames

Mccode username


PoNiT
 Share

Recommended Posts

Can anyone please give me a php code that stops users from adding any special characters in their username.

I need to add this in register.php and in preferences.php.

As when people have a ' in their name, it acts as an sql injection and causes SERIOUS BUGS!

I dont know what code to do so only letters and numbers caan be used in usernames.

Please can someone help fast?

Link to comment
Share on other sites

Re: Mccode username

Here is how the Horizons Game Engine does user name validation, and it works 100% guaranteed, with no possibility of anything except letters, numbers, spaces and underscores.

 

	if (!isset($_REQUEST['ulogin'])) {
	$reg_error = "[*]Please submit a login name.";
	$form_highlight[1] = true;
} else {
	$ulogin = $_REQUEST['ulogin'];
	if (strlen(trim($ulogin)) < 7) {
		$reg_error = "[*]The login name must be at least 7 characters long.";
		$form_highlight[1] = true;
	} elseif (strlen(trim($ulogin)) > 20) {
		$reg_error = "[*]The login name must be 20 characters or less.";
		$form_highlight[1] = true;
	} elseif (!ctype_alnum(ereg_replace('[ _]', '', $ulogin))) {
		$reg_error = "[*]The login name may only contain letters, numbers, spaces, or underscores.";
		$form_highlight[1] = true;
	}
}

 

This portion here:

 

if (!ctype_alnum(ereg_replace('[ _]', '', $ulogin)))

 

is the portion that does what you want. ;)

it will detect an invalid name

Link to comment
Share on other sites

Guest Anonymous

Re: Mccode username

 

$new_name = isset($_POST['new_name']) && is_string($_POST['new_name']) && preg_match("`^[a-z0-9_]{4,15}$`ims", trim($_POST['new_name'])) ? trim($_POST['new_name']) : null;
if (is_null($new_name))
echo "invalid name";
else
// check for duplicates ...

 

Just change the {4,15} to suit - minimum length, maximum length.

Link to comment
Share on other sites

Guest Anonymous

Re: Mccode username

That's dependent on your context.

 

if (condition)
true_statement
else
false_statement


if (condtion)
{
true_statement
}
else
{
false_statement
}

 

The first form works only for single line statements, however the second form is needed when you are using multiple line statements.

Personally, I feel the second form used throughout, results in cleaner, more legible code, however I do mix the two in my own sources from time to time.

However... be that as it may -- asking this type of questions shows a lack of understanding of the language itself. Perhaps you would be better employed reading the PHP manual itself, and looking at much smaller games rather than diving into a DBS/MCcode's based game which is fraught with problems.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

 Share

×
×
  • Create New...