Tag PHP

A Tip for the PHP Noobs: Get the Description of the Error or Function Causing the Error

I am a self thought PHP coder and when I was new to this fantastic language I always wanted to know why errors are errors and why certain functions are causing errors. Also I was clueless of why errors are (sometimes) hyplerlinked?

Likewise, every PHP newbie would ask the same frustrating question and would want a description of the mystery behind the error.

Here, I will show you a quick tip to turn your test environment into a study center. Follow the steps below and enjoy your time with PHP.

Hint: I assume you are using XAMPP (the all-in-one suite of Apache, mySQL and PHP) running on Windows machine. You can still implement this technique in any PHP installation though.

Preparation

Download and install XAMPP on your computer.

Visit the PHP manual download page, and select one of the downloads under the ‘Many HTML files’ column. There are many languages to choose from but the English version is the most accurate.

Extract the downloaded archive. Inside the extracted archive you should find a folder named ‘html’. Copy that folder —and its contents— into the following directory:


C:\<your-xampp-installation-path>\

After successfully copying the folder into the XAMPP installation directory, rename your copied folder to ‘phpmanual’. It should be something like this:


C:\<your-xampp-installation-path>\phpmanual

Installation

Well, we placed the PHP manual above the DocumentRoot, just in an out-of-web-reach directory, the reason is that there may be multiple domains or hostnames maintained on the machine via VirtualHost containers, so we need to make the manual work on every domain or hostname on the machine.

Open the XAMPP configuration file:


C:\<your-xampp-installation-path>\apache\conf\extra\httpd-xampp.conf

…then insert these lines in it:


Alias /phpmanual "C:/<your-xampp-installation-path>/phpmanual/"
<Directory "C:/<your-xampp-installation-path>/phpmanual">
    AllowOverride AuthConfig
    Order allow,deny
    Allow from all
</Directory>

Just make sure to change C:/<your-xampp-installation-path> to your XAMPP installation drive and path, respectively.

Configuration

Next, open your PHP.INI file found at: C:\<your-xampp-installation-path>\PHP\php.ini and find the following line:

;html_errors = Off

Uncomment the above directive, then turn it on like this:

html_errors = On

Find these two lines:


;docref_root = "/phpmanual/"
;docref_ext = .html

Uncomment the above two directives, they should match the code below:


docref_root = "/phpmanual/"
docref_ext = .html

Save your changes and close the file.

Restart Apache.

Testing

Now, lets see if our effort is working properly. Create a PHP file with the following code inside it:


<?php

include 'a-php-file-that-does-not-exist.php';

?>

Save the file as test-manual.php in C:\<your-xampp-installation-path>\htdocs\, then visit the following URL in your browser:


http://localhost/test-manual.php

If everything was correct, you should receive an error like the following one:


Warning: include(a-php-file-that-does-not-exist.php) [function.include.html]: failed to open stream: No such file or directory in C:\web-server\htdocs\localhost\test-manual.php on line 3

