-2

For some reason, my code won't run. I have no clue why. Is anything wrong with my PHP mail() function, or is there something else wrong. Essentially, I'm trying send a email verification email after the user registers. I am very new to PHP, please help!

My PHP Code:

<?php

$con=mysqli_connect('localhost','anuraag','1234','test');
if(!$con || mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
}
function enc($s) { global $con; return mysqli_real_escape_string($con, $s); }
function createUser() {
    global $con;
    $name = enc($_POST['name']);
    $email = enc($_POST['email']);
    $password = enc(sha1(trim($_POST['password'])));
    $confirmpassword = enc(sha1(trim($_POST['confirmpassword'])));

    if($password == $confirmpassword) {
        mysqli_query($con, "INSERT INTO users (name, email, password) VALUES ('" . $name . "','" . $email . "','" . $password . "');");
        echo "Your registration is completed. Please verify your email."; 

        $regid = sha1("foo" . $email . "bar"); // This is a cheap way to do it

        mail($_POST['email'], "Email verification", "Click this link to verify your email: http://localhost/register.php?email=".urlencode($_POST['email'])."&token=".urlencode($regid)."\n\nThanks.", "From: noreply@healthfullu.com");
    } else {
        echo "Passwords do not match";
    }

}

My HTML:

<html>
<head>
<title> Register!</title>
</head>
<body>
    <form action="register.php" method="POST">

            <h1>Signup!</h1>            

                <p>Create an account:</p>

                <div>
                    <label for="name">Name:</label>
                    <input type="text" id="name" name="name" value="" placeholder="First Name" class="login" />
                </div> <!-- /field -->

                <div>
                    <label for="email">Email Address:</label>
                    <input type="text" id="email" name="email" value="" placeholder="Email" class="login"/>
                </div> <!-- /field -->

                <div>
                    <label for="pass">Password:</label>
                    <input type="password" id="password" name="password" value="" placeholder="Password" class="login"/>
                </div> <!-- /field -->

                <div>
                    <label for="confirm_password">Confirm Password:</label>
                    <input type="password" id="confirmpassword" name="confirmpassword" value="" placeholder="Confirm Password" class="login"/>
                </div> <!-- /field -->

                <input type="submit" name="submit"></input>     
        </form>
    </div> <!-- /content -->

</div> <!-- /account-container -->
</body>

</html>

1 Answers1

0

You should execute your function. Add the the end of your code:

createUser();

to simply run this function and process $_POST data.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291