简体   繁体   English

如何使用 PHP 获取 UTM?

[英]How to get the UTM using PHP?

I'm new with UTM (Urchin tracking module) and I want to get the UTM parameters where the user clicked the link from (eg facebook, google, twitter) and how many time the user clicked the link with same utm source and save it to the mysql database.我是 UTM(Urchin 跟踪模块)的新手,我想获取用户单击链接的 UTM 参数(例如 facebook、google、twitter)以及用户单击具有相同 utm 源的链接的次数并保存它到mysql数据库。

I have a PHP snippet but the utm parameters didn't get and save in the database.我有一个 PHP 代码段,但 utm 参数没有获取并保存在数据库中。

How to fix this?如何解决这个问题?

if(isset($_SERVER['HTTP_REFERER'])){
 $utm_source = isset($_GET['utm_source']) ? $_GET['utm_source'] : '';
 $utm_medium = isset($_GET['utm_medium']) ? $_GET['utm_medium'] : '';
 $utm_campaign = isset($_GET['utm_campaign']) ? $_GET['utm_campaign'] : '';
 $utm_term = isset($_GET['utm_term']) ? $_GET['utm_term'] : '';

echo $_SERVER['HTTP_REFERER'];

$sql = "INSERT INTO utm_param(utm_source,utm_medium,utm_campaign)
        VALUES('".$conn->real_escape_string($utm_source)."',
               '".$conn->real_escape_string($utm_medium)."',
               '".$conn->real_escape_string($utm_campaign)."')";
}

utm_param db table structure utm_param db 表结构

描述 utm_param;

Before we worry about getting the value of the UTM attributes, you have a security issue that needs some attention.在我们担心获取 UTM 属性的值之前,您有一个需要注意的安全问题。 You are currently taking un-sanitised (unsafe) user input (via $_POST['var'] ) and putting it straight into a database query, which is an unsafe practice that allows SQL injection (eg a malicious user can send you a specially crafted string that gives them the ability to do whatever they like to your database).您目前正在获取未经消毒(不安全)的用户输入(通过$_POST['var'] )并将其直接放入数据库查询中,这是一种允许 SQL 注入的不安全做法(例如,恶意用户可以向您发送特别精心制作的字符串,使他们能够对您的数据库执行任何他们喜欢的操作)。

Please have a read of this Stack Overflow question on how to avoid SQL injection in PHP.请阅读有关如何避免 PHP 中的 SQL 注入的堆栈溢出问题

Typically, the standard utm parameters for tracking where a user comes from form part of the query string, for example:通常,用于跟踪用户来自何处的标准utm参数构成查询字符串的一部分,例如:

http://yoursite.com?utm_source=google&utm_campaign=campaign_name

Unless you are doing something non-standard, this will be a GET request to your server (as opposed to a POST) so your variables will be available via the $_GET superglobal.除非您正在做一些非标准的事情,否则这将是对您的服务器的 GET 请求(而不是 POST),因此您的变量将通过 $_GET 超全局变量可用。 For example $_GET['utm_source'] and $_GET['utm_campaign']例如$_GET['utm_source']$_GET['utm_campaign']

Looking at your table schema, you may also need to add auto_increment to your ID column, so that it will get a value automatically when a new row is added.查看您的表架构,您可能还需要将auto_increment添加到您的 ID 列,以便在添加新行时自动获取一个值。

ALTER TABLE `utm_param` 
CHANGE COLUMN `id` `id` INT(100) NOT NULL AUTO_INCREMENT;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM