1$email = $_POST["email"];
2if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
3 $emailErr = "Invalid email format";
4}
1// E-mail Code Validation using a four digit number:
2// var.inc.php:
3<?php
4session_start();
5$x = mt_rand(1000,9999);
6// index.php:
7<?php
8include_once 'var.inc.php';
9$_SESSION['key'] = $x;
10if(isset($_POST['submit'])){
11 if(!mail($_POST['email-in'], "Verify", "Code: ". $x)){
12 echo "ERROR EMAIL";
13 }else{
14 header("Location: validate.php");
15 }
16}
17?>
18<!DOCTYPE html>
19<html lang="en">
20<head>
21 <meta charset="UTF-8">
22 <meta http-equiv="X-UA-Compatible" content="IE=edge">
23 <meta name="viewport" content="width=device-width, initial-scale=1.0">
24 <title>Validate email</title>
25</head>
26<body>
27 <form action="" method="post">
28 <input type="email" placeholder="email" name="email-in">
29 <button id="submit" type="submit" name="submit">Submit</button>
30 </form>
31</body>
32</html>
33// validate.php:
34 <?php session_start();?>
35<!DOCTYPE html>
36<html lang="en">
37<head>
38 <meta charset="UTF-8">
39 <meta http-equiv="X-UA-Compatible" content="IE=edge">
40 <meta name="viewport" content="width=device-width, initial-scale=1.0">
41 <title>Validate</title>
42</head>
43<body>
44 <form action="" method="post">
45 <input type="number" name="user-key">
46 <button type="submit" name="submit-user-key">Validate</button>
47 </form>
48</body>
49</html>
50<?php
51 if(isset($_POST['submit-user-key'])){
52 if($_POST['user-key'] == $_SESSION['key']){
53 //Do something
54 }
55 }
56?>
1//BE CAREFUL!!!
2//NEVER VALIDATE A EMAIL WITH A NORMAL PREG MATCH
3//I SAW THIS A LOT VALIDATING A EMAIL IS THE BEST TO SEND A EMAIL
4//AND VERIFY
5//BECAUSE SOMEONE MIGHT HAVE A RIGHT EMAIL THAT DOESNT WORK BECAUSE OF
6//PREG MATCH