-1

Here is my script:

if ($_COOKIE['aktifko'] != 'XXX')
{

$aktifko= 'XXX';
setcookie('aktifko',$aktifko,time() + (3600)); // aktif 1 jam
echo("
<script src=\"myjavascript.js\"></script>
");
}

I wanto run this script only first time access, after visitor open it 2nd time, my js file not active anymore untill 1hr. it's running already but on the first time access show error:

Notice: Undefined index: aktifko in /scriptdirectory/script.php on line 11111

How to run it without error?

1 Answers1

2

You should first check if your element exists in your array:

if (array_key_exists('aktifko',$_COOKIE) AND $_COOKIE['aktifko'] != 'XXX')

With your current code, you are checking the value of this element in the cookie array. But if it's the first time for the user, this element won't exist in the array. So you have to check if the key already exists before checking its value.