Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- //
- // $encrypted = my_simple_crypt( $password, 'e' ); Encrypting an password
- // $decrypted = my_simple_crypt( $password, 'd' ); Decrypting an password
- //
- // INSERT DATA INTO MYSQL: INSERT INTO userinformation (firstname,lastname,email,username,password) values ("ff","ff","ff","ff","ff")
- //
- //Start up the database conversation.
- $usernameError = "";
- $emailError = "";
- $registerSucces = "";
- $loginUsernameError = "";
- $servername = "localhost";
- $username = "root";
- $password = "";
- $database = "inlogscherm";
- $conn = new mysqli($servername, $username, $password, $database);
- if ($conn->connect_error){ echo "Connection failed please contact your service provider."; die("Connection failed: " . $conn->connect_error); }
- //Check if a user wants to login.
- if(isset($_POST["login-username"]) && isset($_POST["login-password"])){
- $GETusername = $_POST["login-username"];
- $GETpassword = $_POST["login-password"];
- $GETpassword = my_simple_crypt( $GETpassword, 'e' );
- checkAuth($GETusername, $GETpassword);
- header('Location: /Inlogscherm/index.php');
- }
- //Check if a user is already loged in.
- if(isset($_SESSION["password"]) && isset($_SESSION["username"])){
- checkAuth($_SESSION["password"], $_SESSION["username"]);
- }
- //Check if a user wants to send an register form.
- if(isset($_POST["register-firstname"]) && isset($_POST["register-lastname"]) && isset($_POST["register-email"]) && isset($_POST["register-username"]) && isset($_POST["register-password"])){
- $GETfirstname = $_POST["register-firstname"];
- $GETlastname = $_POST["register-lastname"];
- $GETemail = $_POST["register-email"];
- $GETusername = $_POST["register-username"];
- $GETpassword = $_POST["register-password"];
- insertAuth($GETfirstname, $GETlastname, $GETemail, $GETusername, $GETpassword);
- }
- //Checks the given user authecation with the database information.
- function checkAuth($GETusername, $GETpassword){
- global $conn, $loginUsernameError;
- $userInformation = $conn->query("SELECT username,password FROM userinformation WHERE username='$GETusername' and password='$GETpassword'");
- $userInformation = $userInformation->fetch_assoc();
- if($userInformation["username"] == $GETusername && $userInformation["password"] == $GETpassword){
- $_SESSION["password"] = $userInformation["username"];
- $_SESSION["username"] = $userInformation["password"];
- return;
- }
- $usernameError = "<div class='register-error'>Sorry... The username and/or password is wrong.</div>";
- }
- //Inserting user given authecation data.
- function insertAuth($GETfirstname, $GETlastname, $GETemail, $GETusername, $GETpassword){
- global $conn, $usernameError, $emailError ;
- $username = $conn->query("SELECT username FROM userinformation WHERE username='$GETusername'");
- $mail = $conn->query("SELECT email FROM userinformation WHERE email='$GETemail'");
- if($mail->num_rows == 0 && $username->num_rows == 0){
- echo $GETpassword;
- $GETpassword = my_simple_crypt( $GETpassword, 'd' );
- $result = $conn->query("INSERT INTO userinformation (firstname, lastname, email, username, password) values ('$GETfirstname', '$GETlastname', '$GETemail', '$GETusername', '$GETpassword')");
- }
- if($mail->num_rows == 1){
- $emailError = "<div class='register-error'>Sorry... The email has already been used.</div>";
- }
- if($username->num_rows == 1){
- $usernameError = "<div class='register-error'>Sorry... The username has already been used.</div>";
- }
- }
- //Logout function.
- function logout(){
- echo "<script>alert('Your now logout of your account');</script>";
- session_destroy();
- header('Location: /Inlogscherm/index.php');
- }
- function my_simple_crypt( $string, $action = 'e' ) {
- // you may change these values to your own
- $secret_key = 'KJH23dw0234hj@$#kl53wfnFWK;wqir756@#ohjfs012-3$#';
- $secret_iv = 'sdf32rSDFl23e@#wklh24ds;loqjF21!9234@#0dfFWo$@#ier';
- $output = false;
- $encrypt_method = "AES-256-CBC";
- $key = hash( 'sha256', $secret_key );
- $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
- if( $action == 'e' ) {
- $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
- }
- else if( $action == 'd' ){
- $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
- }
- return $output;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement