dongshi1606 2013-01-04 00:02
浏览 28
已采纳

通过Javascript或PHP删除cookie

I want to provide a javascript snippet code (kinda like google analytics) to my clients which will use the code on their website and track visit duration on their website. I want to use cookies for this.

My only question is what's the best way to implement? A few options I have in mind:

  1. Should I set cookies from PHP (which is going to be sitting on my domain) and will that be considered as 3rd party cookie?
  2. Or do I have to set a cookie from Javascript since it will be client side loaded from my site?
  3. Or would be the best way to set cookies with javascript and access them with PHP?
  • 写回答

1条回答 默认 最新

  • dongshan4518 2013-01-04 00:22
    关注

    If you set the cookies in JavaScript the domain of the cookie is going to be the website's domain and not your domain. So that information is not available when doing something like.

    <script src="http://example.com/tracking-js.php"></script>
    

    With other words, that won't work.

    Instead you would need to send the clients information to your webservice using AJAX or (how google does it) by loading a blank images.

    There are a lot of parameters that are available in JavaScript, but not send to the server (not in $_SERVER). Google adds that information as GET parameters.

    However, if you don't really need that data, the easiest ways is to include your script as image on the client's website.

    <img src="http://example.com/tracking.php" />
    

    $_SERVER['HTTP_REFERRER'] will be the URL of the page where the script is included. Be sure to set the Content-Type to 'image/gif' and output a blank 1px gif.

    When using AJAX, please be aware that you'll need to use CORS, which means setting the Access-Control-Allow-Origin response header.

    Note that using AJAX without jQuery can be a pain. Since you don't care about the response, creating a tracking image (using javascript) is probably your best option. Read this article on the basics how to implement that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?