Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setcookie

Status
Not open for further replies.

neonep

Programmer
Jun 26, 2003
34
Hello everyone,

This is my first venture with php and I am trying to simulate a login screen.
This is what I have.

<?
if ( isset($_POST["login"]))
{
setcookie ( "loggedIn", $_POST["username"]);
}
?>
<html>
<body>
<form name="addForm" action="<? echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="hidden" name="login">
Username: <input type="text" name="username" value="<? echo $_POST["username"]?>">
Password: <input type="password" name="passwd">
<a href="welcome.php">Click Here to LogIn"</a>
and so on

My problem is that once "Click here to Login" is clicked, welcome.php does come up but the cookie isn't set and I believe this is because the form hasn't been submitted. How do I make it so that the cookie is set and also it goes to the welcome page? Thanks.
 
Look at the header() function to redirect the browser:
Code:
header("Location: [URL unfurl="true"]http://www.wherever.com/welcome.html");[/URL]
 
also note that when setting cookies, they usually don't work if you use them too fast after setting them. i mean, if you set the cookie in the beginning of the code and then try to read it after it won't work, the same if you set the cookie and redirect to a page that will read the cookie immediatly after. this is personnal experience, i suggest you wait 2 secs or more between setting the cookie and reading it.

jamesp0tter,
mr.jamespotter@gmail.com

p.s.: sorry for my (sometimes) bad english :p
 
You need to refresh it, before you can view the cookie you set. Don't use the cookie in the same file. =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top