...

View Full Version : Pass username to different page using....Javascript???



dj262501
08-20-2010, 09:37 PM
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?

Old Pedant
08-20-2010, 09:51 PM
And what part did you not like about the answers you got in this thread:
http://www.codingforums.com/showthread.php?t=202561
???

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.

Old Pedant
08-20-2010, 10:01 PM
Example, for next page only:

*** page1.html ****


<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 ****


<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>

dj262501
08-21-2010, 12:15 AM
Just the username. No way I'd want to pass anything else to the second page.

Old Pedant
08-21-2010, 12:40 AM
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.

Philip M
08-21-2010, 08:05 AM
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.

dj262501
08-21-2010, 07:09 PM
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?

Philip M
08-21-2010, 07:26 PM
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. :p You are wanting a session cookie, which expires when the session ends.

One more time .....


<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.

Old Pedant
08-22-2010, 02:23 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum