Membuat Fuction Registration di PHP

Jadi validasi formnya gak kebaca dan data yg diinputkan gak ke-save di databasenya. Setiap kali tes validasi form, tiba - tiba formnya itu hilang semua alias halamannya kosong. Dan setiap kali me-inputkan data ke formnya itu hasil error di bagian file register.php. Seharusnya ada sebuah notifikasi dan form registernya...

controller/register.php
<?php
//error_reporting(0);
include_once "../lib/connected.php";
include_once "../controller/activityLog.php";
include_once "../lib/generate_randomStr.php";

session_start();

// koneksi ke database
$dbconn = connected();

// deklarasi variabel
$username = " ";
$name     = " ";
$email    = " ";
$address  = " ";
$token    = " ";
$errors   = array();

if (isset($_POST['register_btn']  && $_POST['token'] == $_SESSION['csrf_token'])) {
    $token = generate_randomStr(16);
    register();
    csrf_token();
    email_queue();
}


function register(){
    global $dbconn, $errors, $username, $name, $email, $address, $token;

    $username        = e($_POST['username']);
    $name            = e($_POST['name']);
    $email           = e($_POST['email']);
    $password        = e(sha1($_POST['password']));
    $repeat_password = e(sha1($_POST['repeat_password']));
    $address         = e($_POST['address']);
    $token           = e($_POST['token']);

    // validasi form
    $usernameValidation = "/^[a-zA-Z][a-zA-Z0-9_]*$/";

    if((strlen($username) > 64)){
        array_push($errors, "username has exceeded 64 characters");
        header("Location: ../register/index.php");
        activityLog($username, "Register", "Someone failed to register because the inputting username exceeds 64 characters long");
    }else if(preg_match($usernameValidation, $username)){
        array_push($errors, "Username cannot begin with a number!");
        header("Location: ../register/index.php");
        activityLog($username, "Register", "GET", "Someone failed to register because the username entered cannot be preceded by numbers");
    }else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        array_push($errors, "Invalid email format");
        header("Location: ../register/index.php");
        activityLog($username, "Register", "POST", "Someone failed to register because invalid email format");
    }else if{
        // now check if the mail is already registered
        $query = pg_query($dbconn, "SELECT email FROM basicapp.bas_user WHERE email = '$email'");
        if(pg_fetch_array($query)[0] == $email) {
            array_push($errors, "This email already exists.");
            header("Location: ../register/index.php");
            activityLog($username, "Register", "GET", "Someone failed to register because the email already exists");
        }
    }else if($password != $repeat_password){
        array_push($errors, "The two pindex.phpassword do not match!");
        header("Location: ../register/");
        activityLog($username, "Register", "GET", "Someone failed to register because the name field is empty");
    }else if(count($errors) == 0){
        //enkripsi password sebelum disimpan didalam database
        $password = sha1($password);

        if(isset($_POST['register_btn'])){
            $register_btn = e($_POST['register_btn']);
            $query = "INSERT INTO basicapp.bas_user (username, name, email, password, address, is_active, activation_code)
                      VALUES('$username', '$name', '$email', '$password', '$address', '0', '$token')";
            $save_query = pg_query($dbconn, $query);

            $result = pg_affected_rows($save_query);
            // get id
            $logged_in_user_id = pg_last_oid($dbconn);
            if($result){
                $_SESSION['user'] = getUserById($logged_in_user_id);
                $_SESSION['success']  = "You are now logged in!";
                header('Location: ../login/index.php');
                activityLog($username, "Register", "GET", "Someone successfully to register with data fullname = $name, username = $username, email = $email, address = $address");
            }
            else{
                header("Location: ../register/index.php");
            }

        }
    }

}
//register();
var_dump(register());
var_dump(pg_last_error(register));

function getUserById($id){
    global $dbconn;
    $query = "SELECT * FROM basicapp.bas_user WHERE id = $id";
    $result = pg_query($dbconn, $query);

    $user = pg_fetch_assoc($result);
    return $user;
}
// escape string
function e($val){
    global $dbconn;
    return pg_escape_string($dbconn, trim($val));
}


