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

Dan Petrie
Dan Petrie
8,228 Points

Linking html login page with PHP and MYSQL

Hello,

I have created a website from a friend who wants to password four webpages on his website.

I am fairly new to php programming but have installed WAMP on my computer.

I am happy to share the website with anyone who maybe able to point me in the right direction?

3 Answers

Kim Jami
Kim Jami
3,098 Points

You could store a session and check if the session is set on each password protected page.

password-protected-page.php

<?PHP
//init session
session_start();

if(isset($_SESSION["user"]) && $_SESSION["user"] == true)) {
// show the password protected page
} else {
// redirect to the login page
header('Location: login.php');
die();
}

// rest of the password protected page

login.php

<?PHP
// set session
if(isset($_POST["password"]) && $_POST["password"] == "mypassword") {
// init session
session_start();
$_SESSION["user"] = true;
//redirect to the password protected page
header('Location: password-protected-page.php');
}
?>

<form method="post" action="">
<input type="password" name="password" />
</form>

Note: This is a very simple approach, and if you're even considering it you should atleast md5 the password, also note the code I've posted may included syntax errors as I have not tested it, but it should atleast give you an idea how to approach it.

<?PHP
md5($_POST["password"]) == "mymd5password"
?>
Dan Petrie
Dan Petrie
8,228 Points

Hi Kim, thankyou for the reply. So above my html code I should put this code? Dan

Kim Jami
Kim Jami
3,098 Points

Yes exactly, as you can see I've specified two documents, login.php and password-protected-page.php.

In the password-protected-page.php all your HTML code goes below the ?> php tag, right where I've written "// rest of the password protected page".

Good luck.