I made a password protected page script in PHP, which is actually embedded in the .php file you want to put a password on. I actually made this for a friend just a while ago and decided to share it with everyone that doesn't know how to do it.
<?php //You can change this password to anything you want... If you want double quotes in your password, You need to escape them like this: \" $password = "macbookpro"; $enteredPass = $_POST["password"]; if (!($enteredPass == $password)) { die('<div style="text-align: center;"><p>This page is protected with a password!</p><form name="input" action="' . $_SERVER['PHP_SELF']. '" method="POST">Password: <input type="password" name="password"><input type="submit" value="Submit"></form></div>'); } // Anything after this line will only be shown if the password is entered! ?> <!DOCTYPE html> <html> <head> <title>Protected page test</title> </head> <body> <p>This text will only appear after the password has been entered!</p> </body> </html>
That is the complete file. Just save it in a file with the .php file extension.
Feel free to change this any way you want!
Offline
I might use that sometime if I actually consider learning php
Offline
If you want to make it so that people can't find the password by looking at the file source (say they get on your computer), so hash the input, and put the hash in the code instead of the password.
Offline
shouldn't
(!($enteredPass == $password))
be
($enteredPass!=$password)
that's what i always thought
Offline
scimonster wrote:
If you want to make it so that people can't find the password by looking at the file source (say they get on your computer), so hash the input, and put the hash in the code instead of the password.
yea this is important..
Offline
RedRocker227 wrote:
shouldn't
Code:
(!($enteredPass == $password))be
Code:
($enteredPass!=$password)that's what i always thought
Yep. I didn't notice that. Using != is shorter.
Offline
You could do this in htaccess.
Offline
scimonster wrote:
RedRocker227 wrote:
shouldn't
Code:
(!($enteredPass == $password))be
Code:
($enteredPass!=$password)that's what i always thought
Yep. I didn't notice that. Using != is shorter.
I didn't notice that either. It only took a few minutes to make.
scimonster wrote:
If you want to make it so that people can't find the password by looking at the file source (say they get on your computer), so hash the input, and put the hash in the code instead of the password.
I didn't really take much notice of that. You can still crack hashed passwords, but it is better than storing the password as plain text.
EDIT: I just realised that if someone has access to your computer, there is probably no point in hashing the password if someone can just open the file and see the contents of the file...
Last edited by nathanprocks (2013-01-22 19:47:17)
Offline
I think a better idea would be to save whether the user is authenticated in session data, so they only have to enter the password once.
That idea becomes even more useful when implementing something like this across an entire site (like Scratch 2.0).
Offline
jvvg wrote:
I think a better idea would be to save whether the user is authenticated in session data, so they only have to enter the password once.
That idea becomes even more useful when implementing something like this across an entire site (like Scratch 2.0).
Lol. I only made this because my friend wanted a personal page on his website... I guess it could be useful for some things. I already said "Feel free to change this any way you want!", so if you used this code yourself, maybe if your too lazy to write one yourself or something, you could modify it to do that.
Offline
Cool script. Innovative use of the die() function, though I probably would have used if else logic to display content.
Offline
jji7skyline wrote:
Cool script. Innovative use of the die() function, though I probably would have used if else logic to display content.
Thanks!. Die() was the first thing that came into my head when I made this lol.
Offline