Location: PHPKode > scripts > Mail Send Addon for Drupal Simplenews Newsletter > unsubscribe.php
<html> 
<head>
<title>Unsubscribe</title>
</head>
<body>

<?php

//example unsubscribe page. Set the settings below and use this file or copy the php code to a Drupal page -you have to enable php in text editor from Drupal modules/settings-. Simplenews unsubscribe functions still work as well but we need this file since we create a new type of unsubscribe URL that is send with mails.

//settings
$db_host="localhost"; $db_user="user"; $db_pass="pass"; $db_name="dbname"; //database connection details
$db_id = mysql_connect($db_host, $db_user, $db_pass)
 or die("Could not connect to DB.");
mysql_select_db($db_name, $db_id);

//functions
function clean_code($str)
{
 $cleaned=strip_tags($str);
 $cleaned=htmlspecialchars($cleaned);
 $to_clean=array("%20", "\"", "'", "\\", ";", ":", "*", "?", "(", ")");
 $cleaned=str_replace($to_clean, "", $cleaned);
 return $cleaned;
}
//end of functions


if(!isset($_GET["checkcode"]) || $_GET["checkcode"]=='') {echo "Wrong check code";
} else {
$_GET["checkcode"]=clean_code($_GET["checkcode"]); 

if(isset($_POST["opsw"]) && $_POST["opsw"]=='1' && isset($_POST["mail"]) && $_POST["mail"]!=''){

$_POST["mail"]=clean_code($_POST["mail"]);

$urdata=explode("-",$_GET["checkcode"]);

if(count($urdata)!='3') { echo "Wrong check code"; } else {

$confirm_sub="select snid from simplenews_subscription where snid='".$urdata['0']."' and tid='".$urdata['1']."' and timestamp='".$urdata['2']."'";
$result_confirm_sub=mysql_query($confirm_sub, $db_id);
$num_rows_confirm_sub=mysql_num_rows($result_confirm_sub);

if ($num_rows_confirm_sub<='0' || !$result_confirm_sub){ echo "Wrong check code"; }
else {
$row_confirm_sub=mysql_fetch_row($result_confirm_sub);

$confirm_mail="select snid from simplenews_subscriber where snid='".$row_confirm_sub['0']."' and mail='".$_POST["mail"]."'";
$result_confirm_mail=mysql_query($confirm_mail, $db_id);
$num_rows_confirm_mail=mysql_num_rows($result_confirm_mail);

if ($num_rows_confirm_mail<='0' || !$result_confirm_mail){ echo "Wrong check code"; }
else {
	
	$update_sub="update  simplenews_subscription set status='0' and timestamp='".time()."' where snid='".$row_confirm_sub['0']."'";
 $result_update_sub=mysql_query($update_sub, $db_id);
 
echo "You have been unsubscribed. Thank you.";
}// if code is ok
}// if code is ok
} //if e-mail is ok
} else { //if not opsw then show form
echo "<form action='' method='post'>Type your e-mail:<input name='mail' type='text' value=''><input name='opsw' type='hidden' value='1'><input id='button' name='button' type='submit' value='Unsubscribe' /></form>";
}
}

?>

</body></html>
Return current item: Mail Send Addon for Drupal Simplenews Newsletter