Skip to content

Instantly share code, notes, and snippets.

@NitishDiwakar
Created June 25, 2017 12:44
Show Gist options
  • Save NitishDiwakar/31450d4ae3ca054f6dda9114a4cf15dd to your computer and use it in GitHub Desktop.
Save NitishDiwakar/31450d4ae3ca054f6dda9114a4cf15dd to your computer and use it in GitHub Desktop.
Register and login with php & mysqli using oop
<?php
include 'include/connection.php';
$conn = db();
if(isset($_POST["username"]) && strlen($_POST["username"]) >= 3 && !empty($_POST["username"]) )
{
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
$username = filter_var($_POST["username"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
$sql = "SELECT count(*) FROM users WHERE uname = '". $username ."' ";
$result = $conn->query($sql);
$row = $result->fetch_array();
$user_count = $row[0];
if($user_count>0) {
echo '<span style="color: red"><i class="glyphicon glyphicon-remove"></i> not available</span>';
exit();
}
else {
echo '<span style="color: green"><i class="glyphicon glyphicon-ok"></i> available</span>';
exit();
}
}
if(isset($_POST["username"]) && strlen($_POST["username"]) < 3 && !empty($_POST["username"]) )
{
echo '<span style="color: red"><i class="glyphicon glyphicon-remove"></i> username too short!</span>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment