Jump to content

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


Go to solution Solved by requinix,

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!

 

Edited by tangar

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);
}
  • Like 1

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 by tangar

Thanks, but I have proper indentation

No, 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?

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 by tangar
  • Solution

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.

  • Like 1

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~~ 

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.