✅ WEB ja WordPressi uudised, teemad, pistikprogrammid. Siin jagame näpunäiteid ja parimaid veebisaidi lahendusi.

Kuidas luua oma veebisaidile PHP registreerimissüsteemi

25

Kas soovite integreerida PHP registreerimissüsteemi? Selles postituses loome registreerimisvormi, kinnitame e-posti andmebaasi vastu. Kasutaja eduka registreerimise korral saadame kasutaja e-posti aadressile aktiveerimislingi. Kui kasutaja klõpsab e-kirjas aktiveerimislingi, aktiveerime tema konto.

Olles seda öelnud, loome oma PHP registreerimissüsteemi.

Looge andmebaasis kasutajate tabel

Iga registreerimissüsteemi jaoks vajame andmebaasitabelit. Selle tabeli kirjete põhjal kontrollime, kas kasutaja sisestab unikaalse e-posti aadressi või mitte. Kui kasutajaandmed on kordumatud ja õiged, saavad meie süsteemi registreeruda ainult nemad.

Käivitage allpool olev päring oma andmebaasi, et luua tabel nimega "kasutajad".

CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT,
 `fullname` varchar(255) NOT NULL,
 `email` varchar(255) NOT NULL,
 `password` varchar(255) NOT NULL,
 `activation_key` varchar(255) NOT NULL,
 `status` enum('0','1') NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`)) ENGINE=MyISAM;

Looge registreerumisvorm

Kuidas luua oma veebisaidile PHP registreerimissüsteemi

Registreerimisvormi kujundamiseks kasutame Bootstrap Framework’i. Looge fail „index.php" ja pange sellesse allolev kood.

<!DOCTYPE html>
<html>
    <head>
        <title>Registration Form</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <h3>Registration Form</h3>
                    <?php if(!empty($arr_message['msg'])) { ?>
                        <div class="alert <?php echo $arr_message['class']; ?>"><?php echo $arr_message['msg']; ?></div>
                    <?php } ?>
                    <form method="post">
                        <div class="form-group">
                            <label for="exampleInputFullname">Full Name</label>
                            <input type="text" class="form-control" id="exampleInputFullname" name="fullname" placeholder="Full Name" value="<?php if(isset($fullname)) echo $fullname; ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="exampleInputEmail1">Email address</label>
                            <input type="email" class="form-control" id="exampleInputEmail1" name="email" placeholder="Email" value="<?php if(isset($email)) echo $email; ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="exampleInputPassword1">Password</label>
                            <input type="password" class="form-control" id="exampleInputPassword1" name="password" placeholder="Password" required>
                        </div>
                        <div class="form-group">
                            <label for="exampleInputPassword2">Confirm Password</label>
                            <input type="password" class="form-control" id="exampleInputPassword2" name="cpassword" placeholder="Confirm Password" required>
                        </div>
                        <button type="submit" name="submit" class="btn btn-default">Submit</button>
                    </form>
                </div>
            </div>
        </div>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </body>
</html>

Koodi kohal kuvatakse registreerimisvorm, kui käivitame faili ‘index.php’. Jälgite koodis muutujat $ arr_message [‘msg’], mis prindib registreerumisvea, kui seda on. See muutuja deklareeritakse lähiajal.

Serveripoolne kood PHP registreerimissüsteemi jaoks

Selles etapis oleme registreerumisvormi ja andmebaasitabeliga valmis. Järgmisena peame vormile esitama kirjutama PHP-koodi, mis kontrollib, kas e-post on andmebaasis juba olemas. Kui meil on olemas, viskame veateate. Kui kõik üksikasjad on õiged, sisestame kasutajaandmed andmebaasi ja saadame nende e-posti aadressile aktiveerimislingi.

Looge fail ‘config.php’, kuhu salvestame andmebaasi mandaadid.

<?php
$conn = new mysqli('DB_HOST', 'DB_USER', 'DB_PASSWORD', 'DB_NAME');
 
if ($conn->connect_errno) {
    echo "Error: ". $conn->connect_error;
}
?>

Enne meie HTML-märgendi algust ‘index.php’ lisage allolev kood.

<?php
require_once('config.php');
 
$arr_message = [];
if (isset($_POST['submit'])) {
 
    $_POST = array_map('trim', $_POST);
    extract($_POST);
 
    if (!empty($fullname) && !empty($email) && !empty($password)) {
         
        if ($password == $cpassword) {
            $sql = "SELECT id FROM users WHERE email = '".$conn->real_escape_string($email)."'";
            $result = $conn->query($sql);
 
            if ($result->num_rows > 0) {
                $arr_message = [
                    'class' => 'alert-danger',
                    'msg' => 'Email already exist.',
                ];
            } else {
                $email = $conn->real_escape_string($email);
                $activation_key = sha1(mt_rand(10000,99999).time().$email);
 
                $sql = "INSERT INTO users(fullname, email, password, activation_key) VALUES('".$conn->real_escape_string($fullname)."', '".$email."', '".md5($conn->real_escape_string($password))."', '".$activation_key."')";
                $conn->query($sql);
 
                //send activation link in an email
                $subject = 'Activate Your Account';
                $message = 'Hello '.ucwords($fullname).',<br> 
                            <p>Click the below link to activate your account.</p>
                            <a href="YOUR_PROJECT_URL/activate.php?key='.$activation_key.'">Activate Account</a><br><br>
                            Thanks,<br>Admin';
 
                $headers[] = 'MIME-Version: 1.0';
                $headers[] = 'Content-type: text/html; charset=iso-8859-1';
                mail($email, $subject, $message, implode("rn", $headers));
 
                $arr_message = [
                    'class' => 'alert-success',
                    'msg' => 'We have sent an activation link to your email. Please activate your account.',
                ];
                $fullname = $email = '';
            }
        } else {
            $arr_message = [
                'class' => 'alert-danger',
                'msg' => 'Password mismatch.',
            ];
        }
    }
}
?>

Siin kasutame parooli salvestamise ajal PHP-meetodit md5, eeldades, et salvestasite parooli selle algoritmi abil.

activ.php fail konto aktiveerimiseks

Nüüd on aeg luua fail „activ.php”. See fail haldab konto aktiveerimiskoodi. Kirjutame koodi kasutajakonto aktiveerimiseks, kui URL sisaldab õiget aktiveerimisvõtit.

‘activ.php’ fail sisaldab allolevat koodi.

<?php
require_once('config.php');
 
if (!isset($_GET['key']) || empty($_GET['key'])) {
    header('Location:index.php');
}
 
$sql = "SELECT id, status FROM users WHERE activation_key = '".$_GET['key']."'";
$result = $conn->query($sql);
 
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    if ($row['status']) {
        $arr_message = [
            'class' => 'alert-success',
            'msg' => 'Your account is already activated.',
        ];
    } else {
        $sql = "UPDATE users SET status = '1' WHERE id = ".$row['id']." AND activation_key = '".$_GET['key']."'";
        $conn->query($sql);
 
        $arr_message = [
            'class' => 'alert-success',
            'msg' => 'Your account is activated. You can <a href="YOUR_PROJECT_URL/login.php">login now</a>.',
        ];
    }
} else {
    $arr_message = [
        'class' => 'alert-danger',
        'msg' => 'Invalid URL.',
    ];
}
?>

Edu / veateate kuvamiseks failis ‘activ.php’ lisage sellesse allpool olev HTML-kood.

<div class="container">
        <div class="row">
            <div class="col-md-6">
                <?php if(!empty($arr_message['msg'])) { ?>
                    <div class="alert <?php echo $arr_message['class']; ?>">
                        <?php echo $arr_message['msg']; ?>
                    </div>
                <?php } ?>
            </div>
        </div>
    </div>

Samuti võiksite lugeda meie seotud artiklit PHP sisselogimissüsteemi loomise kohta.

See veebisait kasutab teie kasutuskogemuse parandamiseks küpsiseid. Eeldame, et olete sellega rahul, kuid saate soovi korral loobuda. Nõustu Loe rohkem