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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting and displaying cookies

Status
Not open for further replies.

dugen

Programmer
Jun 16, 2003
59
0
0
US
Hi,

I am trying to set a cookie equal to the value of a username text box on a login form.

Once the user logs in they have access to a bunch of admin pages. On the top of these admin pages i would like to simply display a welcome message which will display whatever value the cookie is set to.

For example if Eric logs in the welcome message will say "Welcome Eric."

Here is the code that i have:

Login Page:
<form action="page1.php" method=post>
username: <input name="txt_username" type="text" value="">
password: <input type="password" name="txt_password">
<input type= submit value="Submit" name="Submit">
</form>


Here is the php code i have on "page1.php"

<?php
setcookie("user", $_POST["txt_username"], time()+3600);
?>

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

When i first login nothing appears, but when i refresh the page it does display the cookie. Do i need to set the cookie on the previous page?

Thanks,
 
When using cookie's for authentication purposes I have used an if statement to check for the existence of the cookie and then to either display the form or to re direct the user to the next page.

For example on the login page:

Code:
<?php
// set before <html> tag
if ( isset ( $_COOKIE['txt_username'] ) ) {
    header ( 'Location: [URL unfurl="true"]http://servername/page1.php'[/URL] );
} else {
    // Show form
    $show_form = TRUE;
}
?>

<?php
//begin if show form
if ($show_form) {
?>

Form goes here ...

<?php
//end if show form
}
?>

Hope this helps.


Toby Heywood

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top