PHP Script Help

Soldato
Joined
12 Dec 2003
Posts
2,588
Trying to get this mailing list script work: http://www.phptutorial.info/scripts/mailinglist/

I have pasted the whole script from the source page on that site into mailer.php and put it on my local apache server, removing the checkdnsrr line and making the emaillist.txt file.

Problem is when I try and load mailer.php it shows this part of the script printed on the page before the form:

Code:
Please check permissions in the directory or create a file with coresponding name."); fputs($cf, "Mailing list subscribers\n"); fclose($cf); } 
// IF REQUEST HAS BEEN TO SUBSCRIBE FROM MAILING LIST, ADD EMAIL TO THE FILE if ($action=="subc"){ 
// check whether the email is already registered if(strpos($file_content,"<$email>")>0){die("Error: your email is already included in this mailing list");} 
// write the email to the list (append it to the file) $cf = fopen($file, "a"); fputs($cf, "\n<$email>"); 
// new email is written to the file in a new line fclose($cf); 
// notify subscription print "Your email has been added to our mailing list.
Thanks for joining us."; } 
// IF REQUEST HAS BEEN TO UNSUBSCRIBE FROM MAILING LIST, REMOVE EMAIL FROM THE FILE if ($action=="unsubc"){ 
// if email is not in the list, display error if(strpos($file_content,"<$email>")==0){die("Error: your email is not included in this mailing list");} 
// remove email from the content of the file $file_content=preg_replace ("/\n<$email>/","",$file_content); 
// print the new content to the file $cf = fopen($file, "w"); fputs($cf, $file_content); fclose($cf); 
// notify unsubscription print "Your email has been removed from our mailing list. "; } ?> 1 $test2=strpos(substr($email,strpos($email,"@")), "."); 
//value must be >1 $test3=strlen($email); //value must be >6 $test4=substr_count ($email,"@"); 
//value must be 1 if ($test1<2 or $test2<2 or $test3<7 or $test4!=1){die($message);} 
// check whether email is correct (advance checking) 
// extracts whatever is after "@" to variable $email_server $email_server=substr($email,strpos($email, "@")+1); 
// Check DNS records (0 => the server exists; 1=> the server does not exist) if (checkdnsrr($email_server)!=1){die ($message);} } 
// THIS FUNCTION WILL SHOW THE FORM 
// MODIFY IT AS REQUIRED function print_form(){ ?>

I'm not to good with php but looking at the source in firefox shows all the php up to line 49 being highlighted in pink which leads me to believe there is some mistake in the syntax of the script somewhere? If anyone knows a better script for doing this then I'm all ears but if this works it will do almost exactly what I want it to.

Thanks
 
Last edited:
I removed lines 102 (the comment) and 103 completely. Yup, apache and php are both running, phpinfo() execs no problems! The colour coding (both here and in Notepad++) looks ok. I'm using xampp (apache/mysql/php bundle), tried it on my other computers xampp install and it did the same thing. Is it possible that php isn't configured properly or something?

mailer.php:

PHP:
<?
/*
Email list script
by phptutorial.info
*/

// to avoid showning errors (change to 1 to show them). For security
error_reporting(0);

// if info is posted, show the form and die
// the form is in the bottom of the page
if (!$_POST){print_form();die();}

// when info is posted you will be here
?>

<html>
<head>
  <title>Email list</title>
</head>
<body bgcolor=FFFFFF>
<?

// GET EMAIL
        $email=$_POST["email"];
        // To avoid problems, only lower case is used
        $email=strtolower($email);
        // Check whether email is correct by using a function
        //  function requires the email address and the error message
        check_email ($email, "Error: email is not valid.");


// GET VALUE FOR action : subc (subscribe) or unsubc (unsubscribe)
$action=$_POST["action"];

// this is the file with the info (emails)
//    When using the link in the top to download the complete script, a new name for this file
//    will be generated (p.e.: emaillist-2ax1fd34rfs.txt), so users will be unable to find it
$file = "email2.txt";

// lets try to get the content of the file
if (file_exists($file)){
        // If the file is already in the server, its content is pasted to variable $file_content
        $file_content=file_get_contents($file);
}else{
        // If the file does not exists, lets try to create it
        //   In case file can not be created (probably due to problems with directory permissions),
        //   the users is informed (the first user will be the webmaster, who must solve the problem).
        $cf = fopen($file, "w") or die("Error: file does not exits, and it can not be create.<BR>Please check permissions in the directory or create a file with coresponding name.");
        fputs($cf, "Mailing list subscribers\n");
        fclose($cf);
}

// IF REQUEST HAS BEEN TO SUBSCRIBE FROM MAILING LIST, ADD EMAIL TO THE FILE
if ($action=="subc"){
        // check whether the email is already registered
        if(strpos($file_content,"<$email>")>0){die("Error: your email is already included in this mailing list");}
        // write the email to the list (append it to the file)
        $cf = fopen($file, "a");
        fputs($cf, "\n<$email>");       // new email is written to the file in a new line
        fclose($cf);
        // notify subscription
        print "Your email has been added to our mailing list.<br>Thanks for joining us.";
}
// IF REQUEST HAS BEEN TO UNSUBSCRIBE FROM MAILING LIST, REMOVE EMAIL FROM THE FILE
if ($action=="unsubc"){
        // if email is not in the list, display error
        if(strpos($file_content,"<$email>")==0){die("Error: your email is not included in this mailing list");}
        // remove email from the content of the file
        $file_content=preg_replace ("/\n<$email>/","",$file_content);
        // print the new content to the file
        $cf = fopen($file, "w");
        fputs($cf, $file_content);
        fclose($cf);
        // notify unsubscription
        print "Your email has been removed from our mailing list. ";
}

