Log in

View Full Version : How to set a cookie



Xiwl
05-05-2007, 02:44 PM
U have this in an echo.



<form action=\"bank.php\" method=\"post\" autocomplete=\"off\">
<table><tbody><tr><td>Username:</td><td>
<input type=\"text\" maxlength=\"12\" value=\"\" name=\"user\" size=\"20\"></td></tr><tr><td>
Password:</td><td>
<input type=\"password\" maxlength=\"20\" value=\"\" name=\"pass\" size=\"20\"></td></tr><tr><td>&nbsp;</td><td align=\"center\">
<input style=\"background: rgb(77, 66, 46) url('images/brownback.jpg') 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: white\" type=\"submit\" class=\"b\" value=\"Login to Admininistration Panel\" size=\"20\"></td></tr></tbody></table>
<div>
</div>
</form>


How to i set so that when they either click the submit button it sets a cookie with the user + pass in, or that on the next page it sets a cookie with the user + pass in it from post data, and how do i retrive the information from a cookie sort of thing so say if i typed Ryan in the username field and password in the pass field, then it sets the cookie with Ryan + password in, how do i make a page where it shows the username + pass from the cookie.

thnx

Invent
05-05-2007, 02:51 PM
Say the set variable for the username is $username and the set variable for the password is $password then all you do is:



setCookie("user", $password, time()+3600); // Lasts for an hour
setCookie("pass", $password, time()+3600); // Lasts for an hour also


To check for the cookie you just do



if(isset($_COOKIE['user']) && isset($_COOKIE['password'])) {
}


Ofcourse you then have to check the cookie over information in a database to make sure the cookie isnt fake :]

Xiwl
05-05-2007, 02:55 PM
that kinda explained it all dont know how to say thnx enough ^^

one more thing though, how would i display the username+password from the cookie, would it be..



echo("$_COOKIE['user'] is the username and $_COOKIE['pass'] is the password");


Is that right?

Invent
05-05-2007, 03:04 PM
echo("". $_COOKIE["user"] ." is the username and ". $_COOKIE["pass"] ." is the password");
Is the best way to do it :)

Xiwl
05-05-2007, 03:11 PM
ok thankzzz

Want to hide these adverts? Register an account for free!