Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.7 kB
1
Indexable
Never
<?php
if (isset($_POST["username"]) && isset($_POST["email"]) && isset($_POST["name"]) && isset($_POST["pass"])) {
    $username = $_POST["username"];
    $email = $_POST["email"];
    $name = $_POST["name"];
    $pass = $_POST["pass"];

    if (preg_match("/^[a-zA-Z][a-zA-Z0-9_]{2,31}$/", $username) && preg_match("/^[a-z]([a-z]|\s){2,31}$/", $name) && preg_match("/.{4,32}/", $pass)) {

        $file = file_get_contents("users.json");
        $users = json_decode($file, true);

        for ($i = 0; $i < count($users); $i++) {
            if ($users[$i]["username"] == $username) {
                die('<a href="../register.php">Username is exist!</a>');
            }
            if ($users[$i]['email'] == $email) {
                die('<a href="../register.php">email is exist!</a>');
            }
        }


        $lastId = end($users)["id"];
        $id = $lastId + 1;
        function checkId()
        {
            global $users;
            global $id;

            for ($i = 0; $i < count($users); $i++) {
                if ($users[$i]["id"] == $id) {
                    $id++;
                    checkId();
                }
            }
        }
        checkId();

        $array = ["id" => $id, "username" => $username, "email" => $email, "name" => $name, "password" => $pass];
        array_push($users, $array);
        $encoded = json_encode($users);
        file_put_contents("users.json", $encoded);

        $_SESSION['log'] = true;
        header("location:../destination");
    } else {
        die('<a href="../register.php">Enter valid values.</a>');
    }
} else {
    die('<a href="../register.php">Enter all inputs!</a>');
}