Advertise

Monday, 16 September 2013

Secure way to write a Code for Username, Email & url or link Input by Users.

There has been many question on internet on how to write a code for secured username, email or url input. So , here is my code which is the best and secured way to take an input from user for Following Field :-

1. Username.
2. Email.
3. Link or URL.

Username

If you would like to take an input as username, here is PHP code, along with HTML form.

PHP Code:
<form metho="post" action="#">
<
label>Username</label>
<
input type="text" name="username" placeholder="Enter Username.." />
<
input type="submit" value"Submit" />
</
form>



php// As we know, username can only have "alphabets from a-z or A-Z" and "digits" from '0-9' or '-,_,.'
//So, we will only accept above stuff
// If you see below code in internet anywhere,
// than it has been posted by me only until unless some faggot copies it and says his own.
$input "";$random mt_rand(5,8);
function 
validate_username($input)
{
    
$allowed = array(".""-""_"); // you can add here more value, you want to allow.
    
if(ctype_alnum(str_replace($allowed''$input ))) {
        return 
$input;
    } else {
        
$input "Invalid Username ".$random;
        return 
$input;
    }
}
$username = isset($_POST['username']) ? validate_username($_POST['username']) : " ";
if(
strpos($username$random))
{
echo 
'Please Enter only alphanumeric or ._- in username':
} else {
// Process your code.}?>

Email Validation


Many Developer blindly trust Javascript for validation, this is the world's most stupid stuff you would do since you are developer. I always say, Server Side Validation is Must, Must and Must.

Here is PHP code :-

PHP Code:
<form method="post" action="#">
<
label>Email :- </label>

<
input type="email" name="myMail" placeholder="Enter Email.." />
<
input type="submit" value="Submit" />
</
form>


php
$email 
= isset($_POST['myMail']) ? $_POST['myMail'] : " ";
if(!
filter_var($emailFILTER_VALIDATE_EMAIL) )
{
echo 
"Invalid Email";
} else {
echo 
"Processing..";//Process your mail..}?>
But Above FILTER_VALIDATE_EMAIL will also validate 'localhost' or other stuff, so not that accurate, I prefer my own Coded php function which i feel is better than FILTER_VALIDATE_EMAIL, though you may combine them and use them, it will be more simple and cool  here is mine.
PHP Code:
// In most of the cases, type=email will do, but it only checks if string has "@",
// we will go 1 step beyond than that and will see if such host exists or not.
$email = isset($_POST['myMail']) ? $_POST['myMail'] : " ";$fire explode("@"$email);$present strpos($fire[1], '.');
if(
$present !== false) {
    
$semifinal gethostbyname($fire[1]);
    
// I am checking only for IPV4 ip address.
    
$final is_numeric(str_replace('.'''$semifinal));
    
$check strpos($semifinal'.');
    if(
$final) {
        echo 
"Processing..";
        
// Process Email
    
} else     {
        echo 
"Invalid Email Address";
    }
} else {
    echo 
"We dont consider email with domain something like 'localhost' so please input something example.com ";
}
?>

URL Validation


I leave this part as your homework. If not answered within a day, I will post the answer.
HINT: check email validation 
 
World of Hacker © 2011 Creative Commons License
World of Hacker by KroKite is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at http://www.worldofhacker.com.
Permissions beyond the scope of this license may be available at https://groups.google.com/forum/#!newtopic/hackerforum.