Skip to main content
Participant
June 21, 2017
Answered

Create a password protect page in Dreamweaver CC

  • June 21, 2017
  • 4 replies
  • 10224 views

I am updating my website. One of the features the current/soon to be former site has is a page that is password protected. My web host does not have a C Panel login therefore, after researching, I believe my option is to use .htaccess. I found the .htaccess file however I am at a loss as to what to do with my page.

Any suggestions?

This topic has been closed for replies.
Correct answer osgood_

If you named the page login.php then you need to change the action attribute in the form tag -

 

From:

<form name="login" action="secure_information.php" method="post">

 

To:

<form name="login" action="login.php" method="post">

 

 

If you do that then the code should work.

 

Any protected information should be inserted after the <!--  --> (comment tags) in the code:

<!-- PROTECTED INFORMATION GOES HERE -->

 

and before

 

<?php } else { ?>

 

 

 

 

4 replies

jimg82619726
Participant
June 12, 2018

I love this and it actually works great for me, but...

I am getting an error on this line:

<?php foreach($error as $errors) {

echo "<p style='color: red;'>".$errors."</p>";

}

?>

I did not change anything but I did remove many empty lines. I am new to PHP and I am wondering why the foreach statement causes errors.

JG.

jimg82619726
Participant
June 12, 2018

This works, but I am getting an error. I copied and pasted the entire example and saved it. The only difference is on line 25 which is my "include" statement to take the user to the secured web page. Here is the entire contents of my "secure_information.php" file:

<?php

$username = "";

$password = "";

if(isset($_POST['submit'])) {

$username = $_POST['username'];

$password = $_POST['password'];

if($username != "Bunny") {

$error['username'] = "Wrong Username";

}

if($password != "Rabbit") {

$error['password'] = "Wrong Password";

}

}

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8"/>

<title>Secure Information</title>

</head>

<body>

<?php if($username == "Bunny" && $password == "Rabbit") { ?>

<h1>Welcome</h1>

<!-- PROTECTED INFORMATION GOES HERE -->

<?php include("../info.php"); ?>

<?php } else { ?>

<h2>Login</h2>

<?php foreach($error as $errors) {

echo "<p style='color: red;'>".$errors."</p>";

}

?>

<form name="login" action="secure_information.php" method="post">

<p>

<label for="username">Username</label>

<input type="text" id="username" class="username" name="username" placeholder="Username" value="<?php if(isset($username)) { echo $username; } ?>">

</p>

<p>

<label for="password">Password</label>

<input type="text" id="password" class="password" name="password" placeholder="Password" value="<?php if(isset($password)) { echo $password; } ?>">

</p>

<p>

<input type="submit" name="submit" value="Submit">

</p>

<?php } ?>

</form>

</body>

</html>

When I run this, I get the following error:

Warning: Invalid argument supplied for foreach() in /home/ccwatersys/public_html/templates/secure_information.php on line 28

Legend
June 12, 2018

PERFECT!

The working code is:

<?php if(is_array($error)) { foreach($error as $errors) {

echo "<p style='color: red;'>".$errors."</p>";

}

}

Now if I could only find the "Correct" option for this answer. Hmmmmmmm.


Great, glad its working for you, so far.

Ok so now you need a way of securing your secure page should anyone accidentallly come upon it whille traweling the internet because at the moment that is unprotected.

This involves checking to see if the username/password has been set and if it has not then anyone that attempts to access your secure page directly without going via the log in page will get sent back to the log in page.

I have covered this in one of my many other posts in the forum in the past regarding password protected pages it requires starting a php session and passing information from page to page

I will post the code here tomorrow when l get back onto a real bit of kit not this junky tablet.

John Waller
Community Expert
Community Expert
June 21, 2017

bethd9416885  wrote

I am updating my website.

What is the website address?

Do yo have a hosting plan which includes PHP? If not, what hosting plan do you have?

EPAStaffAuthor
Participant
June 21, 2017

I neglected to add that my site does not usee PHP.

Thanks.

Legend
June 21, 2017

bethd9416885  wrote

I neglected to add that my site does not usee PHP.

Thanks.

Oh well might be useful for someone else. This question is quite common in the forum.

Someone will probably be able to offer an alternative solution based on what you have available - htaccess

Legend
June 21, 2017

This can get as complicated as you like or as easy as you like. If your server can support php and you just need to protect the one page with a generic Username and Password combination then it's very easy. Example code below. You just need to change the current Username/Password combination from Bunny/Rabbit (marked in red) to that of your own choice and insert the code you want to protect where it says <!-- PROTECTED INFORMATION GOES HERE -->

Create a new completely blank DW file and name it secure_information.php - copy code and paste The login form is incorporated within the code. Its not styled, just a bare bones example of  how you would go about creating a simple protected information solution.

<?php

$username = "";

$password = "";

if(isset($_POST['submit'])) {

$username = $_POST['username'];

$password = $_POST['password'];

if($username != "Bunny") {

$error['username'] = "Wrong Username";

}

if($password != "Rabbit") {

$error['password'] = "Wrong Password";

}

}

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8"/>

<title>Secure Information</title>

</head>

<body>

<?php if($username == "Bunny" && $password == "Rabbit") { ?>

<h1>Welcome</h1>

<!-- PROTECTED INFORMATION GOES HERE -->

<?php } else { ?>

<h2>Login</h2>

<?php foreach($error as $errors) {

echo "<p style='color: red;'>".$errors."</p>";

}

?>

<form name="login" action="secure_information.php" method="post">

<p>

<label for="username">Username</label>

<input type="text" id="username" class="username" name="username" placeholder="Username" value="<?php if(isset($username)) { echo $username; } ?>">

</p>

<p>

<label for="password">Password</label>

<input type="text" id="password" class="password" name="password" placeholder="Password" value="<?php if(isset($password)) { echo $password; } ?>">

</p>

<p>

<input type="submit" name="submit" value="Submit">

</p>

<?php } ?>

</form>

</body>

</html>

Participant
June 28, 2024

@osgood_ I am looking to create a page within my website where members log in to view articles in a private area.   I created a php page with your code as above, I called it log in, but wasn't sure what I had to put in the protected information part as that area is greyed out and won't allow me to change it, basically I want members to be directed to a new page?   

osgood_Correct answer
Legend
June 28, 2024

If you named the page login.php then you need to change the action attribute in the form tag -

 

From:

<form name="login" action="secure_information.php" method="post">

 

To:

<form name="login" action="login.php" method="post">

 

 

If you do that then the code should work.

 

Any protected information should be inserted after the <!--  --> (comment tags) in the code:

<!-- PROTECTED INFORMATION GOES HERE -->

 

and before

 

<?php } else { ?>