Jump to content

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


Anthop

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.