Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Test Register Page</title>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="css/register.css" />
- <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
- <link rel="stylesheet" href="css/index.css" />
- <link rel="stylesheet" href="css/styles.css" />
- <link href='https://fonts.googleapis.com/css?family=Raleway:400,500,300,600' rel='stylesheet' type='text/css'>
- </head>
- <?php
- //This line creates the function test_input using the parameter $data
- function test_input($data) {
- //This line removes all whitespace from the data parsed in
- $data = trim($data);
- //This line removes all backslashes from the data
- $data = stripslashes($data);
- //This line replaces all special characters with HTML escaped code to prevent cross-site scripting
- $data = htmlspecialchars($data);
- //This line returns the resulting data to the above code
- return $data;
- }
- include_once "navBar.php";
- //These lines create all of the variables and sets them to empty values
- $emailErr = $firstNErr = $surnErr = $phoneErr = $passErr = $confPassErr = "";
- $email = $firstN = $surn = $phone = $pass = "";
- $error = 0;
- //These lines ceate and assign the variables required to connect to the SQL database
- $servername = "127.0.0.1";
- $username = "root";
- $password = "";
- $dbname = "accounts";
- //These lines try to connect to the database with the assigned variables above
- $conn = new mysqli($servername, $username, $password, $dbname);
- //This if statement runs the indented code below when the user clicks the register button
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- //If the connection to the database fails, an error message is displayed to the user
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error . "/nPlease try again. If it still doesn't work, copy the error message and send it to cjbrennan2701@gmail.com");
- }
- //This line checks whether the user has entered anything in the email box
- if (empty($_POST["email"])) {
- //If they haven't, a suitable error message is displayed to the user
- //Also, the value of error becomes 1
- $emailErr = "E-mail required";
- $error = 1;
- } else if (strpos($_POST["email"],'@') === FALSE && $error != 1){
- $emailErr = "Please enter a valid email address";
- } else {
- //0therwise, the user's input is stripped of any whitespace or backslashes
- //Any special characters in the input are converted to HTML escaped code to prevent malicious attacks
- $email = test_input($_POST["email"]);
- }
- //The first name box is checked to see whether it is empty
- if (empty($_POST["firstN"])) {
- //If it is, the user is told to enter a first name and the value of error becomes 1
- $firstNErr = "First name required";
- $error = 1;
- } else {
- //If not, the function test_input removes whitespace and backslashes from the imput
- //and special characters are replaced by HTML escaped code
- $firstN = test_input($_POST["firstN"]);
- }
- //The surname box is checkked to see whether it is empty
- if (empty($_POST["surn"])) {
- //If it is empty the user is informed and the value of error is changed to 1
- $surnErr = "Surname required";
- $error = 1;
- } else {
- //If it isn't empty, test_input removes backslashes and whitespace
- //as well as replaces any special characters with HTML escaped code
- $surn = test_input($_POST["surn"]);
- }
- //The user's input for their phone is then checked
- if (empty($_POST["phone"])) {
- //If it is empty, then nothing is stored
- $phone = "";
- } elseif(strlen($_POST["phone"] === 11) && is_numeric($_POST["phone"])){
- //If the user's input only contains numbers and is 11 characters long
- //it is stored as $phone
- $phone = test_input($_POST["phone"]);
- //If any other format of input is detected, an error is produced
- } else {
- $phoneErr = "Please enter a valid phone number";
- }
- //The user's input for their password is then checked
- //If the box is empty, an error message is produced and error becomes 1
- if (empty($_POST["password"])) {
- $passErr = "Password required";
- $error = 1;
- //0therwise, the user's password has whitespace and backslashes removed and is then stored
- } else {
- $pass = test_input($_POST["password"]);
- }
- //Finally, the user's input for confirming their password is checked
- //if it is empty, a suitable error message is produced and error is changed to 1
- if (empty($_POST["confPass"])) {
- $confPassErr = "Password confirmation required";
- $error = 1;
- }
- //A1so, if the user's input for their password and password confirmation don't match,
- //a message on the page tells the user and error is changed to 1
- elseif ($_POST["confPass"] != $_POST["password"] && $passErr === "") {
- $confPassErr = "Passwords don't match";
- $error = 1;
- }
- //The variable $sql is created which is used to store the query that will be sent to the database
- $sql = "INSERT INTO testTable (email, firstN, surn, phone, psswrd)
- VALUES ('$email', '$firstN', '$surn', '$phone', '$pass')";
- //If no errors were found in the user's inputs, then the data is sent to the database to be stored
- if ($error === 0){
- if ($conn->query($sql) === TRUE) {
- //If the connection to the database fails, an error saying what went wrong is produced
- } else {
- echo "Error: " . $sql . "
- <br>" . $conn->error;
- }
- }
- //This line ends the connection to the database
- $conn->close();
- }
- ?>
- <body>
- <div class="container-fluid">
- <div class="row">
- <div class="col-md-3"></div>
- <div class="col-xs-12 col-md-6">
- <!-- This line creates a form where when the user clicks submit, runs the PHP code above -->
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- <h5 style="color:#136002; font-size:16px;">Please fill out the information below<br>
- Fields marked by an asterisk * are required</h5>
- <div class="row">
- <div id="text" class="col-xs-6">
- <!-- These lines provide an input box for the user to type their email address as well as a space to display any errors -->
- <p>E-mail: </p>
- <br>
- <p>First name: </p>
- <br>
- <p>Surname: </p>
- <br>
- <p>Phone number: </p>
- <br>
- <p>Password: </p>
- <br>
- <p>Confirm password: </p>
- </div>
- <div id="input" class="col-xs-6">
- <p>
- <input type="text" name="email" id="email">
- <span class="error">* <?php echo $emailErr;?></span></p>
- <p>
- <input type="text" name="firstN" id="firstN">
- <span class="error">* <?php echo $firstNErr;?></span></p>
- <p>
- <input type="text" name="surn" id="surn">
- <span class="error">* <?php echo $surnErr;?></span></p>
- <p>
- <input type="text" name="phone" id="phone">
- <span class="error"> <?php echo $phoneErr;?></span></p>
- <p>
- <input type="text" name="password" id="password">
- <span class="error">* <?php echo $passErr;?></span></p>
- <p style="margin-bottom:6px;">
- <input type="text" name="confPass">
- <span class="error">* <?php echo $confPassErr;?></span></p>
- </div>
- </div>
- <div class="row">
- <div id="btnReg" class="col-sm-12">
- <button type="submit" name="submit">Register</button>
- </div>
- </div>
- </form>
- </div>
- <div class="col-sm-2 col-md-3"></div>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement