tangar Posted October 3, 2017 Share Posted October 3, 2017 (edited) $visitCounter = 0; if (isset($_COOKIE['visitCounter'])) { $visitCounter = $_COOKIE['visitCounter']; $visitCounter++; } $lastVisit = ''; if (isset($_COOKIE['lastVisit'])) $lastVisit = date('d-m-Y H:i:s', $_COOKIE['lastVisit']); if (date('d-m-Y', $_COOKIE['lastVisit']) != date('d-m-Y')) { setcookie("visitCounter", $visitCounter, 0x7FFFFFFF); setcookie("lastVisit", time(), 0x7FFFFFFF); } output: if ($visitCounter == 1) echo 'Welcome, new friend!'; else echo 'This is your '.$visitCounter.' visit. Your last visit was at '.$lastVisit.'. Nice to see you again!'; If I got: error_reporting(E_ALL); ini_set('display_errors', '1'); I got errors: Notice: Undefined index: lastVisit in C:\OpenServer\domains\localhost\php\cookie.inc.php on line 10Warning: Cannot modify header information - headers already sent by (output started at C:\OpenServer\domains\localhost\php\cookie.inc.php:10) inC:\OpenServer\domains\localhost\php\cookie.inc.php on line 11Warning: Cannot modify header information - headers already sent by (output started at C:\OpenServer\domains\localhost\php\cookie.inc.php:10) inC:\OpenServer\domains\localhost\php\cookie.inc.php on line 12This is your 0 visit. Your last visit was at . Nice to see you again! If I delete error reporting - it works! Please help - how to make this script work with error reporting? Thanks! Edited October 3, 2017 by tangar Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/ Share on other sites More sharing options...
requinix Posted October 3, 2017 Share Posted October 3, 2017 Learn to use proper code indentation. $visitCounter = 0; if (isset($_COOKIE['visitCounter'])) { $visitCounter = $_COOKIE['visitCounter']; $visitCounter++; } $lastVisit = ''; if (isset($_COOKIE['lastVisit'])) $lastVisit = date('d-m-Y H:i:s', $_COOKIE['lastVisit']); if (date('d-m-Y', $_COOKIE['lastVisit']) != date('d-m-Y')) { setcookie("visitCounter", $visitCounter, 0x7FFFFFFF); setcookie("lastVisit", time(), 0x7FFFFFFF); } 1 Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/#findComment-1552274 Share on other sites More sharing options...
tangar Posted October 3, 2017 Author Share Posted October 3, 2017 (edited) Thanks, but I have proper indentation - when I pasted it at forum, it didn't save. Donno why. I copy again: $visitCounter = 0; if (isset($_COOKIE['visitCounter'])) { $visitCounter = $_COOKIE['visitCounter']; $visitCounter++; } $lastVisit = ''; if (isset($_COOKIE['lastVisit'])) $lastVisit = date('d-m-Y H:i:s', $_COOKIE['lastVisit']); if (date('d-m-Y', $_COOKIE['lastVisit']) != date('d-m-Y')) { setcookie("visitCounter", $visitCounter, 0x7FFFFFFF); setcookie("lastVisit", time(), 0x7FFFFFFF); } Problem is still there, I got errors; without error reporting it works. Please help! Edited October 3, 2017 by tangar Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/#findComment-1552275 Share on other sites More sharing options...
requinix Posted October 3, 2017 Share Posted October 3, 2017 Thanks, but I have proper indentationNo, you don't. Look more closely. if (isset($_COOKIE['lastVisit'])) $lastVisit = date('d-m-Y H:i:s', $_COOKIE['lastVisit']);Should the next line be indented or not? Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/#findComment-1552278 Share on other sites More sharing options...
cyberRobot Posted October 3, 2017 Share Posted October 3, 2017 Perhaps the manual could shed more light on the situation. Note the difference between using an if statement with curly braces and without. http://php.net/manual/en/control-structures.if.php Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/#findComment-1552285 Share on other sites More sharing options...
tangar Posted October 3, 2017 Author Share Posted October 3, 2017 (edited) requinix, thank you. I got some mistakes with style cause I changed this code a lot to make it work, but failed... The problem there - not intendation; although next time I'll try to format code more carefully before posting somewhere. cyberRobot, thanks, but could you be more precise? I know the difference for 'if' with {} and without. I tried several ways fix my code, but it doesn't work. I wrote another version of this program and it works. The question - how to make this particular code to work and understand it works such weird - to solve this riddle, this mistery. I'm rookie, learning PHP from books and it's important to understand such details. Please, help! Problem still unsolved. Edited October 3, 2017 by tangar Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/#findComment-1552291 Share on other sites More sharing options...
Solution requinix Posted October 3, 2017 Solution Share Posted October 3, 2017 Okay, so since we've already explained the thing about if and {}s and indentation then I guess your problem is not understanding the error message? Maybe you didn't know what the isset was doing in the first place? $_COOKIE['lastVisit']If "lastVisit" is not a key in the $_COOKIE array then PHP will give you a warning. Happens any time you try to use it - except in a couple situations, like an isset(). So in this code if (date('d-m-Y', $_COOKIE['lastVisit']) != date('d-m-Y')) {if the "lastVisit" doesn't exist then PHP will give you a warning. You are getting the warning so that means it does not exist. You have to fix your code so that you don't try to use the lastVisit value in the cookie if it does not exist. Much like in the rest of the code. Most likely the problem is that you do not, in fact, understand the thing about if and {}s and indentation, because if you did then I would hope that you would realize the bug in your code and could fix it. We're talking literally adding a { and a } each in one place, but for you to do it right you have to (ahem) learn the thing about if and {}s and indentation. 1 Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/#findComment-1552297 Share on other sites More sharing options...
tangar Posted October 3, 2017 Author Share Posted October 3, 2017 Big thanks! After hour or so I finally fixed it: $visitCounter = 0; if (isset($_COOKIE['visitCounter'])) { $visitCounter = $_COOKIE['visitCounter']; ++$visitCounter; } $lastVisit = ''; if (isset($_COOKIE['lastVisit'])) { $lastVisit = date('d-m-Y H:i:s', $_COOKIE['lastVisit']); if (date('d-m-Y', $_COOKIE['lastVisit']) != date('d-m-Y')) { setcookie("visitCounter", $visitCounter); setcookie("lastVisit", time()); } } else { setcookie("lastVisit", time()); setcookie("visitCounter", $visitCounter); } Is it possible to make it better? I feel that my solution is quite crude hehe :E~~ Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/#findComment-1552301 Share on other sites More sharing options...
requinix Posted October 4, 2017 Share Posted October 4, 2017 There's nothing particularly wrong with it so as long as you understand what it's doing then that's fine. 1 Quote Link to comment https://forums.phpfreaks.com/topic/305188-cookie-errors-with-error-reporting-it-fails-without-works/#findComment-1552310 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.