Skip to content

Instantly share code, notes, and snippets.

@kcuzner
Last active May 22, 2019 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kcuzner/11323907 to your computer and use it in GitHub Desktop.
Save kcuzner/11323907 to your computer and use it in GitHub Desktop.
Registration page example
<?php
$emailvalid = null; //your email error
$passlength = null; //your password length error
$passmatch = null; //your password match error
$overallerror = null; //overall error ("Verification" below)
if ($_GET['submit'] == "true") {
try {
$handler = new PDO('mysql:host=localhost;dbname=s','root', '*');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
exit($e->getMessage());
}
// Post
$name = $_POST['name'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$password1 = $_POST['passwordconf'];
$ip = $_SERVER['REMOTE_ADDR'];
// Verifcation
if (empty($name) || empty($username) || empty($email) || empty($password) || empty($password1)) {
$overallError = "Complete all fields";
}
// Password match
if ($password != $password1) {
$passmatch = "Passwords don't match"; //Notice that there is no echo
}
// Email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailvalid = "Enter a valid email";
}
// Password length
if (strlen($password) <= 6){
$passlength = "Choose a password longer then 6 character";
}
if(empty($overallError) && empty($passmatch) && empty($emailvalid) && empty($passlength)) {
//Securly insert into database
$sql = 'INSERT INTO userinfo (name ,username, email, password, ip) VALUES (:name,:username,:email,:password,:ip)';
$query = $handler->prepare($sql);
$query->execute(array(
':name' => $name,
':username' => $username,
':email' => $email,
':password' => $password,
':ip' => $ip
));
header("Location: http://www.google.com/?q=success"); //your success page redirect here...
die(); //there is no need to continue after issuing a redirect
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<?php if ($overallError): ?>
<div class="alert alert-danger"><?php echo $overallError; ?></div>
<?php endif; ?>
<form class="form-signin" role="form" action="register.php?submit=true" method="post">
<h2 class="form-signin-heading">Please sign up</h2>
<div class="form-group">
<input type="text" class="form-control" placeholder="Name" name="name" autofocus style="border-color:#<?php ?>;">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Username" name="username" autofocus>
</div>
<div class="form-group <?php if (!empty($emailvalid)) { echo 'has-error'; } ?>">
<input type="text" class="form-control" placeholder="Email" name="email" autofocus>
<span class="help-block"><?php echo $emailvalid; ?></span>
</div>
<div class="form-group <?php if (!empty($passmatch) || !empty($passlength)) { echo 'has-error'; } ?>">
<input type="password" class="form-control" placeholder="Password" name="password">
<span class="help-block"><?php echo $passlength; ?></span>
</div>
<div class="form-group <?php if (!empty($passmatch)) { echo 'has-error'; } ?>">
<input type="password" class="form-control" placeholder="Password, Again" name="passwordconf" >
<span class="help-block"><?php echo $passmatch; ?></span>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment