Programming Tutorial #3: Databases (Signup / Login in PHP)

in #programming6 years ago

Welcome to the database setup tutorial.

See This if you haven't seen the previous tutorial.

After starting wamp/lamp/xampp navigate to http://localhost/phpmyadmin or http://localhost:port/phpmyadmin and log in with username root and no password.

Now, click the SQL tab near the top of the page and enter this code into the box:

CREATE DATABASE loginsystem;
CREATE TABLE loginsystem.`users` (`username` tinytext NOT NULL,`password` tinytext NOT NULL,`email` tinytext NOT NULL,`display` tinytext NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;

After the database and table are created, click on loginsystem in the sidebar and then in the topbar click privileges and then add new account.

Enter "LoginSystem" or whatever you want as the username (case sensitive), but remember it, then go down to generate password and copy the password it generates, note it down (case sensitive). Then make sure that this checkbox is unchecked and then click go at the bottom of the page.

Next, click loginsystem in the sidebar, and then click priveliges once again, then click edit priveliges next to the new account you created, then make sure your page looks like this , and then press go.

Now, go to the www folder or htdocs folder and make a folder inside of it called "scripts", then enter this folder and create "config.php" and enter the following code:

<?php
//Database Config:

return (object) array(
    'host' => 'localhost',
    'username' => 'YourUser',
    'pass' => 'YourPassword',
    'database' => 'loginsystem',
    'useStaticCost' => true, //Set to true when you have worked out your time on your server.
    'staticCost' => 12, //Work out by going to /scripts/benchmark.php?getStaticCost
    'domain' => 'localhost'
);
?>

Make sure to replace YourUsername and YourPassword with the credentials (username & password) created previously.
Leave useStaticCost, staticCost & Domain for the moment.

Now we need a way to handle passwords, so create the file "passwordHandler.php" inside of the scripts folder and type this code:

<?php
function verifyPassHash($hash,$password) {
    $md5 = md5($password);
    $base64 = base64_encode($md5);
    if (password_verify($base64,$hash)) {
        return true;
    } else {
        return false;
    }
}

function hashPass($password) {
    /*Send To DB*/
    /*Gets Cost For A 250ms Delay!*/
    $config = include 'config.php';
    if ($config->useStaticCost) {
    $options = [
        'cost' => $config->staticCost,
    ];} else {
        $benchmarker = include 'benchmark.php';
        $options = [
            'cost' => $benchmarker->cost,
    ];}
    $md5 = md5($password);
    $base64 = base64_encode($md5);
    $hashedPass = password_hash($base64,PASSWORD_BCRYPT,$options);
    return $hashedPass;
} 

The first line defines the php language tags
The second line creates a function.
The third one MD5 Hashes The Password
The fourth one encodes it with base64
Lines 5-9 Check whether the password is correct.
Line 10 Ends The Function

Line 12 Creates a new function
Lines 13 & 14 Are Comments
Line 15 Gets the new configuration file
Lines 16-23 Generate The Correct Cost if not specified in the config.
Line 24 MD5 Hashes it
Line 25 Base64 Hashes it to remove weird characters
Line 26 Hashes the password
Line 27 Gives the hashed password back out
Line 28 Ends The Function

Now, Finally Create a file in scripts called Benchmark.php and enter this code:

<?php
function getOptimalBcryptCostParameter($min_ms = 250) {
    for ($i = 4; $i < 31; $i++) {
        $options = [ 'cost' => $i, 'salt' => 'usesomesillystringforsalt' ];
        $time_start = microtime(true);
        password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
        $time_end = microtime(true);
        if (($time_end - $time_start) * 1000 > $min_ms) {
            return $i;
        }
    }
}

if (isset($_GET['getStaticCost'])) {
    print("in 250ms(min) you can decrypt a password of cost: ".getOptimalBcryptCostParameter());
}

return (object) array(
    'cost' => getOptimalBcryptCostParameter()
);
?>

Save it and navigate to:
http://localhost/scripts/benchmark.php?getStaticCost or
http://localhost:8080/scripts/benchmark.php?getStaticCost or
http://localhost:port/scripts/benchmark.php?getStaticCost

Then take the number at the end of: (Underlined in red) and re-open "config.php" and enter the number that you got in place of the 12 here: and make sure useStaticCost is set to true.

Cya next week when we will make the bulk of the system!
Get The Code for this Lesson On Github

Good luck and happy coding
~CA_Dawg
Sort:  

Congratulations @cadawg! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You got your First payout
Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly – Featured Posts are voted every 2.4hrs
Join the Curation Team Here

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by Snaddyvitch Dispenser from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Greetings! I am a minnow exclusive bot that gives a 5X upvote!
I recommend this amazing guide on how to be a steemit rockstar!
I was made by @EarthNation to make Steemit easier and more rewarding for minnows.

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 63373.75
ETH 3170.63
USDT 1.00
SBD 3.88