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

create a login for users

Hi

How you create a sign up form with a login, so users can long in and see the details?

7 Answers

Grace Kelly
Grace Kelly
33,990 Points

Hi James, usually with sign up forms we use databases to store the information and then in the log in process the details are checked against the values in the database to see if they match. Sessions are also used to hold information about the user while they are logged in. There is a great course here on treehouse that explains databases and you can read more about sessions here.

But to show you an example of a very simple log in form you can do the following:

<form action="" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username">

    <label for="password">Password:</label>
    <input type="password" id="password" name="password">

    <input type="submit" name="user_login" value="Login">
</form>
<?php

$username = "mike";
$password = "sesame";

if($_SERVER["REQUEST_METHOD"] === "POST"){

    if($_POST["username"] === $username && $_POST["password"] === $password) {

        echo "Welcome Back " . $username . "!";

    } else {

        echo "Sorry, your login details are incorrect";
    }

}
?>

This isn't ideal as the username has to be "mike" and the password has to be "sesame", so really only one user can use this system.

Hope that helps!!

Hi

i found this site helped http://www.phpeasystep.com/phptu/6.html

once i chaged some code

Dont use session_register(), session_is_registered() and session_unregister()

You can use $_SESSION[] instead of that

Jiri Stepanek
Jiri Stepanek
10,740 Points

Hi, first you need to learn how to use MySQL database and implement it in your code. Then users can create their accounts with passwords and other stuff... ;)

Jiri Stepanek
Jiri Stepanek
10,740 Points

Hi, first you need to learn how to use MySQL database and implement it in your code. Then users can create their accounts with passwords and other stuff... ;)

Hi

yea I have done Using PHP with MySQL in treehouse and have connected my php page to the mysql data base.

Just not sure what the next step is .....

Make sure you hash passwords. I have to learn how to do that, but I know it is necessary.

You mean to tell me there is not a php lesson here on Team Treehouse for this?

I believe they are developing one, but it does not exist yet.