Copy link to clipboard
Copied
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 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 se
...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 { ?>
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
@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?
Copy link to clipboard
Copied
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 { ?>
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I neglected to add that my site does not usee PHP.
Thanks.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Your site may not use PHP currently but your server probably supports it. Most Linux servers do. If not, you have a very strange hosting plan.
If you use the .htaccess and .htpasswd method, you'll need to create a new directory on your server.
Password Protection with htaccess - Htaccess Tools
NOTE: If you're on a Windows server, contact your hosting provider to help you.
Copy link to clipboard
Copied
I always thought the existence of .htaccess = Apache server = PHP
Copy link to clipboard
Copied
https://forums.adobe.com/people/John+Waller wrote
I always thought the existence of .htaccess = Apache server = PHP
Yup. Me too. They go together like peanut butter & jelly.
Copy link to clipboard
Copied
https://forums.adobe.com/people/John+Waller wrote
I always thought the existence of .htaccess = Apache server = PHP
.htaccess is available in Apache if configured through Apache to do so. PHP module would need to be installed on the Apache server in order for the server to support php. That's why LAMP stack is separate installations of Linux, Apache, MySQL, and PHP. They are separate entities that are combined to create a stack.
Apache installed on the server does not automatically mean that php is installed also. You have to specifically install the correct version of php module on the server and then restart the server in order for Apache to support php. In order words: just because you put peanut butter on a slice of bread does not mean that another slice of bread with jelly on it is automatically available. You need to put jelly on another slice of bread and put it together with the slice of bread that has peanut butter on it in order to have a PBJ sandwich.
Copy link to clipboard
Copied
Most reputable commercial web hosts include the LAMP stack as a matter of course. I can't think of any that don't -- at least none that I would use.
Copy link to clipboard
Copied
EbaySeller wrote
Apache installed on the server does not automatically mean that php is installed also.
Yes, of course.
It's just that whenever a client has been uncertain of their hosting plan's inclusions, every time I have found a .htaccess file it's an Apache server with PHP installed too.
In this case, mention of .htaccess but no Control Panel suggests a very basic hosting plan on a PHP/Apache server. Their site may not use PHP but it's available on the server with an upgraded hosting plan.
Copy link to clipboard
Copied
You both mention hosting plans assuming that everyone uses hosting plans, which is an incorrect assumption.
I don't use a web host and never would since I have no problem fully-managing my own servers. Believe it or not there are many, many competent full stack developers that don't use commercial web hosting either. OP never mentioned that they were using a commercial web host anyway... perhaps a server admin setup an apache server and didn't install php since OP told server admin they had no need for php. Managing your own server would not have a Cpanel either since it's managed through the shell. None of us can determine what the situation is other than the OP. You should be aware that it is possible to have an Apache server that does not run php though.
I understand what you're both saying though: inexperienced people that create small traffic ma and pa websites (the typical DW user) use hosting plans managed by companies because they do not have the in-depth technical knowledge to manage a server on their own, and those hosting plans typically have php supported out of the box.
Copy link to clipboard
Copied
EbaySeller wrote
I understand what you're both saying though: inexperienced people that create small traffic ma and pa websites (the typical DW user) use hosting plans managed by companies because they do not have the in-depth technical knowledge to manage a server on their own, and those hosting plans typically have php supported out of the box.
Yep. That's all my comments were based on. I'm aware that Apache servers don't necessarily run PHP. Even Windows servers can run PHP but we rarely see posts from those kinds of technically adept people here.
I assume this OP needs some assistance with a basic website on an entry level commercial hosting plan. And that's fine. That's what these forums are for.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
The snippet of code you posted is correct as far as my eyes can tell. Double check to make sure you have not accidentally deleted something when removing the empty lines.
What is the error message you are getting?
Copy link to clipboard
Copied
I went back and totally copied and pasted. I added my include statement which should take me to the secured page upon entering the right information. That part works great and I love it. However, I do get this on my login screen:
Warning: Invalid argument supplied for foreach() in /home/ccwatersys/public_html/templates/secure_information.php on line 64
Line 64 is where the foreach statement is.
Jim.
Copy link to clipboard
Copied
Youve most likely broken the code somehow when adding the additional include code.
If you paste your complete version of the code in the forum l'll check it over to see if l can identify whats wrong. I cant do it until tomorrow at some stage as lm on one of those silly tablet devices atthe moment but if you post the code and check back at about the same time tomorrow l will hopefully be able to resolve why you are getting the error.
One of the other regulars like Nancy or Ben might be able to identify the issue quicker for you, sometimes they like a bit of a challenge.
Obviously post whats in the include files as well if you do decide to post so we have all the code necessary to run some tests
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Its only a warning not an error. Try surrounding the foreach code loop in an is_array block as below:
<?php if(is_array($error)) {
foreach($error as $errors) {
}
}
?>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Actually no need for this extra code I speak of in my previous post. I was under the impression that the secure page information was a seperate page to the login page but its not , the login code and the secure information include page is combined into one page.The secure information will only be shown if the username and password combination is entered correctly. If you navigate to the page directly only the login is shown to the visitor.....so all is good as it is.
Using php session variables across a scope of pages to check if the username and password has been set is only applicable if the login and the secure pages are seperate pages NOT when they are combined as one page.