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

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
tangar
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Sep 08, 2017 7:15 am

Tue Oct 03, 2017 9:01 am

Code: Select all

<?
$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);
}
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! WTF??

Please help - how to make this script work with error reporting?

Thanks!

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 524
Joined: Mon Feb 22, 2016 5:52 pm

Tue Oct 03, 2017 9:30 am

You have problems that you need to fix.
how to make this script work with error reporting?
Read what the error says, and alter your code accordingly:

The first error is caused by a fault in your program logic.

You haven't shown all of your code, however, if you read the lines indicated in the error message, you should be able to work it out.

User avatar
tangar
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Sep 08, 2017 7:15 am

Tue Oct 03, 2017 11:14 am

Thank you, hyper!

I fixed it:

Code: Select all

$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);
	}

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 524
Joined: Mon Feb 22, 2016 5:52 pm

Tue Oct 03, 2017 2:43 pm

Well done, and you're welcome, however there is more to learn:

I've re-formatted your script as I want you to notice something if you go through the script (the last visit bit):

Code: Select all

$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("lastVisit", time());
			setcookie("visitCounter", $visitCounter);
	}
} else {
	    setcookie("lastVisit", time());
	    setcookie("visitCounter", $visitCounter);
}
As a little hint, it is to do with redundant coding, i.e. something which isn't needed.

Post Reply