Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REGISTER
- ________
- <?php
- require_once("dbconnect.php");
- $error = null;
- if(isset($POST['btn-register'])){
- $email = trim($_POST['email']);
- $email = strip_tags($email);
- $email = htmlentities($email, ENT_QUOTES);
- $pass = trim($_POST['password']);
- $pass = strip_tags($pass);
- $pass = htmlentities($pass, ENT_QUOTES);
- $user = trim($_POST['user']);
- $user = strip_tags($user);
- $user = htmlentities($user, ENT_QUOTES);
- $mysqli_query = sprintf("SELECT userEmail FROM users WHERE userEmail = '$email'", mysqli_real_escape_string($db, $email));
- $conn = $db->query($mysqli_query);
- if (strlen($pass) > 6) {
- if ($conn->num_rows <= 0) {
- $conn->close();
- $mysqli_query = sprintf("SELECT userName FROM users WHERE userName = '$user'", mysqli_real_escape_string($db, $user));
- $conn = $db->query($mysqli_query);
- if ($conn->num_rows <= 0) {
- $conn->close();
- $stmt = $db->prepare("INSERT INTO users (userName, userPassword, userEmail) VALUES (?, ?, ?)");
- $stmt->bind_param("sss", $user, $password, $email);
- $password = password_hash($pass, PASSWORD_BCRYPT);
- $stmt->execute();
- echo "Successfully registered";
- exit;
- }
- else {
- $error = "Username is already taken";
- echo $error;
- exit;
- }
- }
- else {
- $error = "Email is already taken";
- echo $error;
- exit;
- }
- }
- else {
- $error = "Password most be higher than 6";
- echo $error;
- exit;
- }
- }
- ?>
- LOGIN
- _____
- <?php
- session_start();
- require_once("dbconnect.php");
- $error = null;
- if(isset($_POST['btn-login'])) {
- $email = trim($_POST['email']);
- $email = strip_tags($email);
- $email = htmlentities($email, ENT_QUOTES);
- $pass = trim($_POST['password']);
- $pass = strip_tags($pass);
- $pass = htmlentities($pass, ENT_QUOTES);
- if(!empty($email && $pass)) {
- $stmt = $db->prepare("SELECT userId, userName, userPass FROM users WHERE userEmail = '$email'");
- $stmt->execute();
- $row = $stmt->fetch();
- $password = password_hash($pass, PASSWORD_BCRYPT);
- if($stmt->num_rows >= 0 && $row['userPass'] == $password) {
- $_SESSION['user'] = $row['userId'];
- header('Location: home.php');
- }
- else {
- $error = "Incorrect Credentials, Try again...";
- echo $error;
- exit;
- }
- }
- if(empty($email)) {
- $error = "Please enter your email address.";
- echo $error;
- exit;
- }
- elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $error = "Please enter a valid email address.";
- echo $error;
- exit;
- }
- if(empty($pass)) {
- $error = "Please enter your password.";
- echo $error;
- exit;
- }
- }
- ?>
- DBCONNECT
- _________
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "test";
- $db = new mysqli($servername, $username, $password, $dbname);
- if ($db->connect_error) {
- die("Connection failed: " . $db->connect_error);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement