Jump to content

Archived

This topic is now archived and is closed to further replies.

Anthop

Can't Set a Cookie (Not a header problem <_<)

Recommended Posts

Though I've done a lot of desktop development, I'm new to PHP and to web-development in general, so please treat me kindly :D.  In any case, I have a login script that won't set a cookie.  Attempting to set the cookie returns a true on success (see [b]$success[/b]), so I don't think it's a header issue.  However, checking [b]isset($_COOKIE['User ID'])[/b] and just refreshing the page to see if I'm still logged on shows that there is no cookie to be found.  (I also checked my brower's list of cookies to no avail -_-;.)

In any case, here is my code.  I've deleted the non-cookie parts for quicker browsing (My code isn't really this sparse XD).  The cookie isn't set whether I run index.php or login.php directly.


This is index.php.
[code]<?php ob_start() ?>
<html>
<head>
</head>
<body>
<?php // Here is a form for other input.?>
<br /><br />
<?php
include 'login.php';
ob_flush();
?>
</body>
</html>
[/code]



This is login.php.
[code]<?php
// If user is logging out, remove cookie.
if ($_POST['logout']) {
setcookie("User ID", "", mktime(12,0,0,1,1,1990));
}
// If information submitted, and no cookie is found, check login information and set cookie.
if ($_POST['submitted'] && !isset($_COOKIE['User ID'])) {
// Connect to the User database using a public account.

// Search to see if user name matches, and fetch password.

// If password matches that given, set a cookie which expires in 12 hours.
if($password == $row['Password']) {
$success = setcookie('User ID', $row['User ID'], time()+43200, '/', '.light-within.org', 0);
$new_session = 1;
}
}
?>
<html>
<head>
</head>
<body>
<?php
// If not already logged in,
if (!$_POST['submitted'] && !isset($_COOKIE['User ID'])) {

// Display the Login Form (the target is PHP_SELF)

// Otherwise, display welcome message.
}else{
// If this is a new login, display welcome message.
if($new_session) {

// If there is a cookie, retrieve user information.
} else if (isset($_COOKIE['User ID'])) {

// Otherwise, return invalid login message.
} else {
echo("<br><p>Screen Name and Password combination not valid.</p>");
}

// This checks to see if there is a cookie installed.  Success returns true,  but isset returns false.
if(!$success || !isset($_COOKIE['User ID'])) {
echo('NO COOKIE SET!! <br>');
}
// Close database connection.
}
?>
</body>
</html>
[/code]

Share this post


Link to post
Share on other sites
Change
$success = setcookie('User ID', $row['User ID'], time()+43200, '/', '.light-within.org', 0);
to
setcookie('User ID', $row['User ID'], time()+43200, '/', '.light-within.org', 0);

and see if it works

Share this post


Link to post
Share on other sites
A shot in the dark here.. try giving your cookie a name with no spaces in it.

Share this post


Link to post
Share on other sites
O_O.... It worked!  Wow.  So I've learned that you have to set all the values of a cookie and not use any whitespace to make a cookie work in Firefox <_<;;.

Anyway, thanks for your help, btherl and corbin :D.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.