$dbname = "dev";
$password = "devsnake371";
$data = json_decode(file_get_contents('php://input'), true);
$userid = $data["gid"];
$newUname = $data["username"];
$err = false;
$errMsg = "";
if (empty($newUname)) {
    $errMsg = "Please enter a user name";
    $err = true;
} else {
    if (!preg_match("/^[a-zA-Z0-9]*\$/", $newUname)) {
        $errMsg = "Only letters and numbers allowed.";
        $err = true;
    } else {
        if (nameExists($newUname)) {
            $errMsg = "That username is taken, please choose another.";
            $err = true;
        } else {
            insertHash($newUname);
        }
    }
}
$resp = array('error' => $err, 'message' => $errMsg, 'username' => $newUname, 'userid' => $userid);
echo json_encode($resp);
/** checks to see if this new username is already in the database */
function nameExists($newUname)
{
    global $servername, $username, $dbname, $password, $userid;
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
Ejemplo n.º 2
0
$error = "";
?>

<div class="wrapper">
<div class="left">

<?php 
if (isset($_POST['registerFlag'])) {
    if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['confirmedPassword']) && !empty($_POST['email'])) {
        $username = strip_tags($_POST['username']);
        $email = strip_tags($_POST['email']);
        $password = $_POST['password'];
        $confirmedPW = $_POST['confirmedPassword'];
        $ip = $_POST['ip'];
        //TODO: add check to make sure username isn't taken
        $takenUsername = nameExists($username);
        if ($takenUsername) {
            $error .= "Sorry, the requested username is already taken. Please choose another. ";
        } else {
            if ($password != $confirmedPW) {
                $error .= "The entered and confirmed passwords do not match. Please re-enter the password. ";
            } else {
                //username is good, password is good
                requestRegisterAuthentication($username, $email, $password, $ip);
                echo "<p>An email has been sent to you for authentication.</p>";
            }
        }
    } else {
        $error .= "Please enter all information before registering. ";
    }
} else {