Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Aug 2010
    Posts
    74
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Pass username to different page using....Javascript???

    I'm working on a site with someone else and they would like the username to be entered in a form on a page, then passed to a second page that says 'Welcome [username]'. I am aware of how to do this using var prompts on the SAME page, but what about different pages. I'd also like to know how secure this is? The name is being used in a page (i.e. login.php) and passed to another page (i.e. welcome.html). The person has requested Javascript. How could this be done? If there is a better way to do so, what is it?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    And what part did you not like about the answers you got in this thread:
    Displaying Username
    ???

    If you *ONLY* need the username on the very next page, and not on any subsequent pages, then there is another way to do it. But if you even think you will want it on more than just that very next page then cookies are *the* way to go.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Example, for next page only:

    *** page1.html ****
    Code:
    <html>
    <body>
    <form action="page2.html">
    Please enter your name: <input name="user"/>
    <br/>
    Then <input type="submit" value="click here" />
    </form>
    </body>
    </html>
    **** page2.html ****
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var pairs = location.search.substring(1).split( "&" );
    var name = "anonymous";
    for ( var p = 0; p < pairs.length; ++p )
    {
        var pair = pairs[p].split("=");
        if ( pair[0] == "user" ) name = unescape( pair[1].replace(/\+/g," ") );
    }
    </script>
    <body>
    <h1>BIG WELCOME</h1>
    <br/>
    <h2>To our friend, 
    <script type="text/javascript">document.write(name);</script>
    </h2>
    <br/>
    Hope you enjoy this site.
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    dj262501 (08-21-2010)

  • #4
    New Coder
    Join Date
    Aug 2010
    Posts
    74
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Just the username. No way I'd want to pass anything else to the second page.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    I didn't say "anything else". I said to pages *beyond* the second page. In other words, on the 17th page you wanted to be able to use the person's name, again. *THEN* you need to use cookies.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Surely the weakness of this is that even if the username is invalid and the login is not successful, then the user is still transferred to page 2.

  • #7
    New Coder
    Join Date
    Aug 2010
    Posts
    74
    Thanks
    12
    Thanked 0 Times in 0 Posts
    So then cookies would be the safest way to go to ensure that the user login info is valid and pass the info to the second page..which is what I've been told from the beginning. I haven't had any experience with cookies. I will use the internet to research, but is there a place where I can get a good example that applies to my situation?

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    You shoud still validate the username/password server-side, before redirecting the user to page2.
    Cookies have been covered a zillion times in this forum. You are wanting a session cookie, which expires when the session ends.

    One more time .....

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    function setCookie(isName,isValue1, isValue2, isValue3) {
    document.cookie = isName + "=" + isValue1 + "/" + isValue2 + "/" + isValue3 + ";expires="  + "path=/";
    }
    
    function getCookie(isName){
    cookieStr = document.cookie;
    startSlice = cookieStr.indexOf(isName+"=");
    if (startSlice == -1) {return false}
    endSlice = cookieStr.indexOf(";",startSlice+1);
    if (endSlice == -1){endSlice = cookieStr.length}
    isData = cookieStr.substring(startSlice,endSlice)
    isValue = isData.substring(isData.indexOf("=")+1,isData.length);
    return isValue;
    }
    
    function dispCookie(isName) {
    nValue = getCookie(isName);
    if (nValue) {
    var list = nValue.split("/");
    //alert (list);  // for testing
    if (list[0]||list[1]||list[2]) {
    alert ("A field was filled in");  // so capture the values
    }
    }
    else {
    alert ("The cookie was not found");
    }
    }
    
    </script>
    
    </head>
    
    <body>
    <input type = "button" value = 'Set Cookie' onclick = "setCookie('myCookie','x','y','z')">
    <br><br>
    <input type = "button" value = 'Read Cookie' onclick = "dispCookie('myCookie')">
    
    </body>
    </html>
    This will pass three variables, x, y and z but you are wanting to pass only one.

    Obviously you set the cookie on page1, and get and display the cookie value(s) on page2.
    Last edited by Philip M; 08-21-2010 at 07:40 PM.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Quote Originally Posted by dj262501 View Post
    So then cookies would be the safest way to go to ensure that the user login info is valid and pass the info to the second page..
    ABSOLUTELY NOT!

    Cookies add ABSOLUTELY NO SAFETY whatsoever to the process.

    As Philip keeps telling you, *ANY* login system that is *NOT* done via server-side coding (PHP/ASP/ASP.NET/JSP/CGI/et al.) IS WORTHLESS!

    The cookies *ONLY* add "persistence" to the mix, allowing you to show the remembered data for so long as the cookie exists. By default, a cookie persists until the user closes his/her browser. But you *can* ask the user's browser to persist a cookie for days or weeks or years. (But the user can *also* quite easily set up his/her browser to disable cookies!)

    But do *NOT* mistake "persistence" for "safety" in any way, shape, or form.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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