Flash 5 Tutorial – Part 4 – Symbols & Animation the standard drawing tools in Flash

June 9, 2010 by  
Filed under Flash 5 Tutorial

Introduction

Over the last few parts you have learnt how to use most of the standard drawing tools in Flash. In this part of the tutorial I will show you how to create your first Flash movie with animation.

Symbols

In order to animate something in Flash it must first be changed into a Symbol. There are three types of symbol: Graphic, Button and Movie. In this part of the tutorial I will just deal with Graphics.

To start, draw a filled circle in the middle of the screen, a few centimetres high. Choose the arrow tool and double click on the circle to select it and the line around it. Then press F8 on the keyboard. You will get a window called Symbol Properties. In this window you can give a name to your symbol so that you can refer to it later. Type ‘Circle’ (without the quotes) in the box and then select Graphic and click OK.

You will now notice that the circle apears with a blue line around it. The next thing you will want to do is to animate this circle.

The Timeline

To create animation in flash you must use the timeline:

The timeline window shows all the frames that make up your animation and all the layers (which will be covered later). Each small box in the timeline is a frame. The animation runs at 12 frames per second (shown at the bottom) as standard but this can be changed. As you can see above, there is a black dot in the first frame. This signifies that it is a keyframe.

Keyframes

Keyframes are very important in flash as they are used whenever something is changed. For instance if you wanted the circle to appear in another position later in the movie you would create a keyframe in the frame where you want it to change and then you could move the circle without affecting the rest of the movie. That is exactly what you are going to do now.

Right click in frame 50 on the timeline and choose Insert Keyframe. This will insert a new keyframe into the animation at frame 50 and it will contain the same information as the previous keyframe. You could have also chosen Blank Keyframe which will make a new blank keyframe but you want the circle to be in both keyframes in your movie.

Now, click in frame one and press Enter to play the movie. As you can see you now have a 4.1 second long movie of a circle in the middle of the screen – not very interesting.

To make something happen you will need to change the second keyframe. Click on it (frame 50) and the symbol of the circle will be selected. Now, with the arrow tool, click and drag the circle to the upper left hand corner of the stage. Then click in frame one again and press Enter to play the movie.

Animation

The movie you have created now has a circle which moves on the screen but, as you will have noticed, it stays in the same place and then suddenly moves in the last frame. Animations, like television and film, are made up many frames, each of which has a slight change from the last one. As they are played very fast (12 frames per second in flash) the object looks like it is moving. Luckily, flash has been built so that you don’t have to do all of this manually.

Acutally, animating the circle on the screen is amazingly easy because of the Flash feature called Motion Tweening. This feature will automatically create all the frames to go between two keyframes to animate an object which you have moved (in this case the circle). All you have to do is right click in any frame between your two keyframes and choose Create Motion Tween.

Once you have done this the frames will change from being grey to being blue with an arrow across them. This signifies a motion tween. Click in frame one and press Enter to view your movie. As you can see, now flash has made your circle move smoothly accross the screen and, if you click i
n the frames between your keyframes you will see that it has created all the frames in between.

Scaling

Motion Tweens can be used for other things as well as moving objects. You can also change their size. For this you will use the scale tool. Right click inn frame 80 and create a new keyframe.Your circle will be selected. Now choose the Scale tool:

from the Options section on the tools pallette (if it is not available make sure you have the black pointer tool selected). This tool allows you to change the size of objects. 6 white boxes will appear at the edges of the circle, just like in any other image application. Use the bottom right hand one to drag the circle size until it is considerably larger. You will also notice that the circle grows equally around its centre point. Now, as before, right click in between frames 50 and 80 and choose Create Motion Tween.

Flash 5 Tutorial – Part 3 – More Tools the basic drawing tools in Flash

June 9, 2010 by  
Filed under Flash 5 Tutorial

Introduction

In the last part of the tutorial I showed you some of the basic drawing tools in Flash. Before I show you how to create animations in Flash, I will first explain some more of the tools available to you. These tools are not as important as the ones I showed you last week but, to create good animations, it is useful to know how to use all the tools.

Ink Bottle

The ink bottle tool is quite useful. If you have a block of colour (for example one created with the paintbrush – which will be covered later in this tutorial) you can click on it to add a line round the edge. The colour of the line placed round the edge will be the same as the colour selected as line colour on the tools bar.

Dropper Tool

