Build Your Own Server In PHP (Hyper Text Preproccessor)

By Mark Chidozie   10 months ago   152
Programming

<?php
// start your session

session_start();

// declare all your variables

$username = "";
$email    = "";
$errors   = array();
$_SESSION['success'] = "";
$_SESSION['update'] = "Profile Successfully Updated";

// connect to a database

$db = mysqli_connect('localhost', 'root', '', 'sirtony');

// register button is clicked

if (isset($_POST['reg_user'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);    
    $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);    
    $profileImageName = time() . '-' . $_FILES["avatar"]["name"];
    $target_dir = "images/";
    $target_file = $target_dir . basename($profileImageName);
    
    // form validation check if form was properly filled
    
    if (empty($username)) {
        array_push($errors, "Username field must not be empty!");
    }
    
    if (empty($email)) {
        array_push($errors, "Email field must not be empty!");
    }
    
    if ($_FILES['avatar']['size'] > 900000) {
        array_push($errors, "Files size too large <br>should not exceed 900 Kilobites");
    }
    
    if(!preg_match("!image!", $_FILES['avatar']['type'])) {
        array_push($errors, "Only .GIF or .PNG or .JPG files are Supported");
    }
    
    if (empty($password_1)) {
        array_push($errors, "Password fields must not be empty!");
    }
    
    if (strlen($password_1) < 6) {
        array_push($errors, "Passwords must be minimum of Six Characters");
    }
    
    if (strlen($password_1) > 10) {
        array_push($errors, "Passwords must be a maximum of 10 Characters");
    }
    
    if ($password_1 != $password_2) {
        array_push($errors, "The passwords you entered do not match, please check your inputs");
    }
    
    // check if a user already exists in the database
    
    $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
    $result = mysqli_query($db, $user_check_query);
    $user = mysqli_fetch_assoc($result);
    
    if ($user) {
        if ($user['username'] == $username) {
            array_push($errors, "$username is already in use by another user, <br> <a href='login.php'>Log In</a> if This is you");
        }
        
        if ($user['email'] == $email) {
            array_push($errors, "$email is already in use by another user, please use another");
        }
    }
    
    // Proceed to register the user if no errors occur
    
    if (count($errors) == 0) {
        move_uploaded_file($_FILES['avatar']['tmp_name'], $target_file);
        $password = md5($password_1);
        $query = "INSERT INTO users SET username='$username', email='$email', password='$password', pass2='$password_2', avatar='$profileImageName'";
        mysqli_query($db, $query);
        $_SESSION['username'] = $username;
        $_SESSION['email'] = $email;
        $_SESSION['success'] = "Congrats! your registration was successful";
        header('location: index.php');
    }        
}

//... Login Control

if(isset($_POST['login_user'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);
    
    // Form validation
    
    if (empty($username)) {
        array_push($errors, "Username field must not be empty!");
    }
    
    if (empty($password)) {
        array_push($errors, "Password field must not be empty!");
    }
    
    // Check if user reallt exists
    //''
    //$
    if (count($errors) == 0) {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $result = mysqli_query($db, $query);
        if (mysqli_num_rows($result) == 1) {
        $_SESSION['username'] = $username;
        $_SESSION['email'] = $email;
        $_SESSION['success'] = "Congrats, you are now logged in";
        header('location: index.php');
    } 
        else {
            array_push($errors, "Wrong Username and Password Combination<br>Create An<a href='register.php'> Account</a> If You're Not Yet A Member");
        }
    }
}

// ...
    
if (isset($_POST['upd_user'])) {
    
    $password = "";
    $password_2 = "";
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $password = mysqli_real_escape_string($db, $_POST['password']);
    $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
    $ImageName = time() . '-' . $_FILES["avatar"]["name"];
    
    
    //image location
    $target_dir = "images/";
    $target_file = $target_dir . basename($ImageName);
    
    
    if($_FILES['avatar']['size'] > 900000) {
        array_push($errors, "Files size too large <br>should not exceed 900 Kilobites");
    }
    
    
    
    
    // Form validation
    
    
    
    if (empty($username)) {
        array_push($errors, "Username field must not be empty!");
    }
    
    if (empty($email)) {
        array_push($errors, "Email field must not be empty!");
    }
    
    if (empty($password)) {
        array_push($errors, "Password field must not be empty!");
    }
    
    if (strlen($password) < 6) {
        array_push($errors, "Passwords must be at least six characters long!");
    }
    
    if (strlen($password) > 10) {
        array_push($errors, "Passwords must be a maximum of ten characters long!");
    }
    
    if(!preg_match("!image!", $_FILES['avatar']['type'])) {
        array_push($errors, "Only .GIF or .PNG or .JPG files are Supported");
    }
    
    
    if ($password != $password_2) {
        array_push($errors, "The passwords you entered do not match, please check your inputs");
    }
    
    // check if a user already exists in the database
    
    $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
    $result = mysqli_query($db, $user_check_query);
    $user = mysqli_fetch_assoc($result);
    
    if ($user) {
        if ($user['username'] == $username) {
            array_push($errors, "$username is already in use by another user, please use another");
        }
        
        if ($user['email'] == $email) {
            array_push($errors, "$email is already in use by another user, please use another");
        }
    }
    
    // Proceed to update the user if no errors occur
    
    if (count($errors) == 0) {
        move_uploaded_file($_FILES["avatar"]["tmp_name"], $target_file);
        $password = md5($password);
        $query = "UPDATE users SET username='$username', email='$email', password='$password', pass2='$password_2', avatar='$ImageName' WHERE username='".$_SESSION["username"]."'";
        mysqli_query($db, $query);
        $_SESSION['win'] = "<p style='background−color: green'>Profile Successfully Updated, <br> You Have To Log Out For Changes To Take Place... Log in with your new details</p>";
        
    }        
}

if(isset($_POST['del_user'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);
    $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
    
    // Form validation
    
    if (empty($username)) {
        array_push($errors, "Username field must not be empty!");
    }
    
    if (empty($password)) {
        array_push($errors, "Password fields must not be empty!");
    }
    
    if ($password != $password_2) {
        array_push($errors, "The two passwords you entered, do not match, do you want to <a href='index.php'>Quit?</a>");
    }
    
    // Check if user reallt exists
    //''
    //$
    if (count($errors) == 0) {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='".$_SESSION["username"]."' AND password='$password'";
        $result = mysqli_query($db, $query);
        if (mysqli_num_rows($result) == 1) {
        $query_2 = "DELETE FROM users WHERE username='".$_SESSION["username"]."'"; 
        $done = mysqli_query($db, $query_2);
        if($done) {
        $_SESSION['del'] = "<b style='font-size: 30px'> $username</b> your Account Has Been Successfully Deleted!</b>";
        unset($_SESSION['username']);
        header('location: login.php');
        } else {
            array_push($errors, "An Unknown Error Occured, While Handling You Request");
        }
        
    } 
        else {
            array_push($errors, "Wrong Username and Password Combination!.");
        }
    }
}

?>

[NewsNaira]


Php Server Free Server


Share this article!


Login to comment and earn


Godwin John
10 months ago

Omg u knw wetin dey happen


(Quote) (Likes)
James
10 months ago

Tggr rgvd egyyv cresfgg


(Quote) (Likes: 2)