It is shown in this example how to create a verification code and subscribe/unsubscribe user for any kind of a mailing list. It is meant that the dispatch itself is carried out by means of a server script or a program. Script creates a form with the relevant fields and the verification code on the page. User selects the action he wants to do (subscribe/unsubscribe) and enters the code from the image. If the entered code matches with the imaged one, the form is sent and the actions are carried out. This script may be used almost everywhere, where the security code is used to confirm user's actions. (e.g. enabling and disabling some user options, features etc) The source code of this example is shown below.
In the template of the page:
<?IF($USER_ID$)?> <div align="center"> <p> <b>This example shows the way of using the script to subscribe /unsubscribe for any kind of mailing lists for the current user, using security code</b> </p> </div> <hr> <?$PHPCODE$("http://phpexample.ucoz.com/php/example000/example000.php?uid=",$USER_ID$)?> <?ELSE?> <div id="phpdiv"> <p>Guests are prohibited from viewing this page. Please, authorize yourself.</p> </div> <?ENDIF?>
PHP-script of this example:
<?php session_start(); if(count($_POST)>0){ $___notjson=1; if(isset($_SESSION['captcha_keystring']) && $_SESSION['captcha_keystring'] == $_POST['keystring']){ $lines = file('subscr.dat'); if ($_POST['atype']) { $status_ok=0; foreach ($lines as $line_num => $line) { if (trim($line)==$_POST['userid']) { unset($lines[$line_num]); file_put_contents('subscr.dat', $lines); $status_ok=1; break; } } if ($status_ok) echo "$('#captcha_result').html(You have unsubscribed from the mailing list')"; else echo "$('#captcha_result').html('You haven't been subscribed to the mailing list')"; } else { $status_err=0; foreach ($lines as $line_num => $line) { if (trim($line)==$_POST['userid']) { $status_err=1; break; } } if ($status_err) echo "$('#captcha_result').html('You have been subscribed to the mailing list')"; else { if (file_put_contents('subscr.dat', $_POST['userid']."\n", FILE_APPEND)==FALSE) echo "$('#captcha_result').html('Error, try again later')"; echo "$('#captcha_result').html('You have subscribed to the mailing list')"; } } }else{ echo "$('#captcha_result').html('Incorrect security code')"; } exit; } unset($_SESSION['captcha_keystring']); ?>