Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* In response to a Yahoo Answer's Question */
- /* Check if Page has been POSTed to */
- if( $_SERVER['REQUEST_METHOD'] !== 'POST'){
- echo '<h1>Invalid Permission</h1><p>Please Fill out the form first.</p>';
- exit; // Exit the page.
- }
- /* Functions to help with checks */
- function minLength($str, $len){
- return (strlen($str) >= $len);
- }
- function maxLength($str, $len){
- return (strlen($str) <= $len);
- }
- function pvar($name, $mysqlEscape=false){
- // mysql_real_escape_string() helps prevent MySQL injection attacks. Not perfect, but better than nothing.
- $v = (isset($_POST[$name]))? $_POST[$name] : '';
- return ($mysqlEscape)? mysql_real_escape_string(trim($v)) : trim($v);
- }
- function hasValue(){
- $args = func_get_args();
- foreach($args as $arg){
- if(!isset($args{0})){
- return false;
- }
- }
- return true;
- }
- /* Connect To the database */
- $connect = mysql_connect("localhost","root","") or die('Internal Error. Couldn\'t connect to database');
- mysql_select_db("thorbis", $connect) or die('Internal Error. Could\'t select database'); // Discriptive Error Messages allow you to easily pinpoint causes
- /* Variable Definitions with the help of pvar() function */
- $fullname = strip_tags(pvar('fullname', true));
- $username = strtolower(strip_tags(pvar('username', true))); // Helps to prevent duplicate usernames
- $password = strip_tags(pvar('password', true));
- $repeatpassword = strip_tags(pvar('repeatpassword', true));
- $email = strip_tags(pvar('email', true));
- $firstname = pvar('firstname', true);
- $lastname = pvar('lastname', true);
- $phone = pvar('phone', true);
- $address1 = pvar('address1', true);
- $address2 = pvar('address2', true);
- $country = pvar('counry', true);
- $state = pvar('state', true);
- $city = pvar('city', true);
- $zip = pvar('zip', true);
- $date = date("Y-m-d");
- $errorMsg = ''; // Will hold any error messages
- /* Check for Input Values */
- if (!hasValue($fullname, $username, $password, $repeatpassword, $email, $firstname, $lastname, $phone, $address1, $address2, $country, $state, $city, $zip)){
- $errorMsg .= 'All Fields are Required.<br>';
- }
- /* Check for Existing Username */
- $namecheck = mysql_query("SELECT username FROM users WHERE username='$username' LIMIT 1") or die(mysql_error($connect));
- if( mysql_num_rows($namecheck) > 0){
- $errorMsg .= "Username is already Registered! Please select another.<br>";
- }
- /* Check Passwords Match and Length */
- if( !minLength($password, 6)){ // A maximum length is not needed, as you are hashing the password, which will condense it to 32 characters in length.
- $errorMsg .= 'Password needs to be a minimum of 6 characters in length.<br>';
- if ($password !== $repeatpassword){
- $errorMsg .= 'Password and Confirmation Password DO NOT MATCH.<br>';
- }else{
- $password = md5($password); // Although I would suggest using some salt or adding another hash function to the process ex: $password = sha1('#$1a1' . md5($password) . '@1^'); Remember that to compare this password for login, you need to apply the same steps and salt to the user input. A modifyPassword($password) function is the best way to go about this.
- }
- /* Username Length Checks and Fullname length checks */
- if( !maxLength($username, 25) || !maxLength($fullname, 25)){
- $errorMsg .= "Length of username or fullname is too long!<br>";
- }
- /* Output Error Message, if there is one. Otherwise, register User. */
- if( strlen($errorMsg) > 0){
- echo $errorMsg;
- }else{
- // Yahoo cut off your query statement, so I couldn't write it properly for you, but you only name the column to which you will be adding data to. You cannot name null columns
- $result = mysql_query( "INSERT INTO users(fullname, username, password,...) VALUES('{$fullname}','{$username}','{$password}',...)");
- if($result){
- echo "You have been registered! <a href='index.php'>click here</a> to go login";
- }else{
- echo 'There has been an Internal Error. Please Try Again Later. '; // There was a problem with your query statement. Uncomment the next line to help determine the issue
- /* echo '<br>' . mysql_error($connect); */
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment