如何为新闻通讯生成退订链接? [英] how to generate unsubscribe link for newsletter?

查看:170
本文介绍了如何为新闻通讯生成退订链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用php编写新闻通讯.但我有一个问题:我如何生成退订代码.实际上,我想要每个订户的唯一代码.例如在" http://net.tutsplus.com/"中,您会看到类似以下内容:' http://tutsplus.us1.list-manage. com/profile?u = 0154weg635df2fdwied2541cbed& id = c5652sdfre7& e = 8758563dfgde ". 另一个问题是该代码应该保存在数据库中还是不保存?(因为我认为,如果每个人的代码都是唯一的,则不必每次发送新闻通讯时都生成该代码).有什么主意吗?

I want to write a newsletter with php. but i have a question: how can i generate a code for unsubscribe. In fact i want a unique code for each subscriber. for example in 'http://net.tutsplus.com/' you can see something like this:'http://tutsplus.us1.list-manage.com/profile?u=0154weg635df2fdwied2541cbed&id=c5652sdfre7&e=8758563dfgde'. and another question is that this code should be saved in data base or no?(because i think if it's unique for each person, it's not necessary to generate every time whiling send newsletters). any idea?

推荐答案

生成用户ID的哈希值+一些秘密字符串,将ID和哈希值放入链接,并使用将取消订阅用户的脚本来提供该哈希值,在验证了哈希之后.

Generate a hash of the user id + some secret string, put the id and the hash to the link, and serve it using a script which would unsubscribe the user, after verifying the hash.

哈希不必在数据库中,只需在运行时进行计算即可.

The hash doesn't have to be in a database, just compute it on the fly.

创建退订链接的脚本:

<?
$link = "unsubscribe.php?id=$user['id']&validation_hash=".md5($user['id'].$SECRET_STRING)
<a href="<?=$link?>">Unsubscribe</a>

脚本处理退订链接:

function unsubscribe() {

    $expected = md5( $user['id'] . $SECRET_STRING );

    if( $_GET['validation_hash'] != $expected )
        throw new Exception("Validation failed.");

    sql("UPDATE users SET wants_newsletter = FALSE WHERE id = " . escape($_GET['id']);
}

这不是有史以来最安全的事情,但是足够好.

It's not the most secure thing ever, but good enough.

这篇关于如何为新闻通讯生成退订链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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