Archivatory.com Update | Now With User Accounts!

in #utopian-io6 years ago

archivatory-update-002.png

Repository

https://github.com/jrswab/archivatory

New Features

  • What feature(s) did you add?
    • Now using Bootstrap CSS styling
    • User accounts
      archivatoryUpdate001.png
      archivatoryUpdate002.png
      archivatoryUpdate003.png
    • User Upload List
      archivatoryUpdate004.png

How did you implement it/them?

Account Creation:

Apache uses PHP to send MySQL commands to the server as an authorized (but limited) user.

// Check input errors before inserting in database
    if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
        
        // Prepare an insert statement
        $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
         
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
            
            // Set parameters
            $param_username = $username;
            $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Redirect to login page
                header("location: login.php");
            } else{
                echo "Something went wrong. Please try again later.";
            }
        }
         
        // Close statement
        mysqli_stmt_close($stmt); 

See register.php on GitHub for the full code used

User Login

Again used PHP to look up the hashed and salted string in the database for the user's password.

// Validate credentials
    if(empty($username_err) && empty($password_err)){
        // Prepare a select statement
        $sql = "SELECT username, password FROM users WHERE username = ?";
        
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "s", $param_username);
            
            // Set parameters
            $param_username = $username;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Store result
                mysqli_stmt_store_result($stmt);
                
                // Check if username exists, if yes then verify password
                if(mysqli_stmt_num_rows($stmt) == 1){                    
                    // Bind result variables
                    mysqli_stmt_bind_result($stmt, $username, $hashed_password);
                    if(mysqli_stmt_fetch($stmt)){
                        if(password_verify($password, $hashed_password)){
                            /* Password is correct, so start a new session and
                            save the username to the session */
                            session_start();
                            $_SESSION['username'] = $username;      
                            header("location: welcome.php");
                        } else{
                            // Display an error message if password is not valid
                            $password_err = 'The password you entered was not valid.';
                        }
                    }
                } else{
                    // Display an error message if username doesn't exist
                    $username_err = 'No account found with that username.';
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        
        // Close statement
        mysqli_stmt_close($stmt);

See login.php on GitHub for the full code used
To make sure I did not get this wrong I used this guide when setting up account creation and sign in pages.

Create User table for storing upload information:

First make sure the user is logged in:


<?php
// Initialize the session
session_start();
 
// If session variable is not set it will redirect to login page
if(!isset($_SESSION['username']) || empty($_SESSION['username'])){
  header("location: login.php");
  exit;
}
?>

Then create the users' table if if it does not already exist.

// Loging info for database contianing user tables.
include_once 'config/uploadDBconfig.php';
// Check if a table call 'username' exists
if ($tableCheck = $link->query("SHOW TABLES LIKE '".$_SESSION['username']."'")) {
        if($tableCheck->num_rows == 1) {
                echo "Table Exists";
        } else {
            $sql = "CREATE TABLE " . $_SESSION['username'] . " (
            date TIMESTAMP NOT NULL,
            file_name VARCHAR(256) NOT NULL,
            hash VARCHAR(256) NOT NULL,
            file_size VARCHAR(256) NOT NULL,
            id VARCHAR(256) NOT NULL)";
            $link->query($sql);
        }
}

See memUp.php on GitHub for the full code used

User Upload List

First make sure the user is logged in:


<?php
// Initialize the session
session_start();
 
// If session variable is not set it will redirect to login page
if(!isset($_SESSION['username']) || empty($_SESSION['username'])){
  header("location: login.php");
  exit;
}
?>

Then build the list of uploads from the user created table on the user upload page.

<?php
                include_once 'config/uploadDBconfig.php';
                // query user data
                $sql = "SELECT * FROM ".$_SESSION['username']." ORDER BY date DESC;";
                $result = mysqli_query($link, $sql);
                $resultCheck = mysqli_num_rows($result);
                if ($resultCheck > 0) {
                    // Set up the html table
                    echo "<div class='table-responsive'>";
                    echo "<table class='table table-striped table-sm'>";
                    echo "<thead><tr><th scope='col'>Upload Date</th><th scope='col'>File Name</th><th scope='col'>IPFS Hash</th><th scope='col'>File Size (in bytes)</th></tr></thead>";
                    echo "<tbody>";
                    // loop through users' table and output into html table body    
                    while ($row = mysqli_fetch_assoc($result)) {
                        echo "<tr><td>".$row['date']."</td><td>".$row['file_name']."</td><td><a href='https://ipfs.io/ipfs/".$row['hash']."' target='_blank'>".$row['hash']."</a></td><td>".$row['file_size']."</tr>";
                    }
                    // end html table
                    echo "</tbody>";
                    echo "</table>";
                    echo "</div>";
                }
?>

See hashtable.php on GitHub for the full code used

Future Developments

  • Allow users to create an RSS feed of their IPFS content
  • Allow users to create playlists of their IPFS content
  • Allow the user to delete their upload from Archivatorys' server
  • Allow users to pin content from other sources (eg. Dtube, Dsound, Dlive)
  • User account settings

GitHub Account

https://github.com/jrswab


If you have any questions about the future of Steem
or my witness please feel free to message jrswab#3134 on Discord.

vote-jrswab-for-steem-Witnesses—Steemit.gif

Click here to vote with SteemConnect!
Or go to https://steemit.com/~witnesses
You can see all active witnesses on @drakos' steemian.info


Click here to join the mailing list and get exclusive SDB/STEEM giveaways!

Looking to support my content creation efforts outside of the Steem Blockchain?
Check out jrswab.com/support


Mastodon | Keybase | Twitter | Gitlab | Hacker Culture Podcast

Sort:  

Hey @jrswab
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Thanks for the contribution, @jrswab! It looks very clean now, definitely an improvement on the previous version!

A couple of tiny things:

  • Keep an eye on your indentation. Sometimes it's 2 spaces, sometimes 4, sometimes 8 - it's better to keep it consistent
  • Keep an eye on the line length, as some lines of code are pretty wide and this decreases your code's readability

Other than that, keep up the good work!

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks for the tips and kind words! I'll be mindful of the tabs and line length; maybe I'll do a cleanup day before my next feature integration to keep things easy on the eyes.

I agree. This website is absolutely Stellar JR! You are incredibly talented with web design.

Hahaha, all thanks to the Open-source toolkit known as Bootstrap!

;-)

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by GRASU 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.

Thank you for sharing this tutorial. I upvoted. But here https://speedysense.com/create-registration-login-system-php-mysql/ is another tutorial for login and registration system in PHP.

Coin Marketplace

STEEM 0.33
TRX 0.11
JST 0.035
BTC 67020.94
ETH 3270.13
USDT 1.00
SBD 4.62