Recreate config.php file for phpbb forum board Tutorial
May 4, 2010 by technology
Filed under Phpbb Forums
It is always a good practice to backup your phpbb program folder and database regularly to avoid things like loosing any important files or data, such as the config.php file that contains the database connection info for your phpbb board, loosing this file basically takes down your phpbb board entirely because your phpbb program does not have the necessary connection info to access the database to process the forum web pages.
Luckily, recreating the config.php file is not that difficult, there are two options for you, one is through the dbinformer.php file that is found in the contrib folder in your phpbb program, the other method is to manually create the file with the basic settings.
recreating config.php using dbinformer.php
In the php installation process, it is advised to remove the install and contrib folder after the installation, because these two folders are no longer needed after the installation and may impose security risks to your phpbb forum if the two folders are being accessed by hackers.
Since the contrib folder has been removed in your live phpbb forum program folder, you can still obtain the dbinformer.php file from the phpbb Full package compressed file :-
1.download phpbb Full package
2.extract the compressed file and upload the contrib folder to your hosting account, place it in the phpbb root folder
3.enter http://yourdomain.com/phpbb/contrib/dbinformer.php in your browser window
4.enter the database access information (database name, user, password etc.) as requested in the screen
5.checked the box Generate config file and hit Submit
6.copy the generated code and save it to a file named config.php
7.Upload the new config.php file to your hosting account, place it in the phpbb root folder
8.Test accessing your forum again
Manually recreate config.php file
Manually recreating config.php requires the connection string format phpbb is using, if you have another phpbb forum, you can copy the config.php file over and manually change the information in it for connecting to the mysql database of this current phpbb forum, the config file content should look like :-
Below explain each of the options and what are the values available for each of the settings :-
$dbms
◦MySQL 3.x – ‘mysql’
◦MySQL 4.x and MySQL 5.x = ‘mysql4′
◦postgres = ‘postgres7′
◦MSSQL = ‘mssql’
◦MSSQL ODBC = ‘mssql-odbc’
◦oracle = ‘oracle’
◦msaccess = ‘msaccess’
If you are not sure what type of database you are using for you phpbb forum, login to your hosting account control panel, and you will see the database name and current version of your hosting account, or if you are still not sure, send a support ticket to your web hosting provider to check on it for you.
$dbhost
Depending on where you are hosting your phpbb database, if it is in the same server as your phpbb program, then enter localhost, otherwise enter the domain name or IP address of your remote server where you are hosting your pphbb database.
$dbname
This is the name of the database you have created to contain phpbb data, if you are hosting on a cpanel server, the format is usually username_dbname, you can login to your hosting account control panel, click into the SQLDB option and the databases, database username, you have created and assigned will be listed.
$dbuser
This is the database user name you have created and given access to the database entered in the $dbname above. The format is username_dbusr if you are on a typical cpanel hosting.
$dbpasswd
Pretty straight forward, the password you have set for the $dbuser above.
$table_prefix
This is the prefix you have given for phpbb table names creation during the installation process, this has to be a match with your current table names, otherwise phpbb program will not be able to locate tables in your database even you have entered the correct database login info above, if you have left this as default during installation, then enter phpbb_, you can double confirm this by login to hosting account, click into SQLdb option, click on phpMyAdmin option at the bottom of the screen, and select the phpbb database, phpMyAdmin will list out the tables available in the database, see if there is any prefix to the table names.
define
This indicates that this phpbb forum is installed (‘TRUE’) and should not be redirected to an install process, basically used by phpbb program internally to determine the state of your phpbb forum, no need to change anything and just leave it as it is in the config.php file.
After complete filling in the config.php file, save it and upload to the hosting account, place the file in phpbb root folder. You can now test accessing your forum.
Ask a doctor for your medical problems free
phpBB CHMOD file folder permissions to set proper file and folder permissions Tutorial
May 4, 2010 by technology
Filed under Phpbb Forums
CHMOD is a Linux command for setting file and folder permissions, it is important to set proper file and folder permissions for your phpbb board to avoid important files and data being exploited causing lost of the entire forum. Below list the files and folder in your phpbb forum and the permission level that should be set for each of them :-
•config.php: 666 before install, reset it back to 644 after install
•cache folder: 777
•images/avatars folder: 777
•all the other folders: 755
•all other files: 644
Note: above settings are suitable for servers where php is installed as an apache mod, without the phpsuexec installed. Where the userid executing all the php scripts is the userid that started the apache service. phpsuexec is an apache mod that will run php scripts using individual userids, minimizing the security issues and further tighten the security. There are however pros and cons of running phpsuexec and certain web hosting provider is not setting it as default install on servers.
If you are unsure of your current hosting server setup, you should check with your web host, or simply create a php script with the following code in it, call it on browser to check :-
After calling the script in browser, locate the Server API , if it says Apache then you do not have phpsuexec installed, if it says CGI then you server has php running with phpsuexec.
Tutorial: Converting a phpBB forum into an RSS feed using Python.
May 4, 2010 by technology
Filed under Phpbb Forums
RSS/Atom feeds are taking over the web – people like to keep up to date with the latest news, events, and ramblings, but they want to do so from a familiar, quick loading environment. A feed is just a page of dynamically generated content, in a markup form that a feed reader (which you use to read these feeds) can interperet. To generate your own RSS feed, you either need some form of feed generating program, or knowledge of some kind of programming language which has parsing/regular expression capabilities. Python is such an example of such a language, and this tutorial will show you how to make an RSS feed from any phpBB forum page using it.
First things first: We need to import the modules we will need to download a phpBB page, search for certain patterns, and spit the page back out in an XML format: urllib (a high level wrapper around socket and other modules, specifically for downloading over the net) and re (regular expressions)
#!/usr/bin/python
import urllib, re
# phpbb2rss.py – convert a phpBB board into an RSS feed
Then we need to send out a header to the web browser or rss reader, telling it what type of content we’re going to send over the net.
print “Content-Type: application/rss+xml”
print # blank line
Now we need to set up a few variables which we will use later on. The page we want to convert to a feed; the page title; the page description.
phpbb_page = “http://programmers-corner.com/forums/viewforum.php?f=11″
phpbb_pagetitle = “Python”
phpbb_pagedescr = “Python – Programmer’s Corner Python Support”
We need to start setting up the rss page next. We’ll store the following required information in a variable named rss:
rss = “”"
“”" + phpbb_page + “”"
“”" + phpbb_pagedescr + “”"
“”"
Now it starts to get tricker. We need to set up a regular expression to match links on the forum page that we know will point to topics. View the source of a phpBB forum page, and you’ll see that whenever there is a topic, its given to the browser as a link with the class “topictitle”. So we’re going to look for this:
reg = re.compile(“.+?“)
This basically says “compile a new expression into a regular expression object. This will match any piece of text that contains the characters , then 1 or more characters of anything, followed by the characters “. We then need to download the page with the url that was specified. We’re going to use urllib to do this.
page = urllib.urlopen(phpbb_page)
Now we want to get the domain name of the website the forum is residing on. To do so, we use a little regular expression trickery on the forum url we received.
phpbb_url = re.sub(“viewforum\.php.+?$”, “”, phpbb_page)
This is translated as “set the variable phpbb_url to the value of phpbb_page with all occurences of viewforum.php, followed by one or more occurance of anything up to the end of the string (signified by a dollar symbol), with nothing”
We have pretty much all the data we need, so we just need to loop through all the lines in the webpage we downloaded, look for our regular expression ‘reg’, and then extract the href part of the anchor tag and append it to our rss code, along with what we find in between the a tags.
for line in page.readlines():
# print “Finding regexp…”
if(reg.findall(line)):
url = reg.findall(line)[0]
title = url
url = re.sub(“<a href=\"", "", url)
url = re.sub("\".+$", "", url)
title = re.sub("“, “”, title)
title = re.sub(“$”, “”, title)
rss += ” \n”
rss += ” \n”
rss += ” ” + phpbb_url + url + “\n”
rss += ” \n”
We just extract the values we need from the line we’re scanning if we find a match to our regular expression ‘reg’, and appending them to the rss variable, with the proper tags specified (item, title and link).
Finally, we just close up some tags in our rss variable, and send it to stdout.
rss += ” \n”
rss += “”
print rss
If you couldn’t really care about making this program and are just happy that someone else has, you can used a slightly modified version utilizing mod_python which passes the page name and title (and optionally description) through the url. http:/⁄ceruleanwave.dotmod.net/phpbb2rss.py?p=&t=
For example, http:/⁄ceruleanwave.dotmod.net/phpbb2rss.py?p=http://www.programmers-corner.com/forums/viewforum.php?f=11&t=Python
I would put up a nice form for generating the feed, but as my host is moving datacenters _again_, I can’t at the moment.
Hope you enjoyed the tutorial. Full source below.
#!/usr/bin/python
import urllib, re, string
“”"
Convert an PHPBB forum page into an RSS feed.
“”"
print “Content-Type: application/rss+xml”
print
#
phpbb_page = “http://programmers-corner.com/forums/viewforum.php?f=1″
phpbb_pagetitle = “The Lounge”
phpbb_pagedescr = “The Lounge – discuss coding or anything else you want”
#
rss = “”"
“”" + phpbb_page + “”"
“”" + phpbb_pagedescr + “”"
“”"
reg = re.compile(“.+?“)
page = urllib.urlopen(phpbb_page)
phpbb_url = re.sub(“viewforum\.php.+?$”, “”, phpbb_page)
for line in page.readlines():
# print “Finding regexp…”
if(reg.findall(line)):
url = reg.findall(line)[0]
title = url
url = re.sub(“<a href=\"", "", url)
url = re.sub("\".+$", "", url)
title = re.sub("“, “”, title)
title = re.sub(“$”, “”, title)
rss += ” \n”
rss += ” \n”
rss += ” ” + phpbb_url + url + “\n”
rss += ” \n”
rss += ” \n”
rss += “”
print rss
phpBB install local language pack in your phpbb forum Tutorial
May 4, 2010 by technology
Filed under Phpbb Forums
Want to have a local language forum board ? no problem ! phpbb is a popular forum board software and there are complete sets of language packs for you to localize your forum board, and installing a language pack is simple too, just follow steps below :-
1.download the language pack of your choice and uncompress it
2.upload the expanded folder tree to your hosting account, place the entire folder into the language folder in your phpbb forum folder
3.login to your phpbb administration panel, under the general setting, set the default language of your board to your local language
4.Your phpbb is ready to speak your local language now.
Note: If you are not seeing the default language change to the local language just set when visiting the forum, try clear your PC cookies and revisit the forum again, there may be personal settings in the cookies saved in your PC that is interfering the change.
Please note that installing these language pack changes all the standard text displayed in phpbb (i.e. menu item, navigation descriptions, help text etc.), if you are using a custom style, you may need to make some changes to the image buttons or any other custom text in the style template to reflect the local language.
phpBB3: Open Source Forum Software Evolved phpbb forum
May 4, 2010 by technology
Filed under Phpbb Forums
The 13th of December, 2007 marked the beginning of another chapter of the success story that is open source software, as phpBB version 3 was released. phpBB, an open source bulletin board system, was created by James Atkinson in 2000 as a forum solution for his wife. From its low-key beginnings, phpBB has gone from strength to strength, earning itself a reputation as one of the “killer apps” for the PHP scripting language.
The bulletin board concept dates back to the 1980s, when the earliest forms took shape as newsgroups and primitive dial-up message boards. With the introduction of the Web, bulletin boards, now commonly known as internet forums, have become incredibly user friendly and customisable, and have played a key role in the current social networking trend. It should therefore come as no surprise that on the Internet today there are many millions of active forums, a significant number of which are phpBB installations.
phpBB was first released in April 2002, and has enjoyed constant evolution by an active developer community. In May 2007, the first of eight release candidates was made available to the public. The release candidate stage was lengthy, but ensured that the official release, phpBB v3.0.0, was of a very high standard.
Changes Between phpBB2 and phpBB3
To many people, phpBB2 will be remembered for its revolutionary theme, subSilver, whose combination of simple and slick allowed for a very attractive default theme. Considering it’s now nearly five years old, the design of the subSilver theme still holds up pretty well.
Change is inevitable, though, and perhaps the most noticeable difference that a user sees when comparing a forum that uses phpBB2 to one that uses phpBB3 will be the front-end code used by the board — especially if you dive under the hood. While the colour schemes between the new proSilver theme and subSilver are quite similar, proSilver has quite noticeably distanced itself from a table-based layout. Many people will be pleased to know that the new style is CSS driven, and is XHTML 1.0 Strict compliant. Tables have only been used when appropriate, for instance, in the display of tabular data such as statistics and the member list.
phpBB3 (codenamed Olympus) also includes many features requested by the phpBB community. Features that were only available as modifications in phpBB2 are now available as standard functionality.
Some of the more popular additions include those relating to:
•file attachments
•user and moderator control panels
•the ability to add unlimited layers of forums (subforums)
From an administrative perspective, the largest change comes in the permissions system. While the administration control panel has been completely revamped (it now uses a truly modular system), getting your head around the new permissions system is perhaps the most difficult part of upgrading. The new permission system allows for finer, more granular permission assignment, as well as many new permissions that administrators are able to assign. For easier management, permission roles are included, as is the ability to copy and transfer permissions from other forums. An in-depth overview of the new permissions system forms part of the phpBB documentation.
Notably absent from phpBB3 is an inbuilt modification (MOD) installer. However, as was the case with phpBB2, an official add-on will be published by the phpBB MOD Team for performing automatic MOD installations. Codenamed Blinky, the MOD installer is a modular addition to the administration control panel, which adds a new MODs tab to the administration navigation.
When installing a modification, the MOD installer will read and parse an XML file, storing information about the desired MOD to be installed. Various actions are performed on the basis of this XML file, such as adding, replacing, and removing code. This MOD installer is still under development, but the development team behind Blinky hopes to have something released soon.
The image below shows the details of the MOD installation, which only appear if enabled by the administrator, or if an error occurred.
Below, we see the flexibility in the MOD Manager. The code has three different methods to handle files, including FTP and the creation of a compressed archive.
Reinstall new clean phpbb and use back existing database
May 4, 2010 by technology
Filed under Phpbb Forums
Reinstall new clean phpbb and use back existing database
NOTE: you should make sure you are reinstalling the same version as your current phpbb forum version, otherwise you may risk loosing your database or data, as different phpbb version may introduce new changes to the database schema.
Backup database and phpbb folder
Before doing anything, backup your database and phpbb program folder, disable the forum board while you are working on this reinstallation to ensure there is no new data not captured in the backup.
Prepare phpbb files
1.create a new empty mysql database (this is for temporary keeping the empty tables created in this installation)
2.rename the current phpbb program folder in your hosting account to another name
3.Download phpbb core program files that is the exact version of your current phpbb forum.
4.uncompress the file and upload expanded folder to your hosting account, name it to the phpbb program folder you have been using.
5.enter http://www.yourdomain.com/phpbb/install/install.php to begin the installation
6.Follow the settings you have set in the old phpbb forum, BUT enter the newly created database name in the installation process, this is IMPORTANT otherwise you will mess up your phpbb database
7.Follow the installation process exactly like installing a new phpbb board till you have the board setup
8.install all MODs you have in the old phpbb forum, REMEMBER only install the exact same version otherwise you risk loosing your database
9.install style or language pack if you have those in your old phpbb forum
10.ensure everything are setup identical to the old phpbb forum board
11.When you have everything setup, copy the phpbb/images/avatars folder from old phpbb folder to your new phpbb folder, this folder contains forum user uploaded avatars
12.copy the config.php from old phpbb folder back to the new phpbb folder
13.You have now reinstalled your phpbb forum, you can delete the mysql database that was created temporary for this reinstallation.
After the reinstallation and your board is free from error while retrieving data from your database, you should check if there is any new updates for your current phpbb version, if there is, update your phpbb forum to ensure your board is up to date.
Build a phpBB Forum in 5 Steps on Your Web servers by yourself
May 4, 2010 by technology
Filed under Phpbb Forums
If you’re thinking of setting up a forum for your Website, you need not start from scratch. The open-source script, phpBB, can easily be installed on most Web servers. This article will describe how to do it right, from downloading through to customisation.
To use this tutorial, you need not know anything about PHP, and need only have had minimum experience with databases.
Introduction to phpBB
phpBB is a stable, open-source, bulletin board script available as a free download from phpbb.com. It allows you to set up an unlimited number of forums and categories. Users, moderators and user groups with varying permissions can be created. In short, it provides everything you’d expect of a bulletin board service. For a complete list of all supported features, see phpbb.com/features.
This software can easily be installed using FTP and a browser. It will run on any PHP-enabled server and requires any one of the following databases: MySQL, PostgreSQL, MS SQL or Access via ODBC.
It doesn’t matter whether your server is running IIS or Apache, though setup tends to be simpler on an Apache Web server. Any Website administrator should be able to install and configure phpBB in a minimum amount of time.
The administrator’s interface allows for ongoing maintenance, so that styles can be changed and topics added or removed. In fact, any administrative duties can be easily performed.
The installation instructions provided at the phpBB site are clear and straightforward. This article does not attempt to replace those instructions but to supplement them and to assist site administrators who are not familiar with PHP. It will help you determine whether you can run phpBB on your server, and if so, how to upload, install and configure it.
System Requirements
As phpBB is written using the server-side scripting language PHP, you need to know whether your Web server supports this language.
If you don’t know the answer to this question, you can always ask your Web host, or upload the following script to your server.
Use any text editor to create this file and save it as info.php. The actual name of the file is not as important as the extension, .php. In text mode, upload this file to the root directory on your server. View it by typing the address into your browser.
If your server does not support PHP, all you will see are the three lines of code shown above. In this case, get in touch with your Web host and see if PHP can be enabled for your domain.
If PHP is installed, the configuration settings of PHP will be displayed. Assuming that you have PHP enabled on your server, the info.php file will also be useful to help determine if you have the database support needed by phpBB.
Confirming database support for phpBB varies slightly depending upon the Web server you’re running. Let’s first look at Web servers that use Apache.
Apache/Linux
Most versions of PHP on Apache are compiled with support for MySQL databases. You can confirm this by looking again at the “info.php” file. The third line of this file should be the Configure Command. In this line, look for the words with-mysql.
PostgreSQL is not as widely supported, but you should be able to discover whether your server supports it in exactly the same way, but instead, looking for with-pgsql.
IIS/Windows
There will be no Configure Command available when PHP is run on a Windows platform, so we’ll take a slightly different approach to confirm database support here. Again viewing the info.php file, choose Find from your browser’s menu options and search for your database type.
For instance, if we searched for mssql to confirm support for MS SQL Server, we should see something similar to the text below:
In the same way, you can test for PostgreSQL using the search string pgsql, for MySQL using mysql, and for ODBC support using odbc.
Use the info.php file to determine which version of the database you have. You’ll need to know this when we begin the installation, and, with database and PHP support confirmed, we can now begin that process.
Upload The phpBB Files
Assuming you’ve downloaded and decompressed the files for phpBB, everything you need to upload should be in the directory called phpBB2. The only real requirement is that the directory structure be preserved.
Use your favourite FTP program to upload the files to your server. Make sure that all non-graphic files are transferred in ASCII mode. Most FTP programmes will do this automatically for you, based on the file extensions. Make sure your software is configured to transfer all files with the extensions .php, .tpl, .inc, .htm and .cfg as text files.
After the upload is complete, change the permissions on the config.php file so that they’re writeable by all. If you don’t know how to do this, don’t worry about it. The program will give you the option of saving a new config.php file and then overwriting the original.
Prepare for Installation
In preparation for setting up phpBB on your server, you’ll need the following information:
•Database Server Hostname/DSN
•Database Name
•Database Username
•Database Password
•Admin Email Address
Let’s discuss this information before we proceed. The host name will be the domain name at which your database is hosted. If your database is hosted on the same server as your Web server, all you need enter here is localhost. If it resides on another server, you can enter the appropriate domain name or the IP address. If instead you’re using ODBC to connect, you will need the connection name. If you are unsure about your situation, clarify the specifics with your Web host.
The database name is, quite simply, the name of the database you’re planning to use. You may use an existing database or create a new one. To create a new database, you may again need to speak with your Web host. If you’re using MySQL with phpMyAdmin, then it’s a relatively simple matter. If you’re using an existing database, table names will be created with a distinctive prefix so that the phpBB tables are readily identifiable.
The username and password need no explanation. The admin email is the email address to which you want questions relating to your bulletin board to be sent.
Installation
With all the information at hand, installation is as simple as pointing your browser towards the install.php file. If you uploaded the directory phpBB2 and its contents to the root directory of your server, the path you need is http://www./phpBB2/install/install.php.
Fill in the text boxes and press the Start Install button. Within seconds you should be finished — installation really is that simple. All the necessary database tables have been created for you. If you wish, have a look at them. There should be a total of 29 new tables — and we haven’t even had to think about SQL!
Shut down your browser, and don’t forget to delete the directories called contrib and install. phpBB won’t run until you do so. If, during the installation, you were asked to download the “config.php” file, you must now upload it to the phpBB2 directory, overwriting the existing file.
Administer Your Forum
To administer your Bulletin Board open http://www./phpBB2/index.php in your browser, and log in with the administrator name and password that you specified on installation. Then, click the Go to Administration Panel link at the bottom of the page. You’ll probably want to go to Forum Admin > Management first, to set up a new forum and categories.
Styles Admin is probably the next area you’ll want to look at, but before you do, go to the phpBB site and see what other styles you might like to download. Upon installation, the phpBB default style, subSilver, is the only one available, but any number of styles may be installed.
After downloading them from the phpBB site, you need to upload your new styles to the Templates directory, which is directly below the phpBB2 directory. After this, new styles must be installed from the Administration panel by going to Styles Admin > Add.
Once you’ve found a theme you like, you can further customise it from the administration panel by choosing Style Admin > Management > Edit. You can associate a theme with an entirely new Cascading Style Sheet (CSS), or you can edit the existing one.
Customise Your Forum
Once you’ve settled on a style, you will doubtless want to further customise it in ways that aren’t available through the Administration panel. For instance, you might want to display your own logo, rather than the phpBB logo.
If you have some knowledge of HTML, minor changes like this are not too difficult. Any changes you wish to make to the appearance of the header can be addressed by making changes to the file called overall_header.tpl. Since this file is included for every page within a style, your changes will take effect throughout the whole bulletin board.
Some other changes can be relatively easy to make. For instance, if it doesn’t make sense to have user groups, the appropriate code can simply be commented out of the overall_header.tpl file.
Be aware that phpBB allows users to choose their own styles, so, unless you choose to override the user’s choice of style from the general configuration menu, any installed templates will be available for users to choose. If you do decide to customise styles and also want to allow users their choice, you’ll have to make the appropriate changes to every style template. One change you shouldn’t make is to the copyright notice on the footer of each page.
Conclusion
With little effort and no cost, you’ve installed a professional bulletin board service on your Website, regardless of the server you’re using and your level of knowledge of PHP and databases. The style of this bulletin board can be easily configured to match the look and feel of the rest of your site, so give it a try!
PHP Tutorial – Part 7 – Final Notes Print, Echo and HTML
May 2, 2010 by technology
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 technology
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
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:
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 technology
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.