The dropper tool, like the ones in graphics software, is used to pick a colour off one part of the screen and use it as the fill or line colour. If you click on a fill, the fill colour will be set and the pointer will change to the fill tool. If you click on a line, the line colour will be set and the pointer will change to the ink bottle tool.

Eraser Tool

The eraser tool is used to rub things out on the stage. It is used by just clicking and holding down the mouse. It does have a few options though:

The large section at the bottom is used to select the size and shape of the eraser brush. On the right at the top is the faucet tool. This makes the eraser work like the fill tool – you just have to click once to remove the fill from an area. The part on the left is the special option. This allows you to choose the type of eraser you use. The default is Erase Normal, which works like a normal eraser tool. Some of the other options are:

  • Erase Fills which will leave all lines intact as you use the eraser but will rub out fills
  • Erase Lines which leaves fills but rubs out lines
  • Erase Inside which allows you to erase inside a shape without harming the edges

Paintbrush Tool

The paintbrush tool will paint lines all the time you have the mouse button held down. It has some options, though, which are like the eraser tool options:

  • Paint fills, will only paint inside a shape
  • Paint Behind, which will paint behind all shapes and fills

Pencil Tool

The pencil tool allows you to draw lines on the screen. It is different to a normal freehand tool, though, as it has three options which can be set:

This option will allow you to choose what flash does to your line once you have drawn it. The default option is Straighten. Flash will straighten out your line so that any part of it that is almost straight will be made into a straight line. The two other options are smooth and ink. Smooth will change your line so that it is smoothed out to make curves. The ink option will not make so many changes to your line but will still smooth it a bit and straighten some lines.

The Text Tool

The text tool is used for adding text to your flash movies. Just click on the stage where you want the text to appear and a cursor will appear, where you can type in your text. To make chan
ges you need to use the character palette:

The options here are quite self explanitary. The font section lets you choose the font for the text. With flash all fonts are embeded in the file so the user does not need to have the font on their computer. Below that is the size box which lets you choose the text size. Next to that are the normal bold, italic and colour options. Below that is the Tracking option. This allows you to choose how spaced out the characters are. At 0 the spacing is normal. If you increase this, spaces will be left between your characters. If you decrese it, your characters will start to overlap. The option below this is the superscript/subscript option and below this you can specify a URL for a link.

Flash 5 Tutorial – Part 2 – Drawing to draw basic shapes in Flash

June 9, 2010 by  
Filed under Flash 5 Tutorial

Introduction

In the last part I showed you the Flash interface and explained exactly what Flash is. In this part I will show you how to use Flash’s drawing and painting tools to put something into your animation. At this point there will be no interactivity and nothing will move but once you know the basics you can develop this futher.

Basic Drawing

The first thing you need to learn how to do is to draw basic shapes in Flash. We will start with the most basic shape, the circle/oval. Before you start you might want to move some of the floating pallettes so that you can see enough of the stage to work on.

Firstly, choose the Oval tool from the Tools bar on the left:

Then, draw the oval or circle you want on the stage (just as you would in a normal graphics program). Holding down shift will force the object drawn to be a circle. Once you have drawn your circle you will now notice a few things about it. Firstly, there is a line around the circle and a fill inside it. These are changed using the colors section of the Tools bar:

The first option (set at black in this example) is the line colour and the bottom option (blue in this example) is the fill colour. To change them click on the box filled with colour. You will get a menu with a selection of colours. If you want to match the colour to a colour on your screen click on anything on the screen and its colour will be used.

Now, try drawing another circle or oval, but this time change the fill and line colours.

The next tool you will use is the rectangle tool:

As with the circle tool, this works in exactly the same way as in any standard grapics program, you can drag out a rectangle you want to draw and hold down shift to force a square. Like the circle, the rectangle’s line and fill colours are set using the colors section of the Tools bar.

The rectange tool, unlike the oval, has some options which can be set. These appear in the Options section of the Tools bar:

There is only one option for the rectange, the Round Rectangle Radius. This creates rectangles with rounded corners like this:

Click on the option. A box appears asking for the radius in pixels. The example above used a radius of 20 pixels but it is best to experiment to find the best setting for what you want. After you have set this draw another rectangle. This one should have rounded corners. Remember: Flash will remember this setting so before you draw another standard rectange you should set it back to 0.

Like any other drawing program, Flash also has a straight line too:

This works in exactly the same way as the line tool in graphics software.

