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 trialDan Petrie
8,228 PointsLinking 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
3,098 PointsYou 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
8,228 PointsHi Kim, thankyou for the reply. So above my html code I should put this code? Dan
Kim Jami
3,098 PointsYes 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.