douyunjiaok300404 2013-11-29 03:32
浏览 25

如何删除cookie错误提示:PHP上的未定义索引[重复]

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?

</div>
  • 写回答

1条回答 默认 最新

  • duanhuanzhi6431 2013-11-29 03:36
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击