The objects you have drawn can also be moved. You must be careful, though, as Flash treats the fill and the line separately, so to move both together you must double-click in the object before dragging it if you want to move them both.

Drawing Or Painting?

Flash is a very strange program for editing grapics. It is really a cross between a drawing and a painting program with some special features of its own. It is like a drawing program because you can move and change objects once you have drawn them but it acts like a painting program with fills etc. For example if you drew two circles, a large one with a black line and a blue fill and a small one with a black line and no fill, then moved
the small one on top of the bigger one. You could then use the fill tool to fill in the middle of the small circle, changing only part of the big one.

Flash also allows you to break up graphics using other ones. If you now moved the small circle away from the larger circle it would leave a white space behind where it used to be. This feature can be extremely useful.

Special Fills

AS well as filling a shape with a single colour you can also use Flash’s premade fills. They are found at the bottom of the fill box. There are three gradient fills but the best are the radial fills. These allow you to fill in any shape with a radial gradient (going from light to dark). The special thing about them is that the lightest part will be where you click your mouse when using the fill option:

so you can very effectively make 3D looking circles, which are excellent for buttons.

Flash 5 Tutorial – Part 1 – Introduction multimedia on web pages,

June 9, 2010 by  
Filed under Flash 5 Tutorial

Introduction

Flash is one of the most popular technologies on the internet, with thousands of websites using it for introductions, animations and advertisements. Although many people feel that these animations are sometimes unnecessary, Flash has created a way of including multimedia on web pages, which will run over a standard internet connections. The recent release, Flash 5, has brought many changes to the creation of Flash animations. Many of the techniques covered in this tutorial will also apply to past versions of Flash, as well as Flash MX, the very latest version, though. If you are not sure what a Flash animation is like click here for an example. If this does not work you will need to download the Flash Plugin.

Why Use Flash?

Flash is one of the best multimedia formats on the internet today for several reasons. Firstly, the Flash plugin (required to view the animations) is installed on nearly every computer connected to the internet. All the major browsers come with it installed by default and, for those who don’t have it, the download is very small. Secondly, Flash is a ‘vector based’ program, which means the animations and graphics created by it have much smaller file sizes than a video or streaming media version of the same animation would be. You can also include sound, graphics and dynamically created information in your animation.

Flash provides a versatile and easy way for webmasters to create animations for their users.

What Do I Need?

As mentioned above, all you need to view a Flash animation is a modern web browser with the Flash plugin. To create Flash animations, though, you will need some software. Although there are other Flash creations on the market, Macromedia’s Flash 5, is by far the best and, as Macromedia created the Flash format, it always has the latest features in it. Unfortunately, the software costs $400, but you can download a free 30 day trial from Macromedia.

Once you have downloaded and installed the trial (or bought the software) you will be ready to start creating Flash animations.

The Flash Interface

When you first open Flash you will find an interface that looks something like this:

Flash 5 Interface

I will quickly cover the different aspects of this screen.

In the centre is the large white ‘Stage’. This is the actual movie where you will place all the objects you want to include in it.

Across the top of the screen is the timeline. This is where you insert all the actions that happen in your movie so that they happen at the correct times. It is split up into frames.

Down the left hand side of the screen is the ‘Tools’ pallette. This is where you will find all the tools for inserting objects and text into your animation.

There are also four floating pallettes on the screen. The ‘Mixer’ pallette allows you to choose the colours you will be using in your animation. It will change the colours of the currently selected object. The’ Info’ pallette will allow you to find out a bit of information about the object you have selected and will allow you to make changes to the properties of a tool you are using. The ‘Character’ pallette contains all the text formatting tools. Finally the ‘Instance’ pallette contains all the tools for changing objects when you are animating them, including sound and several other tools for making changes to your animation.

Each of the parts of the Flash window does many different things. Instead of going through each tool explaining what it does, I will show you examples and explain how to create them, showing you how to use each tool while doing so.

.htaccess Tutorial – Part 3 – Password Protection on websites

June 8, 2010 by  
Filed under .htaccess tutorials

Introduction

Although there are many uses of the .htaccess file, by far the most popular, and probably most useful, is being able to relaibly password protect directories on websites. Although JavaScript etc. can also be used to do this, only .htaccess has total security (as someone must know the password to get into the directory, there are no ‘back doors’)

The .htaccess File

Adding password protection to a directory using .htaccess takes two stages. The first part is to add the appropriate lines to your .htaccess file in the directory you would like to protect. Everything below this directory will be password protected:

