I have a page I want to password-protect. I've tried doing HTTP authentication, but for some reason it doesn't work on my hosting. Any other quick (and easy) way to do this? Thanks!
|
Not exactly the most robust password protection here, so please don't use this to protect credit card numbers or something very important. Simply drop all of the following code into a file called (secure.php), change the user and pass from "admin" to whatever you want. Then right under those lines where it says include("secure.html"), simply replace that with the filename you want them to be able to see. They will access this page at [YouDomain.com/secure.php] and then the PHP script will internally include the file you want password protected so they won't know the name of that file, and can't later just access it directly bypassing the password prompt. If you would like to add a further level of protection, I would recommend you take your (secure.html) file outside of your site's root folder [/public_html], and place it on the same level as that directory, so that it is not inside the directory. Then in the PHP script where you are including the file simply use ("../secure.html"). That (../) means go back a directory to find the file. Doing it this way, the only way someone can access the content that's on the (secure.html) page is through the (secure.php) script.
|
|||||||||||||||||||||
|
And the login form on the page...
|
|||||
|
This is a bit late but I wanted to reply in case anyone else came upon this page and found that the highest reply was a bit off. I have improved upon the system just a tad bit. Note, it is still not amazingly secure but it is an improvement. First prepare your password salts file: hash_generate.php:
Take your output
now no one can peek into the hash. Next up is your index.php: index.php:
|
||||
|
If you want you can also make it so only certain ip addresses can login.. :) really easy with lighttpd Update: I will post some examples soon, so don't vote down for no examples, i just need to get some down for this answer. If you want to use sessions the following is the best way to go:
This method does not contain the examples for above but you seamed interested in this method. The other method examples are still to come, I have not got enough time to get it for apache or lighttpd settings and the php header auth: http://php.net/manual/en/features.http-auth.php Will do. |
||||
|
I would simply look for a
Now, if this page is located at say: You can now access it with: |
||||
|
Basically what I did here is make a page all in one php file where when you enter the password if its right it will hide the password screen and bring the stuff that protected forward. and then heres the css which is a crucial part because it makes the classes that hide and show the different parts of the page.
|
|||
|
Not the solution, but for your interest: HTTP authentication only works, when PHP runs as Apache module. Most hosters provide PHP as CGI version only. |
|||
|
This helped me a lot and save me much time, its easy to use, and work well, i've even take the risque of change it and it still works. Fairly good if you dont want to lost to much time on doing it :) |
|||||||||
|
You could use a simple if with a form:
But there are a lot of this kind of scripts online: https://www.codester.com/items/705/php-easy-lock-password-protect-php-script |
|||
|
You can use this: http://ps.lixter.com/loginexample.php
|
|||||||||
|