Jump to content

PHP newsletter debugging help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
LalinM

LalinM

    Newbie

  • Members
  • Pip
  • 2 posts
Hi All,

I have this double opt-in newsletter code and everything works until the point where the subscriber has to click the confirmation link. Clicking the link brings the subscriber to the 'unable to confirm' page (confirm-ko.htm). i.e user couldn't be added to the list. Also I'm supposed to get an email when someone signs up but I don't get anything at all. My webhost supports PHP 5.2.

Here's the newsletter code (newsletter.html):

<link href="NL-style.css" rel="stylesheet" type="text/css">

<form name="form1" method="post" action="NL-process.php">

<table border="0" cellpadding="10" cellspacing="5" class="tbmain">

  <tr>

    <td class="left">E-mail:</td>

    <td><input name="email" type="text" class="text"></td>

  </tr>

  <tr>

    <td colspan="2"><p align="center" class="text">A confirmation email will

        be <br>

        sent to you to verify your address</p>

      <p align="center">

        <input type="submit" name="Submit" value="Send" class="btn">

        <br>

  </tr>

</table>

</form>

code for NL-process.php:


<?php


$emailmanager = '';

$email = $_GET ['email'];

$emailmanager = $_GET['emailmanager'];

$id = $_POST['id'];

$msg = $_POST['msg'];

$headers = $_POST['headers'];

$k = $_POST['k'];

$v = $_POST['v'];

$body = $_POST['body'];

$Ok = $_POST ['Ok'];



$scriptUrl = 'http://www.mydomain.com/NL-confirm.php';


//    These are the positive and negative reply pages URLs


$urlok = 'NL-ok.htm'; //subscribe successful.click confirmation //mail.

$urlko = 'NL-ko.htm'; // invalid email address. go back.


error_reporting(1);


set_magic_quotes_runtime (0);

if (get_magic_quotes_gpc()) {

 foreach($_POST as $k=>$v)

    $_POST[$k] = stripslashes($v);

 foreach($_COOKIE as $k=>$v)

    $_COOKIE[$k] = stripslashes($v);

}


$msg = '';

foreach($_POST as $k=>$v) {

 if (strtolower($k) != "submit" && trim($v) != '')

    $msg .= "$k:$v

";

}


$id = md5($msg);

setcookie("sb$id",$msg,time()+86400,'','',0);


$email = trim($_POST['email']);


$body = "Thank you for your subscription!


In order to confirm your request, please click

on the following link or copy it in your browser

(use the same browser you used for subscribing):


$scriptUrl?email=$email&id=$id


If you didn't ask for this subscription please ignore this message.

";



$Ok = ereg("^([a-zA-Z0-9_.-]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$", $email);

if ($Ok) {

 mail($email,'Please confirm your subscription',$body,'From: '.$emailmanager);

 Header("Location: $urlok");

} else {

 Header("Location: $urlko");

}

?>

NL-confirm.php code:

<?php


$emailmanager = '';

$email = $_GET['email'];

$emailmanager =$_GETT['emailmanager'];

$id = $_POST['id'];

$msg = $_POST['msg'];

$headers = $_POST['headers'];

$k = $_POST['k'];

$v = $_POST['v'];

$body = $_POST['body'];

$Ok = $_POST['Ok'];


//    These are the positive and negative reply pages URLs


$urlok = 'confirm-ok.htm'; // you have been added to the list

$urlko = 'confirm-ko.htm'; // unable to add to list either your // email is invalid or you subscribed after one day


error_reporting(1);

set_magic_quotes_runtime (0);

if (get_magic_quotes_gpc()) {

 foreach($_POST as $k=>$v)

    $_POST[$k] = stripslashes($v);

 foreach($_COOKIE as $k=>$v)

    $_COOKIE[$k] = stripslashes($v);

}


$msg = $_COOKIE['sb'.$_GET['id']];


$email = trim($_GET['email']);

$Ok = ereg("^([a-zA-Z0-9_.-]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$", $email);


$headers  = 'From: ' . $email . "

";

$headers .= 'MIME-Version: 1.0' ."

";

$headers .= 'Content-Type: text/plain; charset=iso-8859-1' ."

";

$headers .= 'Content-Transfer-Encoding: 8bit'. "


";


if ($Ok && ($msg != '')) {

 mail($emailmanager,'Subscribe',$msg,$headers);

 Header("Location: $urlok");

} else {

 Header("Location: $urlko");

}

?>

I've asked this question but I get answers I can't figure out. I'm told the ereg statement is the cause but I don't know how to fix it. I'm new to PHP BTW.

Thanks a lot for the help.

Edited by TkTech, 18 July 2010 - 08:32 AM.
Added code tags.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
With PHP >= 5.2.0 you can use this instead of regex (especially because ereg is deprecated anyway):
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $Ok = FALSE;
} else {
    $Ok = TRUE;
}
Also, you send POST data to NL-process.php yet on NL-process.php:
$email = $_GET ['email'];
$emailmanager = $_GET['emailmanager'];
How do you get $_GET elements from a form that only POSTs? The $email form may not even exist.

Try fixing those first and see what the result is, remember, it'd be usedful to echo some data and test it yourself to see what really works or doesn't before letting end users try it (and having it fail).
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
LalinM

LalinM

    Newbie

  • Members
  • Pip
  • 2 posts
Re: Nullw0rm,

Many thanks for your reply. I tried what you suggested, but I get the same result. I just replaced the ereg statement with the if {!filter.... and changed the GET to POST.

Any ideas?
Thanks again.