PDA

View Full Version : Password Protect a Page Script Help



isamarlow
02-28-2007, 09:05 PM
Hi - I have this login script but I would like to have the prompt "incorrect password" with buttons to retry or cancel and only upon cancel would I like it redirect to "jpass2.htm"

<SCRIPT language="JavaScript">
<!--hide
var password=prompt('Enter the password:','');
var mypassword="cool";
if (password==mypassword)
{
window.location="jex11.htm";
}
else
{
window.location="jpass2.htm";
}
//-->
</SCRIPT>

I'm new to javascript, sorry if this seems really simple. :eek:

pcbrainbuster
02-28-2007, 09:24 PM
Hello dude :),

Well heres probably what you want -

<script>
<!--
function startpass() {
var a = prompt("Enter the password:","Password here")
if (a=="cool") {
window.location="jex11.htm"}
else {
startpass2()}
}

function startpass2() {
var cd = confirm("Incorrect Password.\nDo you wish to try again \?")
if (cd==false) {window.location="jpass2.htm"}
else {startpass()}
}
//-->
</script>

Well report back and your welcome :)

pcbrainbuster
02-28-2007, 09:26 PM
Let's see anyone say that's invalid !!! :mad:

Twey
02-28-2007, 09:27 PM
Huh, that's one heck of a lot of overkill.
<script type="text/javascript">
do {
if(prompt("Enter the password:") === "cool")
window.location.href = "jex11.htm";
} while(confirm("Wrong password. Try again?"));
window.location.href = "jpass2.htm";
</script>Note that this isn't at all secure, since anyone can look in your source and find the password, or even just find the URL and go there directly.
If you want a more secure client-side solution, see my script (http://www.twey.co.uk/?q=encpass), but your best bet will always be a server-side script.

/EDIT: Cross-posted, sorry pcbrainbuster.
Let's see anyone say that's invalid !!! It is: you're missing the type attribute on the <script> tag.

pcbrainbuster
02-28-2007, 09:27 PM
And oh yeah this is only gaurenteed to work on ie i have no idea for the others :)

pcbrainbuster
02-28-2007, 09:28 PM
Grrr twey is back !!!

pcbrainbuster
02-28-2007, 09:29 PM
And its not overkill !!!
Its just the way i script !!!+!

Blake
02-28-2007, 09:30 PM
Have you considered doing this server side (ie with php)? It's much more secure, as no one get get you password by looking at the source.

I could help you do that if you want.

jscheuer1
02-28-2007, 09:30 PM
Even posting three times to respond is overkill. :)

pcbrainbuster
02-28-2007, 09:31 PM
As your server-side script for a password you can use .htaccess

pcbrainbuster
02-28-2007, 09:32 PM
Don't make me come there to show you overKILL !!! (:))

Twey
02-28-2007, 09:36 PM
And its not overkill !!!I was actually referring to the original, but now you mention it yours is rather :) Redundant code is only useful when it improves readability.
As your server-side script for a password you can use .htaccessOnly on some webservers. Apache, Zeus and some other implement .htaccess.

pcbrainbuster
02-28-2007, 09:39 PM
LOL :) -

Do you really can't "read" mine at all ?
I can't read yours ! :) But if it actuall works then i will have to submit myself to you :) (i don't meen like ripping up my clothes for you i simply meant that i will have to admit that your script is better):)

Twey
02-28-2007, 09:57 PM
Do you really can't "read" mine at all ?I can read it, but that doesn't mean that someone who hasn't had a decade's experience with Javascript and a couple of years on this forum learning to pick through ugly code will be able to comprehend the eventual effect of two mutually recursive functions :)
Commented version of mine:
<script type="text/javascript">
do {
// Display a prompt, and check if the value the
// user has entered is "cool".
if(prompt("Enter the password:") === "cool")
// If so, redirect to jex11.htm. This will, of
// course, break the loop.
window.location.href = "jex11.htm";
// Otherwise, ask whether to try again, and if
// the answer is "OK," loop (from the "do"
// keyword).
} while(confirm("Wrong password. Try again?"));
// If the user presses "cancel," however, the
// loop will break, and the user will be redirected
// to jpass2.htm.
window.location.href = "jpass2.htm";
</script>

pcbrainbuster
02-28-2007, 10:24 PM
Grrr - was that supposed to be advise or evil(you hate me) :mad:

pcbrainbuster
02-28-2007, 10:39 PM
One thing though you can't really say that decade thing because i have not even had half a year of experience i am pretty new to this sort off...

Titan85
02-28-2007, 10:59 PM
One thing though you can't really say that decade thing because i have not even had half a year of experience i am pretty new to this sort off...I think that was his point... There is a nifty edit feature for all your posts, its good to use it.

-- On Topic --

I would recommend that you use PHP (as someone already said) to do password protection stuff because it is easier (in my opinion) and its more secure.

Twey
02-28-2007, 11:16 PM
Grrr - was that supposed to be advise or evil(you hate me)Erm... paranoia anyone? :p
I was saying that although I can read it, as, I would suspect, can most of the regulars here, that's no guarantee that someone without experience in this area, as the OP appears to be, will be able to do the same.

techno_race
02-28-2007, 11:58 PM
Grrr twey is back !!!


And its not overkill !!!
Its just the way i script !!!+!

Calm down!!!

Titan85
03-01-2007, 03:06 AM
pcbrainbusterStop() {
var stop = "stop pcbrainbuster";
alert(stop);
}That should work good

jscheuer1
03-01-2007, 05:50 AM
pcbrainbusterStop() {
var stop = "stop pcbrainbuster";
alert(stop);
}That should work good

I've just installed that on all my live pages! :p

pcbrainbuster
03-01-2007, 07:30 AM
:)
I just like typing down "!!!!" i was not angry (same with the earlier on :mad:) :)
And when i said twey is back i simply ment that he's outdone me again and thats pretty much it!!! (lol)

mburt
03-01-2007, 11:04 AM
I've just installed that on all my live pages!
Me too! :p

Titan85
03-01-2007, 12:01 PM
Me too! :p
It's supposed to password protect, not show annoying popups that do nothing whatsoever. Twey's script does work, there's no need for that one.It was a joke...

pcbrainbuster
03-01-2007, 06:00 PM
Muhahahahahahaha :mad: (evil laugh)