Program – Verification Account on Website by Email using PHP
Solution – Verification Account on Website by Email using PHP
SQL Code
CREATE TABLE users( email VARCHAR(255) NOT NULL, creted_on DATETIME NOT NULL, verify_string VARCHAR(16) NOT NULL, verifed TINYINT UNSIGNED );
notify-user.php
<?php $db = new PDO("mysql:host=localhost;dbname=WA", 'root', ''); $email = 'demo@example.com'; $verify_string = ''; for ($i = 0; $i <16 ; $i++) { $verify_string .=chr(mt_rand(32,126)); } $sth = $db->prepare("INSERT INTO users (email, created_on, verify_string, verifed) VALUES (?, datetime('now'), ?, 0)"); $sth->execute(array($email, $verify_string)); $safe_email = urlencode($email); $verify_url = "http://www.example.com/verify-user.php"; $mail_body = <<<_MAIL_ To $email Please Click on the following link to verify your account creation: $verify_url?email=$safe_email&verify_string=$verify_string If you Do not verify your account in 7 days It will deleted. _MAIL_; mail($email, "Verifcation Code", $mail_body); ?>
verify-user.php
<?php $db = new PDO("mysql:host=localhost;dbname=WA", 'root', ''); $sth = $db->prepare('UPDATE users SET verifed = 1 WHERE email = ? AND verfiy_string=? AND verifed = 0'); $res = $sth->execute(array($_GET['email'], $_GET['verify_string'])); var_dump($res, $sth->rowCount()); if (!$res) { print "Please Try Again Later Due to a DB error"; }else{ if ($db->rowCount()==1) { print "Thank You, Your account is Verifed Now"; }else{ print "Sorry, You could not be verifed"; } } ?>
delete-user.php
<?php $db = new PDO("mysql:host=localhost;dbname=WA", 'root', ''); $time = '-7 days'; $sth = $db->prepare("DELETE FROM Users WHERE verfied = 0 AND created_on < datetime('now',?)"); $res=$sth->execute(array($time)); if ($res) { print "Deactivated ". $sth->rowCount()." users.\n"; }else{ print "Cant delete users.\n"; } ?>
इसे भी पढ़े – Protect a Website Part Using PHP?