Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- $username = "";
- $email = "";
- $errors = array();
- $db = mysqli_connect('localhost','root','','registration');
- if(isset($_POST['register'])){
- $username = mysqli_real_escape_string($db,$_POST['username']);
- $email = mysqli_real_escape_string($db,$_POST['email']);
- $password = mysqli_real_escape_string($db,$_POST['password']);
- $password2 = mysqli_real_escape_string($db,$_POST['password2']);
- if(empty($username)){
- array_push($errors, "Username is required");
- }
- if(empty($email)){
- array_push($errors, "Email is required");
- }
- if(empty($password)){
- array_push($errors, "Password is required");
- }
- if($password != $password2){
- array_push($errors, "The two password do not match");
- }
- if(count($errors) == 0){
- $sql = "INSERT INTO users (username, email, password) VALUES ('$username','$email','$password')";
- mysqli_query($db, $sql);
- $_SESSION['username'] = $username;
- $_SESSION['success'] = "You are now logged in";
- header('location:index.php');
- }
- }
- if(isset($_POST['login'])){
- $username = mysqli_real_escape_string($db,$_POST['username']);
- $password = mysqli_real_escape_string($db,$_POST['password']);
- if(empty($username)){
- array_push($errors, "Username is required");
- }
- if(empty($password)){
- array_push($errors, "Password is required");
- }
- if(count($errors) == 0){
- $query = "SELECT * FROM users WHERE username ='$username' AND password ='$password'";
- $result = mysqli_query($db, $query);
- if(mysqli_num_rows($result) > 0){
- header('location:add.php');
- } else{
- array_push($errors, "wrong username or password");
- }
- }
- }
- if(isset($_GET['logout'])){
- session_destroy();
- unset($_SESSION['username']);
- header('location: login.php');
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement