未能在PHP中设置cookie [英] Failing to set cookies in PHP

查看:179
本文介绍了未能在PHP中设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个PHP脚本,它从网站上获取有关访问者IP和浏览器的信息,并将其存储在我的数据库中的表中。此脚本包含在标题中,每次加载每个页面时都会运行,google analytics type thing。 survey.php

I have written a PHP script which takes information about visitors IP and browser from a website and stores it in a table in my database. This script is included in the header and runs every time each page is loaded, google analytics type thing. survey.php

我希望此代码仅针对网站的每个新访问者运行,以便我只记录每个新访问者。

I want this code to run only for each new visitor of the website, so that I only log each new visitor.

我想这是通过设置一个cookie来完成的,所以我写了这个其他代码来设置一个cookie,然后只包括 survey.php 没有cookie。
我已经使用 isset 中的函数来运行if语句,如果它不存在但是它没有设置cookie并且正在运行脚本在每次加载时。

I guess this is done by setting a cookie so I wrote this other code to set a cookie and then include the survey.php only when there is no cookie. I've used the ! function in isset to run the if statement if it isn't present but it is not setting the cookie and is running the script upon each load.

根据中的说法手册我看不出哪里出错了。

Based on what it says in the manual I can't see where I am going wrong.

<?php 
if(!isset($_COOKIE['Name'])){
    setcookie("Name");
    include ('survey.php');
}
?>

编辑:我添加了一个值,测试了多个用户,脚本仍然运行每个负载。不明白为什么。

推荐答案

已解决

我将代码作为第一段代码而不是第一行代码。

I was including the code as the first piece of code but not on the first line.

设置cookie的代码必须在第1行的所有内容之前开始。

A code to set cookies has to begin before everything, on line 1.

发布此代码以便其他人可以效益。

Posting this so others can benefit.

这篇关于未能在PHP中设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