简体   繁体   English

使用PHP和MySQL数据库构建可跟踪链接

[英]Building trackable links using PHP and a MySQL DB

I have an email list and a personalized PDF associated with each email (a one-to-one relationship). 我有一个电子邮件列表和与每个电子邮件相关联的个性化PDF(一对一关系)。 I also have a server that hosts the PDFs. 我也有一个托管PDF的服务器。 I want to send each person their personalized PDF using a link to the file on my server. 我想使用服务器上文件的链接向每个人发送其个性化PDF。 When I send a person that link, I want to to track whether they have downloaded the file. 当我向某人发送该链接时,我想跟踪他们是否已下载文件。

This download.php script (full code below) was recommended to as I can include my links as http://yourdomain.com/download.php?email={!email} so that when they are clicked, the script will write to a MySQL database to track which email address clicked the link and then serve the file. 推荐使用这个download.php脚本 (下面的完整代码),因为我可以将链接包含为http://yourdomain.com/download.php?email={!email}以便单击它们时,脚本将写入一个MySQL数据库,以跟踪哪个电子邮件地址单击了链接,然后提供文件。

The download.php script download.php脚本

<?php

mysql_connect('localhost', 'username', 'password');
mysql_select_db('dbname');

$sql = "INSERT INTO downloads (email) VALUES ('" . mysql_real_escape_string($_GET['email']) . "')";
mysql_query($sql);

header("Content-type: application/pdf");
readfile("/path/to/thefile.pdf");

?>

My issue is that the last line of the code readfile("/path/to/thefile.pdf"); 我的问题是代码readfile("/path/to/thefile.pdf");的最后一行readfile("/path/to/thefile.pdf"); looks as if it needs to be written such that it contains the path to a single file, which will not work for my purposes. 看起来好像需要编写它,使其包含单个文件的路径,这对我而言不起作用。 Am I reading the code correctly and if so, is there a way to modify it for my purposes? 我是否可以正确阅读代码,如果可以,是否可以针对自己的目的对其进行修改?

I hope I understood your question correctly. 希望我能正确理解您的问题。 You have to paste the path to your PDF on the server. 您必须将路径粘贴到服务器上的PDF。 eg http://www.youdomain.com/pdfs/october.pdf 例如http://www.youdomain.com/pdfs/october.pdf

Avoid mysql functions and use PDO always. 避免使用mysql函数,并始终使用PDO。

This is how you do it in PDO 这就是您在PDO中的做法

$hostname = 'localhost';
$username = 'username';
$password = 'password';

$con = new PDO("mysql:host=$hostname;dbname=dbname", $username, $password);

$user_mail = $_GET['email'];

$sql = "INSERT INTO downloads (email) VALUES (:emailVal)";
$statement = $con_db->prepare($sql);
$statement->execute(array(':emailVal'=> $user_mail));

header("Content-type: application/pdf");
readfile("/path/to/thefile.pdf"); //paste to your PDF on your server

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

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