Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Werner van Strien
Werner van Strien
1,190 Points

PHP Login structure

Dear people who know,

I know to build a static page yet. I am a beginner.

For the company I work for I made a bootstrap setup page just for sharing info with our employees: www.hsadmin.nl.

To start working with it, I have to let our employees login, so the info stays secure and no competitors can't get to the info. In short: how do I do that?

What I want is that when an employee types in the address he immediatly have to put in his username and password to login, if not, he can't get further.

In the site is also a part for every employee where they can see there working hours. So in the page I want another login for every employee seperate. So other employees can't get to that information. Again: how do I do that?

Can someone help me on that way!?

Thanks in advance, a happy new year and regards,

Werner van Strien (The Netherlands)

2 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

A very basic implementation of this that does not require a database using PHP and cookies to set expirey time of login:

<?php
    $username = 'user'; // Set accepted username
    $password = 'password'; // Set accepted password
    $loginstatus = FALSE; // Sets loginstatus to false by default, content will only be shown if the following code changes this to TRUE

    if(!isset($_COOKIE['loggedin'])) { // If cookie loggedin not set.
        if(isset($_POST['username'])) {// Check if POST data exists from login form.
            if ($_POST['username'] === $username && $_POST['password'] === $password) { // If username and password correspond.
                setcookie('loggedin', TRUE, time() + (86400), '/'); // Sets loggedin cookie as true, 86400 = 1 day expirey, "/" sets the cookie to be accessed across the whole website.
                $loginstatus = TRUE; // Username and Password were correct so set $loginstatus to TRUE to allow user to view content
            } else { // Username or password did not match
                echo 'Username or Password incorrect please try again.'
            }
        }
    } else { // Cookie loggedin is set
        if ($_COOKIE['loggedin'] == TRUE) { // Double check the cookie value is TRUE
            $loginstatus = TRUE; // Cookie was set and valid so set login to TRUE
        }
    }
    if ($loginstatus !== TRUE) { // If $loginstatus not equal to TRUE display login form
        ?>
        <html>
        <body>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
                <label for="username">Username:</label><input type="text" name="username"><br>
                <label for="password">Password:</label><input type="password" name="password"><br>
                <input type="submit">
            </form>
        </body>
        </html>
        <?php 
    } else { // Else $loginstatus is equal to true so show restricted page content
        ?>
        <html>
        <body>
            <!-- Your restricted Content Here -->
        </body>
        </html>
        <?php
    }
    ?>

This is a basic example that you should be able to understand and work with quite early on into following the PHP track. It is not the best solution but is very minimal on required PHP knowledge to get to work if you need something quick.

Werner van Strien
Werner van Strien
1,190 Points

Thanks for the info. I am going to try!

Hey Werner,

I'd say you would need at least mid level PHP knowledge, but here are the basic steps:

First, you will need a database. Second, create the login page and the login form with pure html same with the other pages. Third, connect to the database creating a php page template and include it in your login page and everywhere else needed and apply php code to get the username and password from the login form and loop through your database.

Google it and you'll find everything you need :)