Jump to content

Cookie errors: with error reporting it fails, without - works


tangar

Recommended Posts

$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 10

Warning: 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 11

Warning: 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 12
This 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!

 

Link to comment
Share on other sites

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);
}
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Big thanks! :D 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~~ 

Link to comment
Share on other sites

Archived

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

×
×
  • 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.