Monday, October 02, 2006

Beginner PHP programming tips

I'm going to list a few disparate things I've come across that I've found useful in beginning php programming. This article should be useful for people with a background in programming but with little php experience.

First off, php is a scripting language. Its free, open-source, integrates easily with apache (the web's most common web server), and is supported on many web hosts. After installing php (see this earlier post), I started with this good introductory article to get me started and familiar with the syntax etc.

Sending mail with php

Sending mail with php is easy, via the mail() function. However, if your mailserver requires SMTP authentication (and it probably does) - then this won't work. For this, try the free PHPmailer. I got up and running with this by simply following installation instructions and modifying the example on the sourceforge project page. You can also send HTML formatted mail, add attachments etc.

Validating (and remembering) checkbox input

I have a page on my site (how to tell if you are part squid) with a number of checkboxes. One thing I wanted to do was ensure that all the boxes were checked (either yes/no) when the user submitted (or else show an error message). Testing whether they are checked is easy enough, but here is the nice code I found to save the settings for all the checkboxes that were checked, so the user doesn't lose that data if they have to resubmit:








Passing variables between pages

I'm no expert on this one. However, I wanted to get some information from a form, process it, then pass it on to another php page. I believe that the POST and GET methods can be used for passing information from a form, but not for passing variables that you have defined yourself(?). A way I found to do this is to use the session_start function. The documentation and information I could find about this was a little confusing (I probably need to read a decent book) - but I did manage to get it working. Two important points - you need to call session_start on both (or all) pages you want to use session variables, and you must call it before your html (or any header information like doc type).

See an example of session_start working at find out your squid name (I pass the user's squidname from the first page to fill in the text box on the second page).

Server side includes and php?

I've set my website up to use server-side includes (SSI), mainly for the menu and my adsense ads etc. SSI is very useful for content that is replicated on many pages - typically the banner or menu information. It means that this information can be stored in one seperate text file and included by the server in every page in which it is used, e.g.:

The normal way to configure a web server to look for SSI is to name the files with a .shtml extension (this means the server doesn't have to check every html file for SSI, which would be inefficient). But I would also like to use SSI in my php files - does anyone know if this is possible? If you ran your own webserver this would be easy enough, just configure it to also check .php files for SSI. But you dont have this option if your site is hosted by someone else, so I'm not sure if this can be done.

5 Comments:

Anonymous Anonymous said...

gordon i would like to get a browser link for an already configured php mailer or i would like if we chatted about how to configure one on my yahoo messenger, my email is v2byours@yah . please could you contact me?

8:56 PM  
Anonymous Anonymous said...

that is v2byours@yahoo.co.uk

8:56 PM  
Blogger Gordon Simpson said...

BTW - I believe the way to enable server side includes in php is to use the php include() or require() functions. Will be testing this shortly.

2:10 PM  
Anonymous Anonymous said...

hosting web search

Here's some useful info on hosting web option
which you might be looking for. The url is: http://www.jaldisearch.com/

3:43 AM  
Anonymous Anonymous said...

Hey great tips friend. PHP is fairly easy to get started with if you have very little prior programming experience

10:57 AM  

Post a Comment

<< Home