Hi, I want to collect NAME and EMAIL ADDRESS of my website visitors. I'm looking for a simple php script for this job. Could you please give me guidance? Thanks.
Hi, yes, there are many newsletter scripts with this feature. but I'm just looking for a simple script which only receive NAME and EMAIL ADDRESS then save them in a file (not database) Thanks. Hi, I'm looking for a free script! Thanks.
I just rolled this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>Simple e-mail subscription</title> <style type="text/css" media="screen"> body { font-family: verdana, sans-serif; color: #000; background-color: #fff; } </style> </head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Name:</span> <input name="name" type="text" size="20"> e-mail address:</span> <input name="email" type="text" size="20"> <input type="submit" value="Subscribe"> </form> <?php $subscriberfile = "emails.csv"; if (array_key_exists('email', $_POST) && $_POST['email']) { $subscribers = file($subscriberfile); $emails = array(); foreach ($subscribers as $subscriber) { list($email, $name) = split(",",trim($subscriber)); $emails[$email] = $name; } if (!check_email_address($_POST['email'])) { echo '<p>Your email address <b>' .$_POST['email']. '</b> does not look right. Please check your spelling and try again.</p>'; } elseif (array_key_exists($_POST['email'], $emails)) { echo '<p>The email address <b>' .$_POST['email']. '</b> is already subscribed. Thanks again for your interest.</p>'; } else { $emails[$_POST['email']] = $_POST['name']; $f = fopen($subscriberfile, 'w'); foreach ($emails as $key => $value) { $value = ereg_replace("[^A-Za-z0-9' ]", "", $value); fwrite($f, "$key,$value\n"); } fclose($f); echo '<p>Thank you. <b>' .$_POST['email']. '</b> has been subscribed.</p>'; } } ?> </body> </html> <?php function check_email_address($email) { // First, we check that there's one @ symbol, and that the lengths are right if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. return false; } // Split it into sections to make life easier $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) { if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { return false; } } if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) { return false; // Not enough parts to domain } for ($i = 0; $i < sizeof($domain_array); $i++) { if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { return false; } } } return true; } ?> PHP: It writes the submitted email and name to a file in comma separated format (email,name). Email is required, name is not. Email is checked to make sure it looks like an email address. As it is this script expects to write to a file "emails.csv" in the same directory as itself. You should put this file outside your document root and alter the $subscriberfile value to point to its location. Having said all that if you are doing anything other than the most trivial task you would almost certainly be better off with phplist (http://www.phplist.com/) - its a good quality free (open source) newsletter manager.
how can i edit this code, so that the php code is in a separate file than the html form? i want to have the php code that shows the 'emails.csv' file location in another file rather than having to put the code on each page of my website. thank you!