Warning: include() [function.include.html]: Failed opening 'a-php-file-that-does-not-exist.php' for inclusion (include_path='.;C:\web-server\php\pear\') in C:\web-server\htdocs\localhost\test-manual.php on line 3

function.include.html should be hyperlinked and when you click it you should see a page describing the error, otherwise, there is something wrong with your configuration.

Warning

Never use this feature for production boxes. This feature was intended to support your development since it makes it easy to look-up an error or function description. However it should never be used on live websites (e.g. systems connected to the Internet).

How to Send Email with XAMPP on Windows XP

When we are working on a new web application we usually test our projects locally before making them public. But, localhost doesn’t have all the good stuff available on the real host. Some functions need to be configured properly and some additional programs need to be implemented.

Problem

I was working on PHP/MySQL based small project which is all about students’ registration system, where the students can register themselves then they’ll be able to receive school transcripts and such stuff by email.

When I finished the project I tested the application to see if it was working properly but I got a problem with PHP‘s mail() function, which was not sending any email messages out.

I was feeling frustrated for a while before coming up with the solution below.

Solution

To make our locally hosted web applications talk to any SMTP server including those on the Internet, we will configure that by using the PHP‘s configuration file called php.ini which can be found in the following locations (assuming you are using XAMPP installed in drive C:/):

C:\<xampp-installation-path>\php\php.ini
C:\<xampp-installation-path>\php\php5.ini
C:\<xampp-installation-path>\apache\bin\php.ini

Okay, that was locating the configuration files; let’s move to the next steps.

Method 1:

– Open php.ini file and uncomment the php_smtp.dll extension. This is required when sending emails to a remote server.

– Scroll down and find the following lines:

[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25

; For Win32 only.
;sendmail_from = [email protected]

– From the lines above, uncomment SMTP, smtp_port and sendmail_from
directives, then add SMTP server, SMTP port number and your preferred email address to SMTP, smtp_port and sendmail_from directives respectively, your final code should be similar to the one below:

[mail function]
; For Win32 only.
SMTP = mail.server.com
smtp_port = 25

; For Win32 only.
sendmail_from = [email protected]

– Replace mail.server.com and [email protected] with correct values. The defualt SMTP port number is “25”, therefore, you have 99% chance of not changing this.

– Restart your server.

Everything should work properly now. If not, double check your changes again. If you think you made everything correct, but there is nothing working, try method 2 instead.

NB: You should be aware that once you assign an email address to sendmail_from PHP will force all the senders’ emails to that address.

Method 2:

This method is more easier than the steps described in method 1. We’ll use fake Sendmail Program for Windows (sendmail.exe) which is a simple windows console application that emulates sendmail's "-t" option to deliver emails piped via stdin. sendmail.exe is bundled with XAMPP so you don’t need to install it unless you are using hand made server.

– In method 1 we have enabled SMTP, smtp_port and sendmail_from directives, please make sure that these directives are commented out since we don’t need them anymore. Then scroll down and find the following two lines in your php.ini file:

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\<xampp-installation-path>\sendmail\sendmail.exe -t"

– Make sure that the sendmail_path directive is not commented out, and the path is correct, then save php.ini and close it.

Edit Sendmail Configuration File (sendmail.ini):

The fake Sendmail program is found in the following location:

C:\<xampp-installation-path>\sendmail\sendmail.exe

…and its configuration file is found here:

C:\<xampp-installation-path>\sendmail\sendmail.ini

Okey. That was that. Let’s configure it, so it will work the way we wanted.

– Open sendmail.ini file and use the following settings:


[sendmail]

; you must change mail.mydomain.com to your smtp server,
smtp_server=mail.mydomain.com

; smtp port (normally 25)
smtp_port=25

– Replace mail.mydomain.com with a valid SMTP server and assign port number (usually 25) to smtp_port.

Your SMTP server may require an authentication. If this is the case, scroll down the file and find the following lines:


; if your smtp server requires authentication, modify the following two lines

auth_username=username
auth_password=drowssap

– Modify the above two directives. Add your SMTP server’s username and password.

Some SMTP servers use POP3. If yours is one of those servers, then you need further modifications.


; if your smtp server uses pop3 before smtp authentication, modify the
; following three lines

pop3_server=mail.mydomain.com
pop3_username=username
pop3_password=drowssap

– Change the values of the above three directives to fit your needs and save your file. Then restart your server and try to send a test message to your email address.

If everything is correct, you can send emails to any server now. The following snippet is a header of message sent from my localhost server:


Return-path:
Envelope-to: [email protected]
Delivery-date: Sun, 15 Jun 2009 17:18:55 +0200
Received: from [192.168.3.134] (helo=mehmett)
     by host.server.com with esmtpa (Exim 4.62)
     (envelope-from )
     id 1MFrTy-000CQx-OY
     for [email protected]; Sun, 15 Jun 2009 17:18:55 +0200
To: [email protected]
Subject: Taste email from localhost
Date: Sun, 15 Jun 2009 19:18:39 +0400
From: J Mehmett
Message-ID:
X-Priority: 3
X-Mailer: PHPMailer (phpmailer.sourceforge.net) [version 2.0.4]
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="UTF-8"

That was easy, huh?

Final Thoughts

This is a simple mail configuration. I tested it using the SMTP settings of my host and it worked properly.

Gmail users may check Brett Shaffer’s solution, alternatively, AOL users may see KahWee’s solution.

If you have enjoyed reading this post don’t forget to share your thoughts in the comments section.