Archive for June, 2008

How to get incoming email via PHP and cPanel

Tuesday, June 24th, 2008

This may be possible to do with other control panel software such as plesk, although I have only done so with cPanel

  1. Open your cPanel homepage, and navigate to the Mail panel
  2. Select the Default Address option
  3. At the bottom of the page, click the Advanced Options »
  4. Select the Pipe to a Program: radio option
  5. In the text field, enter the path from your home directory to the php script
  6. Enter the following code in your script file
#!/usr/bin/php -q

That will collect all the information from the email sent to you, and store it in the $email variable. You will need to add the code after the fclose() for anything to happen with the email, such as save it to a file or parse it for data, or else it will be lost forever. This method will retrieve all headers as well as the data, just for you to be aware

how to process a form with php using one page

Tuesday, June 24th, 2008

Many first time coders in PHP tend to use a form on one page, and a PHP script on a completely different page in order to process it. This is highly irritating since you need to make two pages instead of one, and also, if there is an error you have to redirect back to the original page whilst jumping through hoops to send the data back to the original page.

There is however a rather simple way to do this. First you need to create your (X)HTML document. Below, I’ve created a simple example form using XHTML 1.1


As you can see, it has a single text box and a submit button, with ID’s Name and Submit respectively. Also note the action="". This will automatically make your page post the data to the same page. Many people include the

which is discouraged, since it can lead to XSS Attacks, and since it is not required, it’s just asking for trouble.

OK, onto the PHP code. Here’s some simple PHP, which will check if the form has been posted, by checking if the $_POST['Submit'] is set

  0)
{
	$_POST['Name'] = strip_tags($_POST['Name']);
	$contentdiv = '
Your name is '.$_POST['Name'].'
'; }else{ $contentdiv = '
Please enter your name below
'; } ?>

As you can see, I’ve added the content output I wish to be displayed into a simple <div>. All that is left now is to add some PHP code to the XHTML code to echo out the $content, and save as a .php file and it will all be finished

The PHP code is simply <?php echo $contentdiv; ?> and is added between the <body> and the <form> tags.

You can try out this code by clicking here, or download the script using the button below

Form processor download

How to enable mod_rewrite in Apache

Sunday, June 22nd, 2008

To enable the rewrite module in Apache you will need access to the httpd.conf file, located in {Apache folder}/conf/httpd.conf
Open up the file, and search for
#LoadModule rewrite_module modules/mod_rewrite.so
and simply remove the # from the start of the line. Then save the file, and restart the Apache server. You should now be able to use the mod_rewrite functionality on your server. to test this, create a new file called test.php in your root html folder, with the following code in it

and put

RewriteEngine On
RewriteBase /
RewriteRule ^test/(.+)$ test.php?p=$1 [L]

into a .htaccess file in the same folder

Don’t forget to delete these two files after testing

Headers already sent error php solution

Sunday, June 22nd, 2008

Many people seem to get a problem with the common error message of

Warning: Cannot modify header information - headers already sent by (output started at C:\file.php:1) in C:\file.php on line 4)

There are a number of possible reasons for why this might happen

Output before the <?php tag

There are so many occasions where people report that they have no output, only to find that they have a single space or a new line before their opening <?php tag. Examples

 

appears to be fine to the untrained eye, but upon further inspection we notice that their is a slight gap (one space) before the <?php tag.


also looks fine, however a single carriage return still gets output to the browser, so it's still going to cause the same problem

Output inside your script via functions

php has a number of functions that display code, such as echo(), var_dump() and print_r(). Some people also believe that (X)HTML code does not qualify as content for some reason, and leave it there. This is STILL output, and will still bring up the error. Another instance where people get confused is where there is more than one error on the page. For instance if a Notice: error message comes up before the headers are sent, this will also be classed as an output, therefore turn off notice messages (This should be the case anyway for production servers)

The error is caused by a different file

Warning: Cannot modify header information - headers already sent by (output started at /home/username/www/includedfile.php:1) in /home/username/www/file.php on line 4)
Above shows the location of the file where the output was started at, and also, the line on which it started. Simply go to that line with your editor in that file, and work out what is causing the output. This type of error is common when using include/include_once or require/require_once

When including php from other files, it is a good idea to not have the closing ?> tag. This will NOT affect your script in any way, as PHP is quite happy with no tag. It will also prevent output being left after the tag such as the single space/new line as mentioned above. This is good practice and I recommend doing this with any included files with php, since you should not mix both (X)HTML and PHP in includes other than templates

Making dynamic/pretty URL’s for PHP scripts

Friday, June 20th, 2008

We’ve all seen sites where the URL looks really ugly, i.e.
example.com/product.php?id=12345&action=purchase&num=2

However, over the last few years, more and more sites have converted to dynamic/pretty URL’s, where it looks more like example.com/buy/12345/2/

Using some simple regular expressions and the rewrite module of apache you can create pretty page URL’s

So we have three sections, clearly coloured below:
example.com/buy/12345/2/ need to map to
example.com/product.php?id=12345&action=purchase&num=2

Here’s the RewriteRule for this example, which should be put inside your .htaccess file

RewriteRule ^buy/(\d+)/(\d\d?)/?$ /path/to/public_html/product.php?id=$1&action=purchase&num=$2 [L]

This is a case sensitive search, ie, /BUY/ would not work. If you wish to make it case insensitive, change the [L] to [L,NC]
Note: If you haven’t already got

RewriteEngine on
RewriteBase /

in the top of your .htaccess file, you will need to add it. The first line is required to get the rewrite engine working, and the second is to start matching the URL after the / on the end of example.com/