Tutorial for converting mysql code to mysqli code?

8 posts by 3 authors in: Forums > CMS Builder
Last Post: September 14, 2017   (RSS)

By gkornbluth - September 2, 2017

Hi All,

Converting mysql code to mysqli code for a PHP7 upgrade is making my head ache (with little success), as it is I’m sure for many other CMSB users.

Any chance that we can start a tutorial on how to convert the code we have.

If we post a code snippet I’m hoping that someone will take a look as well.

Thanks,

Jerry Kornbluth




The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - September 4, 2017

Hi all,

I’ve been using the following to populate and update a double opt in email sign up database, and I’m having trouble converting the code to work in PHP7.

Any thoughts appreciated

Thanks,

Jerry Kornbluth


INSERT ORIGINAL RECORD INTO DATABASE

  mysql_query("INSERT INTO `{$TABLE_PREFIX}email_signup` SET
                      last_name            = '".mysql_real_escape_string( $_REQUEST['last_name'] )."',
                      first_name          = '".mysql_real_escape_string( $_REQUEST['first_name'] )."',
              email                  = '".mysql_real_escape_string( $_REQUEST['email'] )."',
     hidden           = '1',
    createdDate      = NOW(),
            updatedDate      = NOW(),
            createdByUserNum = '0',
            updatedByUserNum = '0'")
    or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
    $recordNum = mysql_insert_id();



UPDATE DATABASE RECORD (SECOND OPT IN)

 $where = "email='".mysql_escape($_REQUEST['email'])."'";
 if(mysql_select_count_from('email_signup',$where)){ //check to ensure that email exists in the table
    $where.=" AND confirmed='1'";
    if(!mysql_select_count_from('email_signup',$where)){ //email exists and has not been confirmed yet
        $query = "UPDATE `{$TABLE_PREFIX}email_signup` SET
              hidden           = '0',
              confirmed          = '1',
              updatedDate      = NOW()
               
             WHERE email = '".mysql_escape( $_REQUEST['email'] )."'";
        mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
        $userNum = mysql_insert_id();



 

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - September 6, 2017

Wow, this is great.

Thank you Dave.

It will sure save a lot of headaches going down the road.

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - September 7, 2017 - edited: September 8, 2017

Hi All,

With a lot of help from Dave Edis, (Thank you Dave!), here’s a complete, revised recipe from my CMSB Cookbook for a CMSB double opt in email signup system, updated for PHP7 and using Google Recaptcha.

I hope it proves helpful...

If anyone has any recommendations for more security or better operation, please post your comments.

Just so you know, there are almost 500 other recipes now in the Cookbook.

CMSB users can get a 3 month free trial subscription at: http://www.thecmsbcookbook.com/trial.php

Best,

Jerry Kornbluth

CMSB DOUBLE OPT IN MAIL LIST SYSTEM (FOR PHP7 AND USING GOOGLE RECAPTCHA)

There are 4 pages that act on a section called email_signup:

email_signup.php - the initial signup form that creates a record in the database with the hidden field checked and the confirmed field unchecked. The page also sends a confirmation email to the email entered with a link that the recipient can click on to confirm that they wanted to sign up.

confirm.php - the confirmation page that the person accesses through the link in their email to confirm that they wanted to sign up.

unsubscribe.php - the form used to unsubscribe. As above a confirmation email is sent to the email entered, and no action is taken until the recipient clicks on the link in the email.

unsubscribe_confirm.php - as above, the page that the person addresses through the link in their email to confirm that they wanted to unsubscribe.

Here’s the .ini.php code for creating the email_signup section, email_signup,ini.php

<?php /* This is a PHP data file */ if (!@$LOADSTRUCT) { die("This is not a program file."); }
return array (
  '_detailPage' => '',
  '_disableAdd' => '0',
  '_disableErase' => '0',
  '_disableModify' => '0',
  '_disablePreview' => '1',
  '_disableView' => '1',
  '_filenameFields' => '',
  '_hideRecordsFromDisabledAccounts' => '0',
  '_indent' => '0',
  '_listPage' => '',
  '_maxRecords' => '',
  '_maxRecordsPerUser' => '',
  '_perPageDefault' => '1000',
  '_previewPage' => '',
  '_requiredPlugins' => '',
  '_tableName' => 'email_signup',
  'listPageFields' => 'last_name,first_name,email,hidden,confirmed,remove,source,createdDate,updatedDate',
  'listPageOrder' => 'source DESC, confirmed DESC, last_name, first_name',
  'listPageSearchFields' => '__ALL__',
  'menuHidden' => '0',
  'menuName' => 'Email Signup',
  'menuOrder' => '0000000004',
  'menuPrefixIcon' => '',
  'menuType' => 'multi',
  'num' => array(
    'order' => 1,
    'type' => 'none',
    'label' => 'Record Number',
    'isSystemField' => '1',
  ),
  'createdDate' => array(
    'order' => 2,
    'type' => 'none',
    'label' => 'Created',
    'isSystemField' => '1',
  ),
  'createdByUserNum' => array(
    'order' => 3,
    'type' => 'none',
    'label' => 'Created By',
    'isSystemField' => '1',
  ),
  'updatedDate' => array(
    'order' => 4,
    'type' => 'none',
    'label' => 'Last Updated',
    'isSystemField' => '1',
  ),
  'updatedByUserNum' => array(
    'order' => 5,
    'type' => 'none',
    'label' => 'Last Updated By',
    'isSystemField' => '1',
  ),
  'hidden' => array(
    'order' => 6,
    'label' => 'Hidden',
    'type' => 'checkbox',
    'fieldPrefix' => '',
    'checkedByDefault' => '0',
    'description' => '',
    'checkedValue' => 'Yes',
    'uncheckedValue' => 'No',
  ),
  'confirmed' => array(
    'order' => 7,
    'label' => 'Confirmed',
    'type' => 'checkbox',
    'fieldPrefix' => '',
    'checkedByDefault' => '0',
    'description' => '',
    'checkedValue' => 'Yes',
    'uncheckedValue' => 'No',
  ),
  'remove' => array(
    'order' => 8,
    'label' => 'Remove',
    'type' => 'checkbox',
    'fieldPrefix' => '',
    'checkedByDefault' => '0',
    'description' => '',
    'checkedValue' => 'Yes',
    'uncheckedValue' => 'No',
  ),
  'first_name' => array(
    'order' => 9,
    'label' => 'First Name',
    'type' => 'textfield',
    'defaultValue' => '',
    'fieldPrefix' => '',
    'description' => '',
    'fieldWidth' => '',
    'isPasswordField' => '0',
    'isRequired' => '0',
    'isUnique' => '0',
    'minLength' => '',
    'maxLength' => '',
    'charsetRule' => '',
    'charset' => '',
  ),
  'last_name' => array(
    'order' => 10,
    'label' => 'Last Name',
    'type' => 'textfield',
    'defaultValue' => '',
    'fieldPrefix' => '',
    'description' => '',
    'fieldWidth' => '',
    'isPasswordField' => '0',
    'isRequired' => '0',
    'isUnique' => '0',
    'minLength' => '',
    'maxLength' => '',
    'charsetRule' => '',
    'charset' => '',
  ),
  'email' => array(
    'order' => 11,
    'label' => 'Email',
    'type' => 'textfield',
    'defaultValue' => '',
    'fieldPrefix' => '',
    'description' => '',
    'fieldWidth' => '',
    'isPasswordField' => '0',
    'isRequired' => '0',
    'isUnique' => '0',
    'minLength' => '',
    'maxLength' => '',
    'charsetRule' => '',
    'charset' => '',
  ),
  'source' => array(
    'order' => 12,
    'label' => 'Source',
    'type' => 'list',
    'fieldPrefix' => '',
    'description' => '',
    'isRequired' => '0',
    'isUnique' => '0',
    'listType' => 'pulldown',
    'optionsType' => 'text',
    'optionsText' => 'Exhibition
Meeting
Street Fair
Web Site',
  ),
);
?>

___________________________
emailsignup.php

The code at the top of the page, above the head, after the records calls required for your site:

<?php
  // load records from 'email_signup'
  list($email_signupRecords, $email_signupMetaData) = getRecords(array(
    'tableName'   => 'email_signup',
    'loadUploads' => true,
    'allowSearch' => false,
  ));
?>
<?php $signup_email = ' the_email_address_you_want_to_use_for_return_and_reply' ?>
<?php
if (@$_REQUEST['submit']) {
 
function validateGoogleCaptcha(){
    
    $errorsAndAlerts = "";

    if (!@$_REQUEST['g-recaptcha-response'])     { $errorsAndAlerts .= "Please check the anti-spam 'I am not a robot' checkbox!<br />\n";
    $showSignupForm = true; // don't change this value
    }
    else {
      // check recaptcha
      $postdata = array();
      $postdata['secret']   = 'your Google Recaptcha secret code';
      $postdata['response'] = @$_REQUEST['g-recaptcha-response'];
      $postdata['remoteip'] = $_SERVER['REMOTE_ADDR'];
      $url = "https://www.google.com/recaptcha/api/siteverify?". http_build_query($postdata, '', '&');
      list($json, $httpStatusCode, $headers, $request) = getPage($url, 5, '', true);
      $recaptchaResponse = json_decode($json, true);
      
      if (!$recaptchaResponse['success']) {
        if (is_array($recaptchaResponse['error-codes'])) {
          if (in_array('missing-input-secret', $recaptchaResponse['error-codes']))     { $errorsAndAlerts .= "There's a problem with recaptcha, please let us know! (no secret)<br />\n"; }
          if (in_array('invalid-input-secret', $recaptchaResponse['error-codes']))     { $errorsAndAlerts .= "There's a problem with recaptcha, please let us know! (invald secret)<br />\n"; }
          if (in_array('missing-input-response', $recaptchaResponse['error-codes']))   { $errorsAndAlerts .= "Please fill out the recaptcha box!<br />\n";
          $showSignupForm = true; // do we need this line?
           }
          if (in_array('invalid-input-response', $recaptchaResponse['error-codes']))   { $errorsAndAlerts .= "Please fill out the recaptcha box again, your answer was incorrect!<br />\n";
          $showSignupForm = true; // do we need this line?
          }
        }
        if (!@$errorsAndAlerts) { @$errorsAndAlerts .= "Invalid captcha response, please try again or contact us directly and let us know."; }
        @trigger_error("Failed recaptcha on signup form", E_USER_NOTICE);
      }
    }
    return $errorsAndAlerts;
  }
}
?>
<?php
// submit form
if (@$_REQUEST['submit']) {
 @$errorsAndAlerts .= validateGoogleCaptcha();
  // error checking

  if (!@$_REQUEST['first_name'])    { $errorsAndAlerts .= "Please enter your first name<br /><br />\n"; }
  if (!@$_REQUEST['last_name'])  { $errorsAndAlerts .= "Please enter your last name<br /><br />\n"; }
    if (!@$_REQUEST['email'])  { $errorsAndAlerts .= "Please enter your email address<br /><br />\n"; }

// email checking
    if ($_REQUEST['email'] || $_REQUEST['email2']) {
      if (!@$_REQUEST['email'])                            { $errorsAndAlerts .= "Please enter your email address<br /><br />\n"; }
      elseif (!@$_REQUEST['email2'])                            { $errorsAndAlerts .= "Please re-enter your email address<br /><br />\n"; }
      elseif ($_REQUEST['email'] != $_REQUEST['email2']) { $errorsAndAlerts .= "Sorry, the e mail addresses you entered don't match!<br /><br />\n"; }
    }

 // check for duplicate emails
    if (!$errorsAndAlerts) {
      
      $count = mysql_select_count_from('email_signup', "'".mysql_escape($_REQUEST['email'])."' IN (email)");
      if ($count > 0) { $errorsAndAlerts .= "That email address is already signed up, please choose another!<br /><br />\n"; }
    }

  // turn off strict mysql error checking for: STRICT_ALL_TABLES
  mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

  // add record
  if (!@$errorsAndAlerts) {
    $tablename   = 'email_signup';
       $colsToValues = array();
       $colsToValues['createdDate=']     = 'NOW()';
       $colsToValues['updatedDate=']     = 'NOW()';
       $colsToValues['createdByUserNum'] = 0;
       $colsToValues['updatedByUserNum'] = 0;
       $colsToValues['first_name']         = $_REQUEST['first_name'];
       $colsToValues['last_name']         = $_REQUEST['last_name'];
       $colsToValues['email']            = $_REQUEST['email'];
       $colsToValues['hidden'] = 1;
       $hideMissingFieldErrors = true;
       $newRecordNum = mysql_insert($tablename, $colsToValues, $hideMissingFieldErrors);

    // display thanks message and clear form
    $errorsAndAlerts = "Thanks for submitting your information. <br /><br /> Before we can add your email address to our list, you'll need to confirm your intent by clicking on the link in the email that you will receive shortly. <br /><br /> If you do not see the email, check your spam folder.";

 
    // send email to applicant    
    $to=$_REQUEST['email'];
$subject = 'Email List Signup Request';
$headers = "From: $signup_email" . "\r\n";
$headers .= "Reply-To: $signup_email" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$eml = $_REQUEST['email'];
$message .= "<tr ><td><h2 align='center'>EMAIL LIST SIGNUP REQUEST</h2><br /><br />There is just one more step to be included on our email distribution list.<br /><br />To make sure that no one else signed you up for this list, please click on this link or paste it into your browser.<br /><br />
 <a href='http://your_site.com/confirmed.php?submit=1&confirmed=1&hidden=0&email=$eml'>http://your_site.com/confirmed.php?submit=1&confirmed=1&hidden=0&email=$eml</a></td></tr>";
$message .= "</table>";
$message .= "</body></html>";


// Send
if (mail($to,$subject,$message, $headers))
{
 echo 'Mail sent!';
} else
{
 echo 'Error! Mail was not sent.';
};
  }

}

?>

And the active code in the body of the page:

 <table width="92%" border="0" align="center">
            <tr>
              <td valign="top"><form method="post" action="">
                  <input type="hidden" name="submit" value="1" />
                  <?php if (@$errorsAndAlerts): ?>
                  <div class="heading_font" align="left" style="color:#C00"><br />
                    <?php echo $errorsAndAlerts; ?><br />
                    <br />
                  </div>
                  <?php endif ?>
                  <table align="left"  border="0" cellspacing="0" cellpadding="2">
                    <tr>
                      <td class=" text_font" valign="top"><b>First Name</b></td>
                      <td><input type="text" name="first_name" value="<?php echo htmlspecialchars(@$_REQUEST['first_name']) ?>" size="30" /></td>
                    </tr>
                    <tr>
                      <td class="text_font" valign="top"><b>Last Name</b></td>
                      <td><input type="text" name="last_name" value="<?php echo htmlspecialchars(@$_REQUEST['last_name']) ?>" size="30" /></td>
                    </tr>
                    <tr>
                      <td class="text_font" valign="top"><b>Email Address</b></td>
                      <td><input type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']) ?>" size="30" /></td>
                    </tr>
                    <tr>
                      <td class="text_font" valign="top"><b>Re-enter Your Email Address</b></td>
                      <td><input type="text" name="email2" value="<?php echo htmlspecialchars(@$_REQUEST['email2']) ?>" size="30" /></td>
                    </tr>
                    <tr>
                      <td valign="top">&nbsp;</td>
                      <td>&nbsp;</td>
                    </tr>
                    <tr>
                      <td colspan="2" class="text_font" style=" font-weight: bold;" valign="top">Please check the "I'm not a robot" box below before submitting.<br />
                        <br />
                        <div class="g-recaptcha" data-theme="light" data-sitekey="Your Google Recaptcha site key"></div></td>
                    </tr>
                    <tr>
                      <td style="padding: 5px;"><input type="submit" name="add" value="Click To Submit &gt;&gt;" />
                        <br />
                        <br /></td>
                    </tr>
                  </table>
                </form></td>
            </tr>
          </table>


___________________________
confirmed.php

The code at the top of the page, above the head, after the records calls required for your site:

<?php
// submit form
if (@$_REQUEST['submit']) {

  // error checking
  $errorsAndAlerts = "";
  if (!@$_REQUEST['email'])  { $errorsAndAlerts .= "Please enter the email address you used when you signed up.<br /><br />\n"; }

   // turn off strict mysql error checking for: STRICT_ALL_TABLES
  mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
 
 // update user
   if (!$errorsAndAlerts) {
    
    $emailExists        = mysql_count('email_signup',         ['email' => $_REQUEST['email']]);
$emailConfirmed     = mysql_count('email_signup', ['email' => $_REQUEST['email'], 'confirmed' => 1]);
$userNum            = 0;
if ($emailExists && !$emailConfirmed) {
  $updateNum    = null;
  $updateWhere  = [ 'email' => $_REQUEST['email'] ];
  $colsToValues = [ 'hidden' => 0, 'confirmed' => '1', 'updatedDate=' => 'NOW()' ];
  mysql_update('email_signup', $updateNum, $updateWhere, $colsToValues);
  $userNum = mysqli()->insert_id;
$errorsAndAlerts = "Thanks, your email address has been succesfully added to our maillist";
    
}
    elseif($emailExists && $emailConfirmed){//email exists, but has already been confirmed
      $errorsAndAlerts.="That Email address has already been confirmed.<br /><br />If you'd like to sign up with another Email address,<br /><br /><a  href='http://your_site.com/email_signup.php'><span class='heading_font' >CLICK ON THIS LINK</span></a><br />";
    }
    
     elseif (!$emailExist){ //email does not exist in the database
    $errorsAndAlerts.="Sorry, that email address doesn't exist in the database.<br /><br />Please enter the same Email address that you used when you signed up, and click on submit.<br />";
      }
       
}}
 
?>


And the active code in the body of the page:

 <form method="post" action="">
        <input type="hidden" name="submit" value="1" />
        
        <table width="90%" border="0" cellpadding="5" cellspacing="0">
          <tr>
            <td valign="top">&<?php if (@$errorsAndAlerts): ?>
        <div class="text_font" align="left" style="color: #C00; font-weight: bold;"><br />
          <?php echo $errorsAndAlerts; ?><br />
        </div>
        <?php endif ?></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td align="left" class="text_font" valign="top"><b>If you see an error above, it's probably because <input type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']) ?>" size="30" /> is not the Email address that you used when you signed up.<br />
              <br />
              Please change it and click on submit.</b></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td valign="top">&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
        </table>
        <input type="submit" name="add" value="Click to Submit &gt;&gt;" />
      </form>
      <p align="left" class="text_font"><br />
        <br />
      If you no longer want to receive information about us, <a style="text-decoration:underline; color:#000" href="http://www.yoursite.com/unsubscribe.php"><span class="text_font">CLICK/TAP HERE</span></a> to unsubscribe.</p>
___________________________
unsubscribe.php

The code at the top of the page, above the head, after the records calls required for your site:

<?php
  // load records from 'email_signup'
  list($email_signupRecords, $email_signupMetaData) = getRecords(array(
    'tableName'   => 'email_signup',
    'loadUploads' => true,
    'allowSearch' => false,
  ));
?>
<?php $signup_email = 'the_email_address_you_want_to_use_for_return_and_reply' // the email to use for return and reply?>
<?php
if (@$_REQUEST['submit']) {
 
function validateGoogleCaptcha(){
    
    $errorsAndAlerts = "";

    if (!@$_REQUEST['g-recaptcha-response'])     { $errorsAndAlerts .= "Please check the anti-spam 'I am not a robot' checkbox!<br />\n";
    $showSignupForm = true; // don't change this value
    }
    else {
      // check recaptcha
      $postdata = array();
      $postdata['secret']   = 'your Google Recaptcha secret code';
      $postdata['response'] = @$_REQUEST['g-recaptcha-response'];
      $postdata['remoteip'] = $_SERVER['REMOTE_ADDR'];
      $url = "https://www.google.com/recaptcha/api/siteverify?". http_build_query($postdata, '', '&');
      list($json, $httpStatusCode, $headers, $request) = getPage($url, 5, '', true);
      $recaptchaResponse = json_decode($json, true);
      
      if (!$recaptchaResponse['success']) {
        if (is_array($recaptchaResponse['error-codes'])) {
          if (in_array('missing-input-secret', $recaptchaResponse['error-codes']))     { $errorsAndAlerts .= "There's a problem with recaptcha, please let us know! (no secret)<br />\n"; }
          if (in_array('invalid-input-secret', $recaptchaResponse['error-codes']))     { $errorsAndAlerts .= "There's a problem with recaptcha, please let us know! (invald secret)<br />\n"; }
          if (in_array('missing-input-response', $recaptchaResponse['error-codes']))   { $errorsAndAlerts .= "Please fill out the recaptcha box!<br />\n";
          $showSignupForm = true; // do we need this line?
           }
          if (in_array('invalid-input-response', $recaptchaResponse['error-codes']))   { $errorsAndAlerts .= "Please fill out the recaptcha box again, your answer was incorrect!<br />\n";
          $showSignupForm = true; // do we need this line?
          }
        }
        if (!@$errorsAndAlerts) { @$errorsAndAlerts .= "Invalid captcha response, please try again or contact us directly and let us know."; }
        @trigger_error("Failed recaptcha on signup form", E_USER_NOTICE);
      }
    }
    return $errorsAndAlerts;
  }
}
?>
<?php $redirect = '0' // set a variable called $redirect to a value of 0 ?>
<?php if(@$_REQUEST['submit']):?>
<?php
    $errorsAndAlerts = "";
    $errorsAndAlerts .= validateGoogleCaptcha();
    
 

  if (!@$_REQUEST['email'])  { $errorsAndAlerts .= "Please enter your email address<br /><br />\n"; }

// email checking
    if ($_REQUEST['email'] || $_REQUEST['email2']) {
      if (!@$_REQUEST['email'])                            { $errorsAndAlerts .= "Please enter your email address<br /><br />\n"; }
      elseif (!@$_REQUEST['email2'])                            { $errorsAndAlerts .= "Please re-enter your email address<br /><br />\n"; }
      elseif ($_REQUEST['email'] != $_REQUEST['email2']) { $errorsAndAlerts .= "Sorry, the e mail addresses you entered don't match!<br /><br />\n"; }
    }
    
 // check for existing emails
   if (!$errorsAndAlerts) {
      
      $count = mysql_select_count_from('email_signup', "'".mysql_escape($_REQUEST['email'])."' IN (email)");
         if ($count < 1) { $redirect = '1'; // if no matching email address change the variable $redirect to a value of 1
         }
    }

  // turn off strict mysql error checking for: STRICT_ALL_TABLES
  mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
   
  ?>
<?php  
  // error checking
  if (!@$errorsAndAlerts && $redirect == '0') {
    

    // display sorry message and clear form
    $errorsAndAlerts = "We'll be sorry to see you go.<br /><br />To make sure that no one else is trying to remove your email address from our list, you'll need to confirm your intent by clicking on the link in the email that you will receive shortly. <br /><br /> If you don't see the email, check your spam folder.";

    // send email to applicant    
    $to=$_REQUEST['email'];
$subject = 'Email List Removal Request';

$headers = "From: $signup_email" . "\r\n";
$headers .= "Reply-To: $signup_email" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$eml = $_REQUEST['email'];
$message .= "<tr ><td><div align='left'><img src='http://www.your_site.com/images/email-masthead-400px.png' style='border:hidden;'/></div><br /><br /><h2 align='center'>EMAIL LIST LIST REMOVAL REQUEST</h2><br /><br />We're sorry to see you go.<br /><br />There's just one more step to be removed from our email distribution list.<br /><br />To make sure that no one else is trying to remove you from this list, please click on this link or paste it into your browser.<br /><br />
 <a href=http:/your_site.com/unsubscribe_confirm.php?submit=1&remove_me=1&email=$eml'>http://your_site.com/unsubscribe_confirm.php?submit=1&remove=1&email=$eml</a></td></tr>";
$message .= "</table>";
$message .= "</body></html>";

// Send
if (mail($to,$subject,$message, $headers))
{
 echo 'Mail sent!';
} else
{
 echo 'Error! Mail was not sent.';
};
  }
?>
<?php if ($redirect == '1'):?>
<?php
/* Redirect browser */
header("Location:http://your_site.com/your_unsubscribe_contact_page.php");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
<?php endif ?>
<?php endif?>

And the active code in the body of the page:

<table width="100%"   border="0" align="center" cellpadding="10">
        <tr>
          <td valign="top"><div align="center" class="heading_font">EMAIL LIST UNSUBSCRIBE<br />
            </div>
            <div align="center" style="width:80%; text-align:left">
            <div align="center">
              <p class="heading_font">Please Use The Form Below<br />
                To Unsubscribe From Our Email List </p>
            </div>
            <form method="post" action="">
              <input type="hidden" name="submit" value="1" />
              <?php if (@$errorsAndAlerts): ?>
              <div align="left" style="color: #C00; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 15px;"><br />
                <?php echo $errorsAndAlerts; ?><br />
                <br />
              </div>
              <?php endif ?>
              <table align="left"  border="0" cellspacing="0" cellpadding="2">
                <tr>
                  <td class="Medium-Text" valign="top"><b>Enter The Email Address<br />
                    To Be Removed</b></td>
                  <td><input type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']) ?>" size="30" /></td>
                </tr>
                <tr>
                  <td class="Medium-Text" valign="top"><b>Re-enter The Email Address</b></td>
                  <td><input type="text" name="email2" value="<?php echo htmlspecialchars(@$_REQUEST['email2']) ?>" size="30" /></td>
                </tr>
                <tr>
                  <td colspan="2" class="text_font" style="color: #<?php echo $site_colorsRecord['menu_background_color'] ?>; font-weight: bold;" valign="top">Please check the "I'm not a robot" box below before submitting.<br />
                    <br />
                    <div class="g-recaptcha" data-theme="light" data-sitekey="your_Google Recaptcha site key"></div></td>
                </tr>
                <tr>
                  <td colspan="2" valign="top"><br />
                    <input type="submit" name="submit" value="Click To Submit &gt;&gt;" /></td>
                </tr>
              </table>
            </form></td>
        </tr>
      </table>
___________________________
unsubscribe_confirm.php

The code at the top of the page, above the head, after the records calls required for your site:

<?php
// submit form
if (@$_REQUEST['submit']) {

  // error checking
  $errorsAndAlerts = "";
  if (!@$_REQUEST['email'])  { $errorsAndAlerts .= "Please enter the email address you used when you signed up.<br /><br />\n"; }

   // turn off strict mysql error checking for: STRICT_ALL_TABLES
  mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
 
 // update user
   if (!$errorsAndAlerts) {
    
    $emailExists        = mysql_count('email_signup',         ['email' => $_REQUEST['email']]); //check to ensure that email exists in the table
$emailRemoved     = mysql_count('email_signup', ['email' => $_REQUEST['email'], 'remove' => 1]); // check to ensure that email exists and has not been removed yet
$userNum            = 0;
if ($emailExists && !$emailRemoved) {
  $updateNum    = null;
  $updateWhere  = [ 'email' => $_REQUEST['email'] ];
  $colsToValues = [ 'hidden' => 1, 'remove' => '1', 'updatedDate=' => 'NOW()' ];
  mysql_update('email_signup', $updateNum, $updateWhere, $colsToValues);
  $userNum = mysqli()->insert_id;
$errorsAndAlerts = "Thanks, your email address has been successfully removed from our maillist.<br /><br />To sign up again, <a style='text-decoration:underline; color:#C00;' href='http://www.your_site.com/email_signup.php'><span class='text_font'><font color='#C00'>CLICK HERE</font></span></a> for our email sign up page.";
    
}
    elseif($emailExists && $emailRemoved){//email exists, but has already been removed
      $errorsAndAlerts.="That Email address has already been removed.<br /><br />To remove another address, <a style='text-decoration:underline; color:#C00;' href='http://www.your_site.com/unsubscribe.php'><span class='text_font'><font color='#C00'>CLICK HERE</font></span></a> to return to our unsubscribe page.<br />";
    }
    
     elseif (!$emailExist){ //email does not exist in the database
    $errorsAndAlerts.="Sorry, that email address doesn't exist in the database.<br /><br />>To be removed from our list, <a style='text-decoration:underline; color:#C00;' href='http://www.your_site.com/unsubscribe.php'><span class='text_font'><font color='#C00'>CLICK HERE</font></span></a> to return to our unsubscribe page and enter the email address that you used when you signed up.<br />";
      }
       
}}
 
?>

And the active code in the body of the page:

<div align="center" class="heading_font">
        <h2>Email List Unsubscribe Confirmation</h2>
      </div>
      <table width="900px"   border="0" align="center" cellpadding="10">
        <tr>
          <td class="heading_font" ><?php if (@$errorsAndAlerts): ?>
            <div align="left" style="color: #C00; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 15px;"><br />
              <?php echo $errorsAndAlerts; ?><br />
              <br />
            </div>
            <?php endif ?>
            <br />
            <br /></td>
        </tr>
      </table>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By rez - September 14, 2017

Hi. What's this all about? Is our usual CMSB code / viewers and such, depreciating? If our hosts update to PHP7 will we have problems on existing sites?

By gkornbluth - September 14, 2017 - edited: September 14, 2017

Hi Rez,

In PHP7, some commands have been removed and replaced with others because of security and other factors.

Most larger web hosts (like Bluehost) don't just upgrade the PHP versions. They install the latest version on their server and  give you the option of using them.

If you've upgraded to the latest version of CMSB, (3.10) the core code for your installation should be fine with PHP7.

If you've got older custom code on your site there's a check box in the latest version of CMSB under Admin < General settings, called "Legacy MySQL Support" which will allow you to "Connect to legacy PHP MySQL library to support old code (doubles required MySQL connections)" until you can update all of your custom code.

If you're using official plugins you should update them to the latest versions. Some user supplied plugins have been updated, some have not, you'll have to see if they work correctly or update them accordingly. (Post issues on the forum and you'll probably get help solving them)

If you're using the website membership plugin, once you've updated it to the latest version (1.12), you'll have to update the code on your generated code pages like the sign up, log in, password request, update profile, and other forms to reflect the newest generated code. There may be other official plugins that will require updating of the code on some generated pages.

If your sites are complex the process may take some time to work through, but you can always contact consulting to help you (use the "Hire Us" tab on the Nav Menu at the top of this page) .

Hope that helps,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By rez - September 14, 2017

I forgot, yes, my Cpanel has options to upgrade PHP only when desired. Thanks for this entire explanation. Very helpful!