PDA

View Full Version : Protected access to a page



techsol
03-27-2006, 07:32 PM
Is there any javascript code available that will allow a page to display only when there is additional text added to the url? I tried using a password protect code but it did not work.

Twey
03-27-2006, 08:00 PM
<script type="text/javascript">
if(window.location.href.indexOf("?redcrestedwarbler") == -1 && window.location.href.indexOf("&redcrestedwarbler") == -1) window.location.replace("http://www.google.com/");
</script>Note that it doesn't actually offer any additional security at all.

jscheuer1
03-27-2006, 08:36 PM
redcrestedwarbler?

Twey
03-27-2006, 08:57 PM
I think it's a rare bird. I just scraped it off the wall. :p

jscheuer1
03-27-2006, 09:11 PM
I guess my question was - Won't that be almost as good as hard coded link to Google?

Twey
03-27-2006, 09:50 PM
It's one line of Javascript... hardly "hard-coded" :)

<script type="text/javascript">
var keyword = "redcrestedwarbler", // Keyword
badurl = "http://www.google.com/"; // Page to go to on failure

if(window.location.href.indexOf("?" + keyword) == -1 && window.location.href.indexOf("&" + keyword) == -1) window.location.replace(badurl);
</script>techsol: Change the bits in red, if you didn't figure that out already :)

jscheuer1
03-27-2006, 10:08 PM
I was thinking more along the lines of testing for any appended characters via a regex. Mike and you are probably better at this than I but isn't there a token for any letter and one for any number? Just check against a 'one or more occurrences' of either and you've got it.

Twey
03-27-2006, 11:10 PM
I was thinking more along the lines of testing for any appended characters via a regex.I sort of got the impression the OP wanted a specific string.
isn't there a token for any letter and one for any number?\d for a digit, or [a-zA-Z] for a letter. But I don't think that's what was wanted here.

techsol
03-28-2006, 02:35 AM
I put the code into my page and replaced the items in red. I tried to get to the correct page by putting this url in my browser: http://www.mysite.com?keyword="redcrestedwarbler" where www.mysite.com is the protected page on my site and "redcrestedwarbler" would be the keyword but I could not get it to work.

What am I doing wrong? I am not javascript savvy…thanks

techsol
03-28-2006, 02:41 AM
I finally figured it out. You have to add ?keyword to the end of the page url. Is there any way to write the code such that the keyword would disappear from the URL when the correct page is selected. When I add this to the end of my page url it goes to the right page but the ?keyword appears in the URl.

Twey
03-28-2006, 04:51 AM
Is there any way to write the code such that the keyword would disappear from the URL when the correct page is selected.No. This would involve reloading the page, which would then send the user to Google.

djr33
03-28-2006, 05:00 AM
So.. you load the page and it goes away? Make a refresh/reload thing. But that defeats the point of it being protected. this security is a joke to anyone who WANTS to get around it, and confusing to people who just want to view your stuff.
I mean... JS just won't do anything. It also can be turned off, so it won't help you realisticaly anyway.

Twey
03-28-2006, 05:11 AM
As I've already pointed out.

djr33
03-28-2006, 06:07 AM
Yeah. It's true. I missed this page.... your post here, and you already said that you can just turn JS off. Anyway, next... :)