AuthName “Section Name”
AuthType Basic
AuthUserFile /full/path/to/.htpasswd
Require valid-user

There are a few parts of this which you will need to change for your site. You should replace “Section Name” with the name of the part of the site you are protecting e.g. “Members Area”.

The /full/parth/to/.htpasswd should be changed to reflect the full server path to the .htpasswd file (more on this later). If you do not know what the full path to your webspace is, contact your system administrator for details.

The .htpasswd File

Password protecting a directory takes a little more work than any of the other .htaccess functions because you must also create a file to contain the usernames and passwords which are allowed to access the site. These should be placed in a file which (by default) should be called .htpasswd. Like the .htaccess file, this is a file with no name and an 8 letter extension. This can be placed anywhere within you website (as the passwords are encrypted) but it is advisable to store it outside the web root so that it is impossible to access it from the web.

Entering Usernames And Passwords

Once you have created your .htpasswd file (you can do this in a standard text editor) you must enter the usernames and passwords to access the site. They should be entered as follows:

username:password

where the password is the encrypted format of the password. To encrypt the password you will either need to use one of the premade scripts available on the web or write your own. There is a good username/password service at the KxS site which will allow you to enter the user name and password and will output it in the correct format.

For multiple users, just add extra lines to your .htpasswd file in the same format as the first. There are even scripts available for free which will manage the .htpasswd file and will allow automatic adding/removing of users etc.

Accessing The Site

When you try to access a site which has been protected by .htaccess your browser will pop up a standard username/password dialog box. If you don’t like this, there are certain scripts available which allow you to embed a username/password box in a website to do the authentication. You can also send the username and password (unencrypted) in the URL as follows:

http://username:password@www.website.com/directory/


Summary

.htaccess is one of the most useful files a webmaster can use. There are a wide variety of different uses for it which can save time and increase security on your website.

.htaccess Tutorial – Part 2 – .htaccess Commands and its useful features

June 8, 2010 by  
Filed under .htaccess tutorials

Introduction

In the last part I introduced you to .htaccess and some of its useful features. In this part I will show you how to use the .htaccess file to implement some of these.

Stop A Directory Index From Being Shown

Sometimes, for one reason or another, you will have no index file in your directory. This will, of course, mean that if someone types the directory name into their browser, a full listing of all the files in that directory will be shown. This could be a security risk for your site.

