Results 1 to 8 of 8

Thread: password script and cookies

  1. #1
    Join Date
    Dec 2004
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default password script and cookies

    Hey guys,
    I got this simple password script for a page.
    Code:
    <script language="JavaScript">
    <!--
    var password = prompt("What's the password","Type password");
    
    if (password == "yes")
    {
    alert("Welcome");
    } else {
    alert("Sorry wrong password");
    javascript:history.back()
    }
    -->
    </script>
    I want this code to create a session cookie until user leaves page or exits his browser, I want to eliminate the need to enter the password every time he goes to that page. Can you help?

    viktor
    Last edited by viktor; 11-18-2007 at 04:35 AM. Reason: spelling

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Not sure about JS... With PHP you can do a session variable.

    Also, with JS you have to declair the password in the coding:

    Code:
    if (password == "yes")
    This will show up when you view source., and if a browser turns off JS then it won't work at all.

    I would go with PHP at a minimum, if you want to protect pages/directories then you really should use .htaccess
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Dec 2004
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, I know. On my site I do use more advanced password protection.
    However, this code is used on a site where I'm the user and I can only insert HTML inside body tags with no forms. This was the easiest way to somewhat protect content with prompt box, instead of form tags. The content is not that sensitive but I wanted to put password on it.

    viktor

  4. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Well, if session cookies in JS are what you want.. then you shall have

    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Javascript used in this manner is virtually no protection. A would be attacker could easily view the source code to get the password.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    You can always encode the password into HEX or something, but that won't stop anyone really, just deter them a little.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  7. #7
    Join Date
    Dec 2004
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I know it's very limited protection but content behind password is not sensitive or personal in any nature. All it is, discounted products for specific people. Just showing password box will let visitors know it's not for them.

    Ok, thanks for the tutorial Blizzard, but I need help implementing it. I've never used cookies.

    Thanks for the help,
    viktor

  8. #8
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Read, read read. Honestly. I don't mind helping you at all, but part of this is you learning too. I could go step by step and show you what to do, but then if/when the code stops working right or it gets corrupt or out dated, etc. you need to know how to fix it.

    If you Google "JS cookies" you will find a wealth of information and implementation, and I suggest you read as much as you can.

    Once you have started and get stuck, then come back and ask for help with the code you have written.

    It's not all that hard, honestly, and you will be all the better prepared for having done so
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •