Archive for the ‘PHP Tutorials’ Category

How to email with PHP

Friday, July 11th, 2008

Sending emails via PHP is rather simple with the mail() function.

mail() function has the following function prototype:

bool mail  ( string $to  , string $subject  , string $message  [, string $additional_headers  [, string $additional_parameters  ]] )

Here is an example usage:

$EMAIL_HEADER = “From: Sender Name <sender@example.com>\r\n”;
$EMAIL_HEADER .= “MIME-Version: 1.0\r\n”;
$EMAIL_HEADER .= “X-Priority: 1\r\n”;
$EMAIL_HEADER .= “X-MSmail-Priority: High\r\n”;

$MESSAGE=”
Hello there,

This is a test message.

From Admin.”;

mail(“recipient@domain.com”, “Example Subject”, $MESSAGE, $EMAIL_HEADER);

The above code will send out an email to recipient@domain.com with subject “Example Subject” and the message stored in $MESSAGE variable.

The $EMAIL_HEADER determines that this email will be sent from Sender Name <sender@example.com> and is high priority.

Generate PHP Random String

Monday, June 30th, 2008

It’s easy to use to use the rand(min, max) function to generate a random number between min and max (inclusive).

Here is a clever way of using rand() and chr() function to generate a random string:

chr(rand(65,90)) will generate a random character between A – Z (capital)

chr(rand(97,122)) will generate a random character between a – z (lower case)

Therefore, if you want to generate a 5 character long string of random string (assuming all lower case), you would write:

$RandomString=chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122));

Or you can write a simple for() loop and concatenate them together.

Hope that helps.

Free PHP Tutorial

Wednesday, June 18th, 2008

Periodically we will publish free, helpful PHP tutorials for your application or program.

If you have a PHP tutorial and would like to share it with us, feel free to register at our site and make your submission.

In our PHP tutorial archive, we have tutorials that were published years ago (date back to 2003). While they are a good starting point, you may also want to try our PHP forum where you can get specific questions answered.

PHP.net is also a very good starting point. Our PHP functions forums is a place to further compliment PHP.net’s function reference where we discuss practical function usage and examples.

Stay tuned…

Passing values in the URL in PHP

Tuesday, June 17th, 2008

It is very easy to pass values in the URL as a query string, and get those values to a PHP processor file.

For example, if we want to pass a tracking session ID in the URL, such as:

http://www.phpdebutant.com/tracking.php?session=ABC123

In our tracking.php, we can print out the value that is passed in via the URL by referencing to $_GET[session] or $_REQUEST[session] variable:

echo $_REQUEST[session];

PHP query string without question mark

Monday, June 16th, 2008

If you want to pass variables in the URL without having to use ?var=value format, then you will need .htaccess file and a URL RewriteRule.

For example, let’s say we want to pass a variable via the query string http://www.phpdebutant.com/var1=value1, then we would use a .htaccess file and place it in the public_html folder.

The .htaccess file would like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.+)=(.+)$ /index.php?$1=$2

Notice the use of regular expression:

^ denotes the beginning of the line
$ denotes the end of the line

We use ( ) to enclose a regular expression match, so the value of the first match would be in $1, and second would be in $2.

The RewriteRule says that whatever query matches that regular expression, we would redirect the request to /index.php?$1=$2 where $1 and $2 are the values of the match.