PDA

View Full Version : Retrive value from cookie



bhavesh
09-19-2006, 10:23 AM
Hello,

I am using <input type="password" name="txtPwd">

How can I retrive value in txtPwd from cookie on page_load.

Thanks

mwinter
09-19-2006, 11:26 AM
You wouldn't because you wouldn't store a password in a cookie.

If you are implementing a log in system, a user's password should be stored, hashed, on the server.

Mike

codeexploiter
09-19-2006, 12:34 PM
Wrong method for handling a password, if you keep a password in a cookie any time it got erased or intercepted in some way, don't do that. MWinter is 100% correct.


ASP

In ASP The "Response.Cookies" command is used to create cookies

The "Request.Cookies" command is used to retrieve a cookie value.

Writing a cookie, cookie name is user and cookie value is codeexploiter

<%
Response.Cookies("user")="codeexploiter"
%>

Reading a cookie


<%
username=Request.Cookies("user")
response.write("user name is " & username)
%>

Cookie with keys: creation

CODE]Response.Cookies("user")("fname")="Code"
Response.Cookies("user")("lname")="Exploiter"
Response.Cookies("user")("country")="India"
Response.Cookies("user")("age")="25"[/CODE]

Cookie with keys: reading


Response.write Request.cookies("user")("firstname")
Response.write Request.cookies("user")("lname")
Response.write Request.cookies("user")("country")
Response.write Request.cookies("user")("age")



If you want to learn how to set cookie using PHP then check the following

The setcookie() function is used to set a cookie.

Note: The setcookie() function must appear BEFORE the <html> tag.

Syntax
setcookie(name, value, expire, path, domain);




<?php
setcookie("user", "codeexploiter", time()+3600);
?>


Retrieve a Cookie Value

The PHP $_COOKIE variable is used to retrieve a cookie value.

In the example below, we retrieve the value of the cookie named "user" and display it on a page:


<?php// Print a cookieecho $_COOKIE["user"];// A way to view all cookies
print_r($_COOKIE);
?>

Here we check whether a cookies exist/has a value or not



<?php
if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!<br />";
else
echo "Welcome guest!<br />";
?>

If you want more details about this topic then refer PHP manual