To prevent against this (without creating lots of new ‘index’ files, you can enter a command into your .htaccess file to stop the directory list from being shown:

Options -Indexes

Deny/Allow Certian IP Addresses

In some situations, you may want to only allow people with specific IP addresses to access your site (for example, only allowing people using a particular ISP to get into a certian directory) or you may want to ban certian IP addresses (for example, keeping disruptive memembers out of your message boards). Of course, this will only work if you know the IP addresses you want to ban and, as most people on the internet now have a dynamic IP address, so this is not always the best way to limit usage.

You can block an IP address by using:

deny from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will block a whole range.

You can allow an IP address by using:

allow from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will allow a whole range.

If you want to deny everyone from accessing a directory, you can use:

deny from all

but this will still allow scripts to use the files in the directory.

Alternative Index Files

You may not always want to use index.htm or index.html as your index file for a directory, for example if you are using PHP files in your site, you may want index.php to be the index file for a directory. You are not limited to ‘index’ files though. Using .htaccess you can set foofoo.blah to be your index file if you want to!

Alternate index files are entered in a list. The server will work from left to right, checking to see if each file exists, if none of them exisit it will display a directory listing (unless, of course, you have turned this off).

DirectoryIndex index.php index.php3 messagebrd.pl index.html index.htm

Redirection

One of the most useful functions of the .htaccess file is to redirect requests to different files, either on the same server, or on a completely different web site. It can be extremely useful if you change the name of one of your files but allow users to still find it. Another use (which I find very useful) is to redirect to a longer URL, for example in my newsletters I can use a very short URL for my affiliate links. The following can be done to redirect a specific file:

Redirect /location/from/root/file.ext http://www.othersite.com/new/file/location.xyz

In this above example, a file in the root directory called oldfile.html would be entered as:

/oldfile.html

and a file in the old subdirectory would be entered as:

/old/oldfile.html

You can also redirect whole directoires of your site using the .htaccess file, for example if you had a directory called olddirectory on your site and you had set up the same files on a new site at: http://www.newsite.com/newdirectory/ you could redirect all the files in that directory without having to specify each one:

Redirect /olddirectory http://www.newsite.com/newdirectory

Then, any request to your site below /olddirectory will bee redirected to the new site, with the
extra information in the URL added on, for example if someone typed in:

http://www.youroldsite.com/olddirecotry/oldfiles/images/image.gif


They would be redirected to:

http://www.newsite.com/newdirectory/oldfiles/images/image.gif


This can prove to be extremely powerful if used correctly.

.htaccess Tutorial – Part 1 – Introduction .htaccess file and its power

June 8, 2010 by  
Filed under .htaccess tutorials

Introduction

In this tutorial you will find out about the .htaccess file and the power it has to improve your website. Although .htaccess is only a file, it can change settings on the servers and allow you to do many different things, the most popular being able to have your own custom 404 error pages. .htaccess isn’t difficult to use and is really just made up of a few simple instructions in a text file.

Will My Host Support It?

This is probably the hardest question to give a simple answer to. Many hosts support .htaccess but don’t actually publicise it and many other hosts have the capability but do not allow their users to have a .htaccess file. As a general rule, if your server runs Unix or Linux, or any version of the Apache web server it will support .htaccess, although your host may not allow you to use it.

A good sign of whether your host allows .htaccess files is if they support password protection of folders. To do this they will need to offer .htaccess (although in a few cases they will offer password protection but not let you use .htaccess). The best thing to do if you are unsure is to either upload your own .htaccess file and see if it works or e-mail your web host and ask them.

What Can I Do?

You may be wondering what .htaccess can do, or you may have read about some of its uses but don’t realise how many things you can actually do with it.

There is a huge range of things .htaccess can do including: password protecting folders, redirecting users automatically, custom error pages, changing your file extensions, banning users with certian IP addresses, only allowing users with certain IP addresses, stopping directory listings and using a different file as the index file.

Creating A .htaccess File

Creating a .htaccess file may cause you a few problems. Writing the file is easy, you just need enter the appropriate code into a text editor (like notepad). You may run into problems with saving the file. Because .htaccess is a strange file name (the file actually has no name but a 8 letter file extension) it may not be accepted on certain systems (e.g. Windows 3.1). With most operating systems, though, all you need to do is to save the file by entering the name as:

“.htaccess”

(including the quotes). If this doesn’t work, you will need to name it something else (e.g. htaccess.txt) and then upload it to the server. Once you have uploaded the file you can then rename it using an FTP program.

Warning

Before beginning using .htaccess, I should give you one warning. Although using .htaccess on your server is extremely unlikely to cause you any problems (if something is wrong it simply won’t work), you should be wary if you are using the Microsoft FrontPage Extensions. The FrontPage extensions use the .htaccess file so you should not really edit it to add your own information. If you do want to (this is not recommended, but possible) you should download the .htaccess file from your server first (if it exists) and then add your code to the beginning.

Custom Error Pages

The first use of the .htaccess file which I will cover is custom error pages. These will allow you to have your own, personal error pages (for example when a file is not found) instead of using your host’s error pages or having no page. This will make your site seem much more professional in the unlikely event of an error. It will also allow you to create scripts to notify you if there is an error (for example I use a PHP script on Free Webmaster Help to automatically e-mail me when a page is not found).

You can use custom error pages for any error as long as you know its number (like 404 for page not found) by adding the following to your .htaccess file:

ErrorDocument errornumber /file.html

For example if I had the file notfound.html in the root direct
ory of my site and I wanted to use it for a 404 error I would use:

ErrorDocument 404 /notfound.html

If the file is not in the root directory of your site, you just need to put the path to it:

ErrorDocument 500 /errorpages/500.html

These are some of the most common errors:

401 – Authorization Required
400 – Bad request
403 – Forbidden
500 – Internal Server Error
404 – Wrong page

Then, all you need to do is to create a file to display when the error happens and upload it and the .htaccess file.

PHP Tutorial – Part 7 – Final Notes Print, Echo and HTML

May 2, 2010 by  
Filed under PHP tutorials

Introduction

In the past 6 parts of this tutorial I have shown you the basics of writing PHP. In this final part I will show you a few small things which don’t really warrant a section of their own.

Comments

As with any programming language, it is quite important to comment in your script. If you are working on a script with someone else you must let them know what you code does and if you are distributing your script you will need to show people how to edit it. Even if you are the only one who will use your script it is useful to comment so that you can edit it at a later date.

In PHP there are two ways you can comment. One way is used for single line comments and the other is used mainly for comments that go over one line. A single line comment is written as follows:

// Your comment can go in here

Everything after the // will be ingnored when the script is executed. You can even place these on the end of another line e.g.

print “Hello $name”; // Welcome to the user

Another way of commenting is by using multi-line comments:

/* The following piece of code will take the input
the user gave and will check that it is valid before
adding it to the database */

Anything between the /* and the */ will be ignored. It is important that you always close this type of comment as not doing so could make your script not work.

Print, Echo and HTML

As you may have noticed during this tutorial I have actually used 4 different ways of outputting information to the browser:

echo(“Text here”);
echo “Text here”;
print(“Text here”;
print “Text here”;

To clarify, all of these do the same thing and you can use any or all of them in a script. There is no reason to even use the same type all through a script. The only problem you may find is that, as I explained in part 2, all the ” in the HTML code must be replaced with \” which, if you have a lot of code, could take a very long time. This brings me to a very useful part of PHP. If, for example, you created the header of a page dynamically in PHP, then had the static page and finally a dynamic footer you can do the following:

HTML Code

This gets even better as the PHP code will just continue from where it was left off so you could do the following:

HTML For IF Being Correct

HTML For IF Being Wrong

You must always remember to close IF statements and loops, though, as it is very easy to forget.

One Line Prints

Being able to place HTML code into your PHP is very useful, but what happens if you want to put the value of a variable into the code. Unlike when using an echo or print statement, you can’t just put in the variable name as this section is not actually part of the PHP code. Instead you must just put in a little PHP.

For example if you wanted to print someone’s name from a script with HTML formatting you would do the following:

In the above code you have just added in the following PHP:

Which is exactly the same as the following PHP code:

But all put onto one line.

Conclusion

This tutorial has given you some of the basics of PHP and should allow you to do most things you will want to. For a much more in depth look you should visit PHP.net, the official homepage of PHP. One major omission of this tutorial, you may have noticed, is using PHP with a database. As this is one of the major reasons that people use PHP and because there are many options
I will put this in a separate PHP/MySQL tutorial.

PHP Tutorial – Part 6 – How to work in PHP With Forms Data

May 2, 2010 by  
Filed under PHP tutorials

Introduction

In the last part, I showed you how to use PHP to send e-mail messages using a script. In this part I will continue this and also show you how to use PHP and forms together to make your PHP scripts useful.

Setting Up Your Form

Setting up a form for use with a PHP script is exactly the same as normal in HTML. As this is a PHP tutorial I will not go into depth in how to write your form but I will show you three of the main pieces of code you must know:

Will display a text input box with Your Name written in it as default. The value section of this code is optional. The information defined by name will be the name of this text box and should be unique.

Will display a large scrolling text box with the text ‘Please write your message here.’ as default. Again, the name is defined and should be unique.

This will create a submit button for your form. You can change what it says on the button by changing the button’s value.

All the elements for your form must be enclosed in the

tags. They are used as follows:

Form elements and formatting etc.

The form’s action tells it what script to send its data to (in this case its process.php). This can also be a full URL (e.g. http://www.mysite.com/scripts/private/processors/process.php). The method tells the form how to submit its data. POST will send the data in a data stream to the script when it is requested. GET is the other option. GET will send the form data in the form of the url so it would appear after a question mark e.g. http://www.mysite.com/process.php?name=david

It really makes no difference which system you use but it is normally better to use POST if you are using passwords or sensitive information as they should not be shown in the browser’s address bar.

Getting The Form Information

The next step is to get the data the form has submitted into your script so that you can do something with it. This is. There are basically two different methods of getting the data into PHP, which depend on how they were submitted. There are two submission methods, GET and POST, which can both be used by forms. The difference between the two is that using GET, the variables and data will be shown in the page address, but using POST it is invisible. The benefit of GET, though is that you can submit information to the script without a form, by simply editing the URL.

This works the same as submitting a form using GET. The advantage of this is that you can create links to your scripts which do different things depending on the link clicked. For example you could create a script which will show different pages depending on the link clicked:

yourpage.php?user=david
could show David’s page and:
yourpage.php?user=tom
could show Tom’s page, using the same script.

It is also possible to pass more than one piece of information to the script using this system by separating them with the & symbol:

yourpage.php?user=david&referrer=gowansnet&area=6

These could all be accessed separately using the GET variables user, referrer and area.

To get a variable which has been sent to a script using the POST method you use the following code:
$variablename=$_POST['variable'];
which basically takes the variable from the POST (the name of a form field) and assigns it to the variable $variablename.

Similarly, if you are using the GET method you should use the form:
$variablename=$_GET['variable'];

This should be done for each variable you wish to use from your form (or URL).

Creating The Form To Mail Script

To finish off this section, I will show you how to use what you have learnt in this part and the last to create a system which will e-mail a user’s comments to you.

Firstly, create this form for your HTML page:

Your Name:
E-mail:

Comments

This will make a simple form where the user can enter their e-mail address, their name and their comments. You can, of course, add extra parts to this form but remember to update the script too. Now create the PHP script:

Remember to replace php@gowansnet.com with your own e-mail address. This script should be saved as mail.php and both should be uploaded. Now, all you need to do is to fill in your comments form.

The first part of that script may look a bit strange:

function checkOK($field)
{
if (eregi(“\r”,$field) || eregi(“\n”,$field)){
die(“Invalid Input!”);
}
}

You don’t really need to worry about what this is doing, but basically, it stops spammers from using your form to send thier spam messages by checking special characters are not present in the input which can be used to trick the computer into sending messages to other addresses. It is a fuction which checks for these characters, and if they are found, stops running the script.

The lines:

checkOK($name);

etc. run this check on each input to ensure it is valid.

PHP Tutorial – Part 5 – How to Send E-mails With PHP by server

May 2, 2010 by  
Filed under PHP tutorials

Introduction

One of the major uses of a server side scripting language is to provide a way of sending e-mail from the server and, in particular, to take form input and output it to an e-mail address. In this part I will show you how to send e-mail messages using PHP.

The Mail Command

Mail is extremely easy to send from PHP, unlike using scripting languages which require special setup (like CGI). There is actually just one command, mail() for sending mail. It is used as follows:

mail($to,$subject,$body,$headers);

In this example I have used variables as they have descriptive names but you could also just place text in the mail command. Firstly, $to. This variable (or section of the command) contains the e-mail address to which the mail will be sent. $subject is the section for the subject of the e-mail and $body is the actual text of the e-mail.

The section $headers is used for any additional e-mail headers you may want to add. The most common use of this is for the From field of an e-mai but you can also include other headers like cc and bcc.

Sending An E-mail

Before sending your mail, if you are using variables, you must, of course, set up the variable content beforehand. Here is some simple code for sending a message:

$to = “php@gowansnet.com”;
$subject = “PHP Is Great”;
$body = “PHP is one of the best scripting languages around”;
$headers = “From: webmaster@gowansnet.com\n”;
mail($to,$subject,$body,$headers);
echo “Mail sent to $to”;

This code will acutally do two things. Firstly it will send a message to php@gowansnet.com with the subject ‘PHP Is Great’ and the text:

PHP is one of the best scripting languages around

and the e-mail will be from webmaster@gowansnet.com. It will also output the text:

Mail sent to php@gowansnet.com

to the browser.

Formatting E-mail

Something you may have noticed from the example is that the From line ended with \n. This is acutally a very important character when sending e-mail. It is the new line character and tells PHP to take a new line in an e-mail. It is very important that this is put in after each header you add so that your e-mail will follow the international standards and will be delivered.

The \n code can also be used in the body section of the e-mail to put line breaks in but should not be used in the subject or the To field.

Mail Without Variables

The e-mail above could have been sent using different variable names (it is the position of the variables in relation to the commas, not the name of them which decides on their use). It could also have been done on one line using text like this:

mail(“php@gowansnet.com”,”PHP Is Great”,”PHP is one of the best scripting languages around”,”From: webmaster@gowansnet.com\n”);

But that would make your code slightly harder to read.

Error Control

As anyone who has been scripting for a while will know, it is extremely easy to make mistakes in your code and it is also very easy to input an invalid e-mail address (especially if you are using your script for form to mail). Because of this, you can add in a small piece of code which will check if the e-mail is sent:

if(mail($to,$subject,$body,$headers)) {
echo “An e-mail was sent to $to with the subject: $subject”;
} else {
echo “There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid”;
}

This code is quite self explanitory. If the mail is sent successfully it will output a message to the browser telling the user, if not, it will display an error message with some suggestions for correcting the problem.

Next Page »