Autoresponders With PHP

Written by Robert Plank

First off, check outrepparttar URL below. You'll learn how to make that today. utorials/3/signup.html

Fill out your e-mail address onrepparttar 105088 page you see. (I promise it's not being saved anywhere.) Then, wait a minute or two and check your mail. You should get a message from Gumby ( containing a sample autoresponder message.

Today, we're going to learn three easy things: redirection, mail sending, and form submission.

When we finish with that, you will know how to put those components together and create an autoresponder. Because if you think about it, that's all an autoresponder does. Somebody enters in their e-mail address, are sent an e-mail message, and then are redirected to a new page.

Of course there are more complex autoresponders, like Gary Ambrose's Opt-In Lightning, or Wes Baylock's Mail Master Pro which handle multiple follow-ups and recordrepparttar 105089 e-mail addresses of those who have signed up forrepparttar 105090 responder. But today we're just going to focus on how to make a very basic, very simple autoresponder.

Hopefully, you've seen what form objects in HTML look like. Here's some code you can use for an example:

Enter Your E-Mail Address:

Copy and paste this code into a file called "signup.html" and upload it to your web server. You'll see a text box waiting for your visitor to enter his or her e-mail address so they can be sent that autoresponse message.

Of course,repparttar 105091 form won't work just yet because, if you look atrepparttar 105092 first line of that HTML code I gave you, you'll see thatrepparttar 105093 form submits to a script called "some-script.php". And we haven't made that just yet.

Look onrepparttar 105094 second line of "signup.html", atrepparttar 105095 last half ofrepparttar 105096 line. You should be familiar with HTML tags, but if you're not, an HTML tag consists of two parts:repparttar 105097 parent tag andrepparttar 105098 attributes.

The parent tag is simplyrepparttar 105099 tag's designation. For example, if you had a slice of HTML code that looked like this:

Thenrepparttar 105100 parent tag would be "font". The rest of what's enclosed inrepparttar 105101 tag tellsrepparttar 105102 browser what to do with it. For example, in this tagrepparttar 105103 attributes are thatrepparttar 105104 font should be Verdana with size 1.

Why am I telling you all this? Because it relates torepparttar 105105 HTML code you see in signup.html.

Now, when you look at this:

The code tellsrepparttar 105106 receiving browser that this is an "input" tag, meaning that it's a form field. The name of this item is "email" and its size is 30, meaning this text box should be 30 characters in width.

Whenrepparttar 105107 form is submitted, it takes allrepparttar 105108 values of allrepparttar 105109 fields inside that form and throws it at its destination. In this case, our destination is "some-script.php".

If you're lost, this will all make a whole lot more sense once you try this next step.

Make a file called "some-script.php" and paste this line of code into it:

Uploadrepparttar 105110 script inrepparttar 105111 same folder as signup.html, and go to "signup.html". Type your e-mail address in and clickrepparttar 105112 submit button.

You should see a new page containing just your e-mail address and nothing else.

Is this starting to make sense? You toldrepparttar 105113 PHP script to dumprepparttar 105114 contents ofrepparttar 105115 variable called "email" torepparttar 105116 screen, and you just submitted a form with a text box called "email".

If you want to try one more exercise like this, changerepparttar 105117 name ofrepparttar 105118 text box to, say, "goober" in signup.html and changerepparttar 105119 $email in some-script.php to $goober. Upload both, go to signup.html, and type anything intorepparttar 105120 text box. You'll getrepparttar 105121 same result.

This is how you'll pass data from forms (like text fields, drop down menus, radio buttons andrepparttar 105122 like) along intorepparttar 105123 PHP scripts you create.

We've just covered how to submit form elements into PHP. Now let's focus on sending mail.

PHP has a really simple function that uses whatever mail sending program is installed on your server to send messages torepparttar 105124 outside world. If you have a crappy web server, this step might not work and you'll have to use a different web host if you want to try this.

But if you're on a good web host that has PHP installed *correctly*, this shouldn't be a problem.

Up until now we haven't used functions in PHP too much, aside from simple things like include() and header(). Today's your lucky day, because functions work in a very similar way to HTML tags. You haverepparttar 105125 parent tag, andrepparttar 105126 attributes (or parameters).

The mail() function basically works like this:


Let's start off by sending a simple e-mail message to yourself. We won't need any special headers this time around, so this will be quick and painless. Copy this one line of code into "mailtest.php":

Password Protection and File Inclusion With PHP

Written by Robert Plank

First off, if you read last week's article by me (the one about site personalization in PHP), I have one addition to make to make your life a little easier. If you didn't read last week's article, read it. It'll help you. You can find it here: utorials/1

Now, remember how we personalized a page for your visitor? This works fine, but what do we do if they didn't use that special link, and just went torepparttar page?

What I'm saying is, if you special personalized page was at but your visitor only went to Instead ofrepparttar 105085 name there would just be a blank spot! Last week I forgot to cover this.

All we have to do to fix it is to tell PHP that if they didn't leave a name, to substitute one in for them. So let's say that if they left their first name blank to make their first name "Friend". This way instead of saying "Dear Oscar:" it would say "Dear Friend:".

Putrepparttar 105086 following line of code JUST ABOVE THE LINE that says something similar to: echo "$f $l" :

if ($f == "") { $f = "Friend"; }

That way, you can use your special personalized page as a normal page and no one will berepparttar 105087 wiser.

Password protection is something you need every once in a while. Whether it's a secret site you're running or justrepparttar 105088 control panel of your favorite script.

Sometimes you don't need a fancy solution like .htaccess if you're only worrying about a single user (you). But JavaScript passwords can be worked around, and HTML-based passwords based on cookies, written in PHP are complicated and take time to write. Htaccess is nice but it's a pain if you just want to use it for one person.

Here is a simple way to use HTTP authentication (the same you see used by htaccess) with just a few lines of code. Below arerepparttar 105089 sample contents of a file you can use.

$myusername = "myusername"; $mypassword = "mypassword"; $areaname = "My Protected Area";

if ($PHP_AUTH_USER == "" || $PHP_AUTH_PW == "" || $PHP_AUTH_USER != $myusername || $PHP_AUTH_PW != $mypassword) { header("HTTP/1.0 401 Unauthorized"); header("WWW-Authenticate: Basic realm="$areaname""); echo "

Authorization Required.

"; die(); }


my main text.

Last week we learned that PHP code can be integrated into your HTML. All you have to do is make surerepparttar 105090 file ends in .php (for example, "firehydrant.php") and it will work. Everything that comes in between this:

/* And this: */


Is treated as PHP code. Everything outside of those tags is treated as plain HTML.

When copying this code over be SURE to include that last line where it says "my main text." Note that "my main text" is located outside ofrepparttar 105091 PHP code brackets. This means that where you see "my main text" can be your normal HTML file!

Take all of this code and Uploadrepparttar 105092 script onto your web server and run it inrepparttar 105093 browser. You should be greeted by a password popup box similar to those you see with htaccess. Enter "myusername" asrepparttar 105094 username and "mypassword" asrepparttar 105095 password. You should be given a page that says "my main text" and nothing else.

Close your browser window (this is very important) and going back to that page. Try enteringrepparttar 105096 wrong info. The box will come up again. You have three tries and then are given that dreadful "Authorization Required" message.

Cont'd on page 2 ==> © 2005
Terms of Use