Hello Everyone,

Hope you're having a great weekend, especially in the Chicago land area where it's usually pretty chilly this time of the year and instead it's in the mid eighties.

I'm looking to make an unsubscribe page. I don't know if everything can be done automatically or not and I'm sure it will probably involve MySQL, so that is why I am writing.

Here's the steps I'd like to have happen:

  1. Users hit unsubscribe link in HTML email
  2. Users either enter email in text field on page or simply hit submit button under a text field prefilled with their email address
  3. Users emali is deleted from list

Not looking for "hand out". I don't want a fish: I want to learn how to fish. If I could led in the right direction to accomplish these tasks or a code snippet jump start that would be appreciated. Thanks for your time.

Dani AI

Generated

Good thread — ’s form + PHP shows the basic flow and ’s question about prefill is common. A few important, practical improvements to make the unsubscribe flow safe and reliable for production:

Use a one-click token (or a signed link) instead of embedding raw email in the URL, use parameterized queries (PDO/MySQLi) instead of deprecated mysql_* calls, and mark rows as unsubscribed with a timestamp rather than permanently deleting them (keeps audit/history). Always serve links over HTTPS and make tokens single‑use with an expiry.

Example: generate a token when sending mail and include it in the message (store token + user_id + expiry):

$token = bin2hex(random_bytes(16));
$stmt = $pdo->prepare('INSERT INTO unsubscribe_tokens (user_id, token, expires_at) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 30 DAY))');
$stmt->execute([$userId, $token]);
$link = '/unsubscribe.php?token=' . $token;

On the unsubscribe page validate the token and update the subscriber safely:

$stmt = $pdo->prepare('SELECT user_id FROM unsubscribe_tokens WHERE token = ? AND expires_at > NOW()');
$stmt->execute([$token]);
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $pdo->prepare('UPDATE subscribers SET subscribed = 0, unsubscribed_at = NOW() WHERE id = ?')->execute([$row['user_id']]);
    $pdo->prepare('DELETE FROM unsubscribe_tokens WHERE token = ?')->execute([$token]);
    echo 'You have been unsubscribed.';
} else {
    echo 'Invalid or expired link. Please enter your email to unsubscribe manually.';
}

Troubleshooting & tips: normalize emails (lowercase + trim), log unsubscribe source, provide a manual email-entry fallback, set reasonable token lifetime, and protect against tampering (HMAC or tokens). If you must prefill a form, fetch the address server-side from a validated token — do not trust a raw ?mail= value.

Recommended Answers

All 5 Replies

Member Avatar for Member #733618

Hello Reliable, what you are trying to do is very simple !

When you go to you page, , you have a form with a text box and a button like this:

<form action="unsubscribe.php" method="post">
    <input name="mail" type="text" size="50" />
    <input name="Submit" type="submit" value="Unsubscribe" />
</form>

In your unsubscribe.php, you $_POST mail and validate that is a valid mail and is not empty, then you do this, assume that your database call subscribers and a table call mails that has one column, mail . With unsubscribe.php you verify that is in the database (the mail) and delete it like this:

<?
$mail = $_POST['mail'];
$link = mysql_connect("localhost","root","123456");
mysql_select_db("subscribers",$link);
$query = mysql_query("SELECT * FROM mails WHERE mail='$mail'");
if(mysql_num_rows($query) == 1){
    mysql_query("DELETE FROM mails WHERE mail='$mail'");
    echo "You are not more subscribe !";
    echo "<meta http-equiv='refresh' content='3;url=index.php'>";
}else{
    echo "The mail you entered does not exists or has already been deleted !";
    echo "<meta http-equiv='refresh' content='3;url=index.php'>";
}
?>
commented: A junior poster on his way up in the ranks +4

That's good stuff. Thank you for your time. You are being a great help. Can't tell you how long I've been searching the internet for the solution. Maybe I was just searching in the wrong places. lol One more thing, though. How do I make it so that the person's email address is already in the box when they get there so that all they have to do is press the unsubscribe button?

Member Avatar for Member #733618

For that you have to read the table with his/her information. You can recibe that info from a $_GET variable or from a post. Suppouse that wi recibe a $_GET (). Then echo the mail on the value input like this

<form action="unsubscribe.php" method="post">
    <input name="mail" type="text" size="50" value="<? echo $_GET['mail']; ?>" />
    <input name="Submit" type="submit" value="Unsubscribe" />
</form>

You'll see that the mail is on the text box without writing it your self...

Thank you Thank you Thank you. Haven't tried it yet, but I believe it's going to work. I'll come back in a couple of hours probably to mark this as solved so you can get your credit.

Member Avatar for Member #733618

;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.