Anthop Posted September 21, 2006 Share Posted September 21, 2006 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 https://forums.phpfreaks.com/topic/21600-cant-set-a-cookie-not-a-header-problem/ Share on other sites More sharing options...
corbin Posted September 22, 2006 Share Posted September 22, 2006 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 Link to comment https://forums.phpfreaks.com/topic/21600-cant-set-a-cookie-not-a-header-problem/#findComment-96410 Share on other sites More sharing options...
Anthop Posted September 22, 2006 Author Share Posted September 22, 2006 Nope. It didn't make a difference -_-;. Link to comment https://forums.phpfreaks.com/topic/21600-cant-set-a-cookie-not-a-header-problem/#findComment-96417 Share on other sites More sharing options...
btherl Posted September 22, 2006 Share Posted September 22, 2006 A shot in the dark here.. try giving your cookie a name with no spaces in it. Link to comment https://forums.phpfreaks.com/topic/21600-cant-set-a-cookie-not-a-header-problem/#findComment-96522 Share on other sites More sharing options...
Anthop Posted September 22, 2006 Author Share Posted September 22, 2006 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. Link to comment https://forums.phpfreaks.com/topic/21600-cant-set-a-cookie-not-a-header-problem/#findComment-96944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.