/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php include("functions.php"); interface post_data { // Create interface that the main class will rely on function login_get_data(); function login_clean_data(); function login_check_data(); function register_get_clean_data(); function register_add_data(); function logout(); } abstract class sql_server { // Class that handles the SQL connection public $cnx; public function __construct() { } } class post_data_class extends sql_server implements post_data { // Creating the main variables and setting them to private for security "Password" => NULL); "Country" => NULL, "Phone Number" => NULL, "Address" => NULL, "Email" => NULL, "User_Name" => NULL, "Pass_Word" => NULL); // Getting the POST data automatically and setting them to the private variables. public function __construct() { parent::__construct(); // Stating the process by collecting data $this -> login_get_data(); } else { // Logging off if no form has been submitted and there's a GET in the URL // Logging out $this -> logout(); } } // Working on the register script if the register form was submitted $this -> register_get_clean_data(); } else { redirect("index.php"); } } public function login_get_data() { // Giving error if any of the fields are empty $_SESSION["login"]["error"] = "Make sure none of the fields are empty"; redirect("index.php"); } // Else, processing else { $this -> login_vars["Username"] = $_POST["username"]; $this -> login_vars["Password"] = $_POST["password"]; $this -> login_clean_data(); } } public function register_get_clean_data() { // Giving error in case any of the fields are empty $_SESSION["register"]["error"] = "Make sure none of the fields are empty."; redirect("index.php"); } // Verifying information // Full Name $_SESSION["register"]["error"] = "Make sure your full name is a valid one."; } // Phone Number $_SESSION["register"]["error"] .= "<br />" . "Make sure your phone number is a valid one."; } if(!is_valid_email($_POST["email"])) { $_SESSION["register"]["error"] .= "<br />" . "Make sure your e-mail is a valid one."; } else { $clean_email = sanitize($_POST["email"]); $_SESSION["register"]["error"] .= "<br />" . "Your email is already taken."; } } // Address $_SESSION["register"]["error"] .= "<br />" . "Make sure your address is a valid one."; } // Username $_SESSION["register"]["error"] .= "<br />" . "Your username is too long."; } else { $clean_user_name = sanitize($_POST["user_name"]); $user_name_check = mysql_query("SELECT * FROM Users WHERE 'Username' = '" . $clean_user_name . "'"); $_SESSION["register"]["error"] .= "<br />" . "Your username is already taken."; } } // Password if($_POST["pass_word"] !== $_POST["pass_word_verification"]) { $_SESSION["register"]["error"] .= "<br />" . "Make sure your passwords match."; } redirect("index.php"); } // Sanitizing the results "Country" => $_POST["country"], "Email" => $_POST["email"], "Address" => $_POST["address"], "Phone Number" => $_POST["phone_number"], "User_Name" => $_POST["user_name"], $this -> register_vars = $clean_register_results; $this -> register_add_data(); } public function login_clean_data() { // Function that sanitizes the POST data $this -> login_vars["Username"] = $clean_results["Username"]; // Processing the last step which is checking to see if what is provided is correct $this -> login_check_data(); } public function login_check_data() { // Checking the database for the given information $query = "SELECT * FROM Users WHERE `Username` = '" . $this -> login_vars["Username"] . "' && `Password` = '" . $this -> login_vars["Password"] . "'"; // If information is valid $_SESSION["id"] = 1; $_SESSION["username"] = $this -> login_vars["Username"]; $_SESSION["password"] = $this -> login_vars["Password"]; redirect("index.php"); } else { $_SESSION["login"]["error"] = "Username/Password combination is invalid."; redirect("index.php"); } } public function register_add_data() { // Adding the values to the database (`Username`, `Password`, `Full Name`, `Phone Number`, `Address`, `Country`, `Email`) VALUES ('" . $this -> register_vars["User_Name"] . "', '" . $this -> register_vars["Pass_Word"] . "', '" . $this -> register_vars["Full Name"] . "', '00" . $this -> register_vars["Phone Number"] . "', '" . $this -> register_vars["Address"] . "', '" . $this -> register_vars["Country"] . "', '" . $this -> register_vars["Email"] . "')"); redirect("index.php"); } public function logout() { // Exiting if user is not logged in if(!is_logged_in()) { } else { redirect("index.php"); } } } $start = new post_data_class(); ?>