?>
</body>
</html>



<?
// THIS FUNCTION WILL CHECK WHETHER AN EMAIL IS CORRECT OR NOT
//      FIRST, BASIC ARCHITECTURE IS CHECKED
//      THEM, EXISTANCE OF THE EMAIL SERVER IS CHECKED
// If email is not correct, the error message is shown and page dies
function check_email ($email, $message){
        // check if email exists
           if ($email==""){die($message);}
        // check whether email is correct (basic checking)
           $test1=strpos($email, "@");                                     //value must be >1
           $test2=strpos(substr($email,strpos($email,"@")), ".");          //value must be >1
           $test3=strlen($email);                                          //value must be >6
           $test4=substr_count ($email,"@");                               //value must be 1
           if ($test1<2 or $test2<2 or $test3<7 or $test4!=1){die($message);}
        // check whether email is correct (advance checking)
           // extracts whatever is after "@" to variable $email_server
           $email_server=substr($email,strpos($email, "@")+1);
}

// THIS FUNCTION WILL SHOW THE FORM
// MODIFY IT AS REQUIRED
function print_form(){
?>

        <html>
        <head>
                <title>My mailing list</title>
        </head>
        <body style="background-color: rgb(255, 255, 255);">
        <center>
        <p>&nbsp;<p>&nbsp;
        <form action="<? $PHP_SELF; ?>" method="post">
          <table>
             <tr>
             <td>
                <div style="text-align: center;">
                        <big style="font-weight: bold;">Join my mailing list </big><br>
                        </div>
                        &nbsp;<input name="email" size="30" type="text"> <br>
                        <div style="text-align: center;">
                        <input name="action" value="subc" checked="checked" type="radio">Subscribe
                        <input name="action" value="unsubc" selected="" type="radio">Unsubscribe
                        <br>
                </div>
                <div style="text-align: center;">
                        <input value="Submit" type="submit">
                </div>
              </td>
              </tr>
          </table>
        </form>
        <!-- you may remove next line, but consider maintaining it -->
        <font size="2">Script provided by <a href="http://www.phptutorial.info/">PhpTutorial.info</a>.
        </font></center>
        </body>
        </html>

<?
} // the function finishes here
?>
 
Ahh nevermind, after some more googling it seems that you can only use <? instead of <?php if you have shortcuts enabled. I changed them all to <?php and now it works fine! Thanks for your help.
 
Ok, I've modified the script to include 2 different subscribe options, the value of which is included with the email in the txt file separated by a comma.

This is my modified script:
PHP:
// lets try to get the content of the file
if (file_exists($file)){
        // If the file is already in the server, its content is pasted to variable $file_content
        $file_content=file_get_contents($file);
}else{
        // If the file does not exists, lets try to create it
        //   In case file can not be created (probably due to problems with directory permissions),
        //   the users is informed (the first user will be the webmaster, who must solve the problem).
        $cf = fopen($file, "w") or die("Error: file does not exits, and it can not be create.<br />Please check permissions in the directory or create a file with coresponding name.");
        fputs($cf, "Mailing list subscribers" . "\n");
        fclose($cf);
}

// IF REQUEST HAS BEEN TO SUBSCRIBE (WITH TYPE=INDUSTRY) TO MAILING LIST, ADD EMAIL TO THE FILE
if ($type=="industry"){
        // check whether the email is already registered
        if(strpos($file_content,"$email")>0){die("Error: your email is already included in this mailing list");}
        // write the email to the list (append it to the file)
        $cf = fopen($file, "a");
        fputs($cf, "\n" . "$email" . ',' . "$type");       // new email is written to the file in a new line
        fclose($cf);
        // notify subscription
        print "Your email has been added to our mailing list.<br />Thanks for joining us.";
}

// IF REQUEST HAS BEEN TO SUBSCRIBE (WITH TYPE=ACADEMIA) TO MAILING LIST, ADD EMAIL TO THE FILE
if ($type=="academia"){
        // check whether the email is already registered
        if(strpos($file_content,"$email")>0){die("Error: your email is already included in this mailing list");}
        // write the email to the list (append it to the file)
        $cf = fopen($file, "a");
        fputs($cf, "\n" . "$email" . ',' . "$type");       // new email is written to the file in a new line
        fclose($cf);
        // notify subscription
        print "Your email has been added to our mailing list.<br />Thanks for joining us.";
}

// IF REQUEST HAS BEEN TO UNSUBSCRIBE FROM MAILING LIST, REMOVE EMAIL FROM THE FILE
if ($type=="unsub"){
        // if email is not in the list, display error
        if(strpos($file_content,"$email")==0){die("Error: your email is not included in this mailing list");}
        // remove email from the content of the file
		$file_content=preg_replace ("\n" . "$email" . ',' . "$type","",$file_content);
        // print the new content to the file
        $cf = fopen($file, "w");
        fputs($cf, $file_content);
        fclose($cf);
        // notify unsubscription
        print "Your email has been removed from our mailing list. ";
}

Subscribing works ok but when I try unsubscribing it erases the entire text file instead of just the line with the specified email on.
Can someone tell me the correct way to write the unsubscribe part?
 
Oh yea :p. Instead of stating the $type in the preg_replace line is it possible to tell the script to overwrite the entire line based on just the $email, some kind of wildcard, up to the next linebreak? Would doing that work?
 
Thanks but it seems easier to just use a regex, this one seems to work ok so far..:
Code:
$file_content=preg_replace ("/\n$email,(industry|academia)/","",$file_content);
 
Back
Top Bottom