znaji Posted November 1, 2013 Share Posted November 1, 2013 Hi I just started to learn how to code and love learning new things. I started with reading codes and just playing around and now I'm trying to fix finish this code. My goal is for this to handle errors (username, email) prevent duplicate entries in MySql and return error (invaild email, username already exist etc) via ajax. I learn faster and better by example if anyone can help and show me a revised code i would greatly appricate it. <?php if(isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['email']) && !empty($_POST['email'])){ $username = mysql_escape_string($_POST['username']); $email = mysql_escape_string($_POST['email']); if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)){ // Return Error - Invalid Email $msg = 'The email you have entered is invalid, please try again.'; }else{ // Return Success - Valid Email $msg = 'Your account has been made, <br /> please verify it by clicking the activation link that has been send to your email.'; $hash = md5( rand(0,1000) ); // Generate random 32 character hash and assign it to a local variable. $password = substr(hash('sha512',rand()),0,50); mysql_query("INSERT IGNORE INTO users (username, password, email, hash, ip, whenadded) VALUES( '". mysql_escape_string($username) ."', '". mysql_escape_string(md5($password)) ."', '". mysql_escape_string($email) ."', '". mysql_escape_string($hash) ."', '". $_SERVER['REMOTE_ADDR'] ."', now()) ") or die(mysql_error()); $to = $email; //Send email to user $subject = 'Signup | Verification'; //// Give the email a subject $message = ' Thanks for signing up! Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below. ------------------------ Username: '.$username.' Password: '.$password.' ------------------------ Please click this link to activate your account: http://www.example.com/verify.php?email='.$email.'&hash='.$hash.' '; // Our message above including the link $headers = 'From:noreply@yourwebsite.com' . "\r\n"; // Set from headers mail($to, $subject, $message, $headers); // Send the email } } ?> <?php if(isset($msg)){ // Check if $msg is not empty echo '<div class="statusmsg">'.$msg.'</div>'; } ?> <form id="form" class="form" action="" method="post"> <ul> <li> <label for="username">Name</label> <input class="text" type="text" placeholder="Username" size="50" id="username" name="username" tabindex="1" /> </li> <li> <label for="email">Email</label> <input class="text" type="text" placeholder="Email address" size="50" id="email" name="email" tabindex="2" /> </li> </ul> <input type="submit" class="submit_button" value="Sign up" /> </form> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.