Secure register page without anti-sql injection

Results 1 to 10 of 10
  1. #1
    Mako is insane. ThePhailure772 is offline
    MemberRank
    Sep 2007 Join Date
    1,115Posts

    Secure register page without anti-sql injection

    Notice: this uses MD5 hashing for passwords, to disable it change:
    Code:
    statement->bindParam(3, md5($password));
    to:
    Code:
    statement->bindParam(3, $password);
    Code:
    <?php
    
    $user = 'sa';
    $pass = 'password';
    $server = 'WIN-69MQRTJIMAS\\SQLEXPRESS'; //host
    $database = 'GunzDB';
    $connectString = sprintf("odbc:Driver={SQL Server};Server={%s};Database={%s};", $server, $database);
    $pdo = new PDO($connectString, $user, $pass);
    
    if (!$pdo) 
    {
    	die("An error has occured while attempting to contact the database.<br />Please try again later.");
    }
    
    function userExists($pdo, $user)
    {
    	$statement = $pdo->prepare("SELECT COUNT(AID) as total FROM Account WHERE UserID = ?");
    	$statement->bindParam(1, $user);
    	$statement->execute();
    	$array = $statement->fetch();
    	
    	return $array['total'] > 0;
    }
    
    function emailExists($pdo, $email)
    {
    	$statement = $pdo->prepare("SELECT COUNT(AID) as total FROM Account WHERE Email = ?");
    	$statement->bindParam(1, $email);
    	$statement->execute();
    	$array = $statement->fetch();
    	
    	return $array['total'] > 0;
    }
    
    if (isset($_POST['username']))
    {
    	$user = $_POST['username'];
    	$email = $_POST['email'];
    	$password = $_POST['password'];
    	$passwordVer = $_POST['passwordVer'];
    	$name = $_POST['name'];
    	$age = (int)$_POST['age'];
    	$error = "";
    	
    	$userLen = strlen($user);
    	$passLen = strlen($password);
    	$nameLen = strlen($name);
    	$emailLen = strlen(filter_var($email, FILTER_VALIDATE_EMAIL));
    	
    	if ($userLen < 4 || $userLen > 12)
    		$error .= "Your username must be within 4 & 12 characters. <br/>";
    		
    	if ($passLen < 4 || $passLen > 16)
    		$error .= "Your password must be within 4 & 16 characters. <br/>";
    		
    	if ($password != $passwordVer)
    		$error .= "Your passwords do not match. <br/>";
    		
    	if ($nameLen < 4 || $nameLen > 64)
    		$error .= "Your name must be within 4 & 64 characters. <br/>";
    	
    	if (!$emailLen)
    		$error .= "You must use a real email account. <br/>";
    	
    	if (userExists($pdo, $user))
    		$error .= "Username is already in-use. <br/>";
    	
    	if (emailExists($pdo, $email))
    		$error .= "Email is already in-use. <br/>";
    	
    	if (!empty($error))
    		die('<div align="center">'.$error.'</div>');
    		
    	$statement = $pdo->prepare("INSERT INTO Account(UserID,UGradeID,PGradeID,Email,Name,RegDate) VALUES(?,0,0,?,?,GetDate())");
    	$statement->bindParam(1, $user);
    	$statement->bindParam(2, $email);
    	$statement->bindParam(3, $name);
    	$statement->execute();
    	
    	$statement = $pdo->prepare("SELECT @@IDENTITY");
    	$statement->execute();
    	$array = $statement->fetch();
    	
    	$aid = $array[0];
    	
    	$statement = $pdo->prepare("INSERT INTO Login(AID,UserID,Password) VALUES(?,?,?)");
    	$statement->bindParam(1, $aid);
    	$statement->bindParam(2, $user);
    	$statement->bindParam(3, md5($password));
    	$statement->execute();
    	
    	die('<div align="center"><span style="font-weight: bold; color: red;">Your account has been registererd with the username: '.$user.'.<br/>Please enjoy your stay at Watch The Throne');
    }
    ?>
    
    
    <!DOCTYPE html>
    <head>
        <title>GunZ Register Page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div align="center">
        <div>Watch The Throne Server:<br></div>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <table>
            <tr>
                <td>Username</td>
                <td><input type="text" name="username" maxlength="25" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password" maxlength="25" /></td>
            </tr>
    		<tr>
                <td>Password(Verification)</td>
                <td><input type="password" name="passwordVer" maxlength="25" /></td>
            </tr>
            <tr>
                <td>E-mail</td>
                <td><input type="text" name="email" maxlength="100" /></td>
            </tr>
            <tr>
                <td>Name</td>
                <td><input type="text" name="name" maxlength="50" /></td>
            </tr>
            <tr>
                <td>Age</td>
                <td><input type="text" name="age" maxlength="2" /></td>
            </tr>
            <tr>
                <td align="center" colspan="2"><input type="submit" name="register" value="Register!" /></td>
            </tr>
        </table>
        </form>
    </div>
    </body>
    </html>
    Credits:
    Myself
    Aaron - for the HTML codez.
    Last edited by ThePhailure772; 06-04-11 at 09:12 AM.


  2. #2
    Account Upgraded | Title Enabled! TheCodeOfGunz is offline
    MemberRank
    Oct 2010 Join Date
    PhilippinesLocation
    532Posts

    Re: Secure register page without anti-sql injection

    thank you very much jacob.

  3. #3
    Infraction Banned maxolahird is offline
    MemberRank
    Sep 2010 Join Date
    195Posts

    Re: Secure register page without anti-sql injection

    I got this error:

    PHP Code:
    Fatal errorUncaught exception 'PDOException' with message 'could not find driver' in C:\AppServ\www\registro.php:8 Stack trace#0 C:\AppServ\www\registro.php(8): PDO->__construct('odbc:Driver={SQ...', 'sa', '***') #1 {main} thrown in C:\AppServ\www\registro.php on line 8 

  4. #4
    Mako is insane. ThePhailure772 is offline
    MemberRank
    Sep 2007 Join Date
    1,115Posts

    Re: Secure register page without anti-sql injection

    Enable the usage of pdo_odbc.dll

  5. #5
    I am THE DON Joe9099 is offline
    MemberRank
    Jan 2007 Join Date
    England, UkLocation
    3,655Posts

    Re: Secure register page without anti-sql injection

    Quote Originally Posted by maxolahird View Post
    I got this error:

    PHP Code:
    Fatal errorUncaught exception 'PDOException' with message 'could not find driver' in C:\AppServ\www\registro.php:8 Stack trace#0 C:\AppServ\www\registro.php(8): PDO->__construct('odbc:Driver={SQ...', 'sa', '***') #1 {main} thrown in C:\AppServ\www\registro.php on line 8 
    php.ini

  6. #6
    Account Upgraded | Title Enabled! Aiona is offline
    MemberRank
    Feb 2008 Join Date
    The NetherlandsLocation
    232Posts

    Re: Secure register page without anti-sql injection

    me likey this.

    10/10 great release!

  7. #7
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: Secure register page without anti-sql injection

    Good job.

    But don't use $_SERVER['PHP_SELF'], it's XSS vulnerable.
    index.php/<script>alert("XSS")</script>
    Last edited by Dave; 06-04-11 at 04:38 PM.

  8. #8
    Mako is insane. ThePhailure772 is offline
    MemberRank
    Sep 2007 Join Date
    1,115Posts

    Re: Secure register page without anti-sql injection

    Quote Originally Posted by SuperWaffle View Post
    Good job.

    But don't use $_SERVER['PHP_SELF'], it's XSS vulnerable.
    index.php/<script>alert("XSS")</script>
    XSS doesn't matter for registration, it's practically useless there.

  9. #9
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: Secure register page without anti-sql injection

    Quote Originally Posted by Phailure772 View Post
    XSS doesn't matter for registration, it's practically useless there.
    It indeed doesn't matter. But it's still vulnerable for it.
    Best would be to use action="" since the register script is on the same page as the form.

    Just giving a tip... anyways thanks for contributing.
    Last edited by Dave; 06-04-11 at 06:09 PM.

  10. #10
    C:\ WizCoder is offline
    MemberRank
    Aug 2010 Join Date
    JapanLocation
    703Posts

    Re: Secure register page without anti-sql injection

    Cool it works.



Advertisement