register/index.php
<?php
include_once "../template/header.php";
?>
<div class="container">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <div class="card shadow">
                <div class="card-header">
                    <h2>Register</h2>
                </div>
                <div class="card-body">
                    <form class="row g-3"  action="<?= include_once "../controller/register.php"; ?>" method="POST">
                        <?php if(count($errors) > 0){ ?>
                            <div class="alert alert-danger" role="alert">
                                <?php
                                    foreach($errors as $error){
                                        echo $error . "<br>";
                                    }
                                ?>

                            </div>
                        <?php } ?>

                        <div class="col-md-12">
                            <label for="name" class="form-label">Fullname<span style="color: red;">*</span></label>
                            <input type="text" name="name" class="form-control" value="<?php echo $name; ?>" for="name" placeholder="enter fullname" required />
                        </div>
                        <div class="col-md-6">
                            <label for="name" class="form-label">Username<span style="color: red;">*</span></label>
                            <input type="text" name="username" class="form-control" value="<?php echo $username; ?>" for="username" placeholder="enter username" required />
                        </div>
                        <div class="col-md-6">
                            <label for="email" class="form-label">Email<span style="color: red;">*</span></label>
                            <input type="email" name="email" class="form-control" value="<?php echo $email; ?>" for="email" placeholder="enter email" required />
                            <input type="hidden" class="form-control" name="token" id="token" value="<?php echo $token; ?>">
                        </div>
                        <div class="col-md-6">
                            <label for="password" class="form-label">Password<span style="color: red;">*</span></label>
                            <input type="password" name="password" class="form-control" for="password" placeholder="enter new password" required />
                        </div>
                        <div class="col-md-6">
                            <label for="repeat_password" class="form-label">Repeat password<span style="color: red;">*</span></label>
                            <input type="password" name="repeat_password" class="form-control" placeholder="repeat new password" required />
                        </div>
                        <div class="col-md-12">
                            <label for="address" class="form-label">Address<span style="color: red;">*</span></label>
                            <input type="address" name="address" class="form-control" value="<?php echo $address; ?>" for="address" placeholder="enter address" required />
                        </div>
                        <div class="col-md-12 d-grid gap-2 mx-auto">
                            <input type="submit" class="btn text-white" name="register_btn" value="Register" />
                        </div>

                        <p class="text-muted">
                            Already have an account? <a href="../login/">Login</a>
                        </p>
                    </form>
                </div>
            </div>

        </div>
    </div>
</div>

<?php
include_once "../template/footer.php";
?>


Apakah ada yang salah dalam penulisan syntax saya? 
Mohon bantuannya..
Terima kasih ...
avatar sweetcorns12

@sweetcorns12

4 Kontribusi 1 Poin

  • errornya apa/? tampilkan di pertanyaan kamu - @hilmanski
  • Oh maaf jadi validasi formnya gak kebaca. Setiap kali tes validasi form, tiba - tiba formnya itu hilang semua alias halamannya kosong. Seharusnya ada sebuah notifikasi dan form registernya. - @sweetcorns12
  • dituliskan di atas ya, edit pertznyannya, dipermudah buat orang yang baca, - @hilmanski


Jawaban

Aktifkan pesan error di PHP,
agar kamu bisa debug pelan pelan, mencari masalahnya di mana.

Masalah yang kamu sebutkan terlalu umum, banyak hal yang bisa jadi sumber kesalahannya
avatar hilmanski

@hilmanski

2315 Kontribusi 1939 Poin


Halo, saya bantu jawab ya, 
Coba kamu ubah if-nya baris ini :
 if (isset($_POST['register_btn']  && $_POST['token'] == $_SESSION['csrf_token'])) {
    $token = generate_randomStr(16);
    register();
    csrf_token();
    email_queue();
}
diubah menjadi :

if (isset($_POST['register_btn'])  && ($_POST['token'] == $_SESSION['csrf_token'])) {
    $token = generate_randomStr(16);
    register();
    csrf_token();
    email_queue();
} 

Semoga berhasil.
avatar ahanafi

@ahanafi

666 Kontribusi 410 Poin


Login untuk gabung berdiskusi