Practical PHP
Get solutions and answers to your PHP development questions here.
What is PHP?
Are you new to web development? Have you heard about PHP and wonder what it's all about? Read this article first.
PHP script basics
Can I use short tags in my PHP scripts? What are the correct file permissions for my PHP scripts? If you're new to A2 Hosting or PHP development in general, get answers to these essential questions and more here.
Viewing PHP settings
You can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
Changing PHP settings and versions
How you change the PHP version and settings depends on the type of hosting account that you have. Read this article for everything you need to know.
Custom php.ini files
You can use php.ini files to customize a wide range of PHP settings for your web site. Learn how here.
Using php.ini directives
The directives in php.ini files allow you to control many settings for your web site. For example, you can control error logging, specify time zone information, and more.
Custom .htaccess files for PHP settings
If you have a managed VPS or Flex Dedicated Server, you can use .htaccess files to customize a wide range of PHP settings for your web site. Learn how here.
Using PHP directives in custom .htaccess files
These articles explain how to set various PHP directives in an .htaccess file. For example, you can control error logging, specify time zone information, and more. Please note that these articles only apply to managed VPS or Flex Dedicated Server accounts.
Run PHP scripts from cron jobs
Learn how to run PHP scripts from cron jobs using the PHP command-line interpreter. This method is faster and more efficient than using curl or wget to run a PHP script.
Using PHP to send e-mail messages
There are several ways you can send e-mail messages from your hosting account by using PHP. Learn more in this article.
PHP include paths
There are several ways you can set the include path in PHP. Learn about them here.
PEAR packages
PEAR packages are reusable PHP code libraries that you can use in your own projects. Learn how to install and configure them here.
Determine if a PHP function is available
You can use the function_exists() function to determine if a particular PHP function is available for your web site.
‘500 Internal Server Error’ while running PHP
There are several reasons why visitors to your web site might receive "500 Internal Server Error" messages. Learn about these causes and how to minimize them here.
‘String could not be parsed as XML’ error message in PHP
When you try to parse XML data in a PHP script file, you may receive a "String could not be parsed as XML" error message. This article describes some possible causes and ways to troubleshoot the problem.
Using the Exif extension in PHP
A2 Hosting's shared servers include PHP support for processing Exif (Exchangeable image file format) data. Learn how to use this extension here.
Common issues after PHP upgrade
This article discusses some issues that may occur after a PHP upgrade on a shared server, and ways to resolve them.
Using a custom php.ini file for cPanel cron jobs
Depending on how PHP scripts are invoked, they often use different php.ini files to run in different environments. This article demonstrates how to use a custom php.ini file that you can use with cron jobs set up in cPanel.
Using PHP sessions
This article discusses PHP sessions -- what they are, when to use them, and how to implement them.
ionCube PHP Loader support
Learn about ionCube PHP Loader support on A2 Hosting servers.
Specifying the MySQL character set in PHP
Learn how to specify the character set when you use the MySQL Improved (mysqli) PHP extension or PDO (PHP Data Objects) to connect to a MySQL database.
‘Unable to allocate memory for pool’ error message in PHP
When you try to view a page running PHP, you may receive an "Unable to allocate memory for pool" error message. This article describes how to resolve this issue.
Optimizing Symfony using APC
Learn how to optimize your Symfony PHP framework code on a managed VPS using APC (Alternative PHP Cache).
Using the internationalization extension in PHP
The PHP internationalization (intl) extension enables you to display locale-specific information for a wide variety of countries and regions. Learn more in this article.
Installing composer
Learn how to install and configure composer, a PHP dependency manager.
Determining if APC is installed on a server
Learn how to determine if the Alternative PHP Cache (APC) is installed on your server. Please note that not all hosting packages support APC.
Using the MailChimp API with PHP
The MailChimp API (application programming interface) enables you to work with a MailChimp account programmatically. This article demonstrates how to use the API with PHP.
‘Allowed memory size exhausted’ error message in PHP
When you try to view a PHP-enabled page, you may receive an "Allowed memory size of bytes exhausted" error message. This article describes how to resolve this issue.
Using ionCube Loader with different PHP versions
Learn how to use ionCube Loader with different PHP versions on your account.
Enabling PHP opcode caching
In this article, learn how to enable PHP opcode caching for your hosting account.
Sending e-mail with PHPMailer
Learn to send e-mail via SMTP with PHP and PHPMailer.
What is PHP?
This article describes what PHP is, and why you might use it to develop your web site.
Table of Contents
What is PHP, and why would I use it?
More Information
Related Articles
- What is PHP, and why would I use it?
PHP is a server-side, cross-platform scripting language that can be embedded in HTML code.
PHP-enabled web pages are handled just like regular HTML pages by web browsers, and you can create and edit PHP pages the same way you do for regular HTML pages.
PHP syntax is similar to Perl and C.
PHP is a very popular scripting language for web development.
It is free and open-source, and is installed by default with all of our hosting plans.
Additionally:
PHP is a relatively easy language to learn.
Programmers with backgrounds in languages such as C or Perl should have little problem learning PHP.
There are also a large number of free online resources available.
PHP is a secure and stable scripting language.
The open-source community is constantly improving PHP, as well as patching and fixing any bugs that are found.
PHP loads and runs quickly without slowing down other processes.
This is because PHP runs in its own memory space.
PHP is flexible because it can connect to several different databases, such as MySQL, PostgreSQL, and others.
PHP web hosting is cost-effective.
Because PHP is open-source, there are no licensing fees associated with it.
Our Guru Crew technical support team are PHP specialists and very familiar with PHP-based sites.
All of A2 Hosting's servers are configured to run PHP.
Additionally, we provide many free PHP tools and applications that you can install with one click using cPanel, and you can choose from among several PHP versions in cPanel.
More Information
To view the official PHP Getting Started guide, please visit http://www.php.net/manual/en/getting-started.php.
To view the official online PHP documentation, please visit http://www.php.net/docs.php.
For more information about PHP hosting at A2 Hosting, please visit https://www.a2hosting.com/php-hosting.
Related Articles
PHP script basicsCan I use short tags in my PHP scripts? What are the correct file permissions for my PHP scripts? If you’re new to A2 Hosting or PHP development in general, get answers to these essential questions and more here.
Viewing PHP settingsYou can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
PHP script basics
This article provides essential information about running PHP scripts on A2 Hosting servers.
Table of Contents
What permissions do I need to set for my PHP script files?
Do I need to add a header (shebang) at the top of my PHP script files?
Can I use short tags in my PHP script files?
What is the path to the PHP executable?
What version of PHP is installed on my account?
What file extensions can I use for my PHP script files?
Do you run PHP in safe mode?
More Information
Related Articles
- What permissions do I need to set for my PHP script files?
To avoid potential problems, you should set your PHP script file permissions to 755 (read, write, and execute permissions for the user, and read and execute permissions for the group and world).
- Do I need to add a header (shebang) at the top of my PHP script files?
No.
You do not need to add a #!/usr/bin/php header (shebang) to your PHP script files.
- Can I use short tags in my PHP script files?
Yes, we have installed support for short tags with PHP.
This means that you can write:
<? echo "Hello world"; ?>
Instead of:
<?php echo "Hello world"; ?>
- What is the path to the PHP executable?
The absolute path to the PHP executable on A2 Hosting's servers is /usr/bin/php.
The /usr/bin directory is in the default path, so generally you do not need to include the full path when you run PHP scripts from the command line.
Just type the following command, replacing filename with the name of your script file:
php filename
However, if you want to run an alternate PHP version from the command line, you must specify the full path to the executable.
To see the PHP versions that are available on your account, type the following command:
ls /opt/php
Each PHP version has its own directory in the /opt/php directory, and the PHP executable is located in the version's bin directory.
For example, if you want to run a file named test.php from the command line using PHP version 5.5, type the following command:
/opt/php/php-5.5.0/bin/php test.php
- What version of PHP is installed on my account?
Currently, you can use PHP version 4.4, 5.x, 7.0, 7.1, 7.2, 7.3 or 7.4.
To determine the command-line PHP version currently installed on your account, type the following command at the command prompt:
php -v
To determine the PHP version that the Apache web server uses to process web pages on your site, log in to cPanel, and in the Software/Services section of the home screen, click PHP Version.
You can also change the PHP version that Apache uses for web site files on this page.
On an unmanaged VPS or Flex Dedicated Server, you can install any version of PHP that you want.
- What file extensions can I use for my PHP script files?
The default file extension for PHP script files is .php.
However, you can define additional PHP script file extensions in an .htaccess file.
For example, the following line demonstrates how to define the file extension .html for PHP script files:
AddHandler application/x-httpd-lsphp .html
The AddHandler directive above works with most Managed and Core plans.
It does not work with Windows plans.
For more information about how to use .htaccess files, please see this article.
- Do you run PHP in safe mode?
No, we do not run PHP on our shared servers with safe_mode enabled, because it can cause issues for scripts.
Additionally, safe mode is deprecated in PHP 5.3.0, and completely removed in PHP 5.4.0.
More Information
For more information about PHP, please visit http://www.php.net.
Related Articles
Configuring file permissions in File ManagerSetting the right file permissions is important for your web site’s functionality and security.
Learn about file permissions and how to set them in this article.
How to view PHP settings with the phpinfo() function
This article describes how to use the phpinfo() function to view detailed information about the PHP environment, settings, and more.
To view phpinfo() output on our servers, you must have an A2 Hosting account.
The information revealed by the phpinfo() function poses a potential security risk, so we do not post it publicly.
Hackers and other malicious actors could use the information from phpinfo() to plan an attack.
Table of Contents
View PHP settings with phpinfo Function
More Information
Related Articles
- View PHP settings with phpinfo Function
You often need to check PHP settings during web development.
You can do this by running a script that calls the phpinfo() function.
When you load the page in a web browser, the script shows all of the PHP configuration settings for your web site.
To use the phpinfo() function to view PHP settings:
Create a file that contains the following code:
<?php
phpinfo();
?>Save the file as info.php or something similar.Upload the file to your public_html directory.Use your browser to go to http://example.com/info.php, where example.com represents your web site's domain name.
The page displays a large amount of information about the PHP installation.
For security reasons, you should disable any calls to the phpinfo() function when web site development and testing is complete.
More Information
For more information about the phpinfo() function, please visit http://www.php.net/manual/en/function.phpinfo.php.
Related Articles
PHP script basicsCan I use short tags in my PHP scripts? What are the correct file permissions for my PHP scripts? If you’re new to A2 Hosting or PHP development in general, get answers to these essential questions and more here.
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
How to change PHP settings and versions on managed hosting accounts
This article discusses how to change the PHP settings and version for your account.
The methods to do this depend on the type of hosting account that you have.
Table of Contents
Changing PHP settings
Changing the PHP version
Related Articles
- Changing PHP settings
PHP enables you to change many configuration settings.
How you do this, however, depends on the type of hosting account you have:
Shared hosting reseller hosting and Managed WordPress accounts that have CageFS enabled: For accounts on servers that have CageFS enabled, you use cPanel or Plesk to control PHP settings.
For more information about changing settings in cPanel, please see this article.
For more information about changing settings in Plesk, please see this article.
To determine if CageFS is enabled for your account, please see this article.Shared hosting and reseller hosting accounts that do not have CageFS enabled: For accounts on servers that do not have CageFS enabled, you use custom php.ini files to control PHP settings.
For more information about how to do this, please see this article.
To determine if CageFS is enabled for your account, please see this article.Managed VPS and Managed Flex Dedicated Servers activated on or after January 16, 2014, or any server that runs PHP as an Apache module: For these servers, you use custom .htaccess files to control PHP settings.
For more information about how to do this, please see this article.Managed VPS and Managed Flex Dedicated Servers activated before January 16, 2014: For these servers, you use custom php.ini files to control PHP settings.
For more information about how to do this, please see this article.
- Changing the PHP version
As with PHP settings, the method to change the PHP version depends on the type of hosting account you have:
Shared hosting, reseller hosting and Managed WordPress accounts that have CageFS enabled:
For information about changing versions in cPanel, please see this article.
For information about changing versions in Plesk, please see this article.
To determine if CageFS is enabled for your account, please see this article.Shared hosting and reseller hosting accounts that do not have CageFS enabled: For information about how to change the PHP version for an account that does not have CageFS enabled, please see this article.
To determine if CageFS is enabled for your account, please see this article.All Managed VPS and Managed Flex Dedicated server accounts: To change the PHP version for one of these accounts, open a support ticket with our Guru Crew on the Customer Portal at https://my.a2hosting.com.
Please include in the ticket which PHP version you would like to use.
Please note that your server will be unavailable for 15 to 20 minutes while we compile the new PHP version.
Related Articles
Determining if your account uses CageFSLearn how to determine if your hosting account is on a server that uses CageFS.
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
Custom .htaccess files for PHP settingsIf you have a managed VPS or Flex Dedicated Server, you can use .htaccess files to customize a wide range of PHP settings for your web site.
Learn how here.
Changing PHP versions and settings using PHP SelectorLearn how to change the PHP version and settings from cPanel.
Please note this article only applies to some hosting accounts.
Controlling PHP settings with a custom php.ini file
This article describes how to use custom php.ini files to control PHP settings for your web site.
This article applies to most managed hosting packages with the cPanel management interface.
To make sure it applies to your situation, start by creating a phpinfo file.
View the file in a web browser and search for Server API.
The associated value should be LiteSpeed along with some version number.
Table of Contents
Setting up a custom php.ini file
More Information
Related Articles
- Setting up a custom php.ini file
A php.ini file contains directives for controlling many PHP settings.
You can set up one or multiple custom php.ini files to define how your web site functions.
For example, you might have a custom file in your /home/username/public_html directory for your web site's main pages, and a separate custom file in your /home/username/public_html/images directory for your web site's image files.
To set up a custom php.ini file, follow these steps:
Log in to cPanel.
If you do not know how to log in to your cPanel account, please see this article.
Using the File Manager, navigate to the directory where you want to modify the behavior of php.
By default, a custom php.ini affects only the directory where it is located.
Click the File button in the File Manager toolbar to create a new file and name it php.ini.
Click on the new file and then click the Edit button in the File Manager toolbar to edit the new php.ini.
Add php directives to the new php.ini and then click the Save Changes button.
After you create the php.ini file and set its permissions, you can verify that the configuration is active.
To do this, create a phpinfo file in the directory where you created the php.ini
Load the phpinfo file in your web browser, and locate one of the directives you set.
Make sure the changes are shown.
You can create custom php.ini files for each directory in your web site, but many people configure settings in one php.ini file and then apply it to the entire web site.
To do this, add the following line to the .htaccess file in your web site's document root directory.
Replace username with your A2 Hosting username:
lsapi_phpini /home/username/public_html/php.ini
More Information
To view a complete list of php.ini directives, please visit http://www.php.net/manual/en/ini.list.php.
Related Articles
PHP script basicsCan I use short tags in my PHP scripts? What are the correct file permissions for my PHP scripts? If you’re new to A2 Hosting or PHP development in general, get answers to these essential questions and more here.
Using php.ini directivesThe directives in php.ini files allow you to control many settings for your web site.
For example, you can control error logging, specify time zone information, and more.
Using php.ini Directives
PHP allows you to control many aspects of your web site by using directives in php.ini files.
Learn about some of these directives here.
These articles assume that you have already set up a custom php.ini file.
If you have not already done this, please read this article first.
You can control whether or not PHP displays error messages to web site visitors by using the display_errors directive in a custom php.ini file.
Learn how here.
You can use the log_errors and error_log directives in a custom php.ini file to configure PHP error logging, and this article shows you how.
By default, A2 Hosting's servers are set to use US Eastern Standard Time.
Learn how to change the PHP time zone setting for your web site here.
By default, the maximum execution time for PHP scripts is set to 30 seconds, but you can change this interval by using the max_execution_time directive.
You can enable and disable the allow_url_fopen directive in a custom php.ini file.
You can enable and disable the register_globals directive in a custom php.ini file.
This article only applies to PHP 5.3 and earlier versions.
You can enable and disable the magic quotes directive in a custom php.ini file.
This article only applies to PHP 5.3 and earlier versions.
By default, the maximum upload file size for PHP scripts is set to 128 megabytes, but you can change this amount by using the upload_max_filesize and post_max_size directives.
By default, the memory limit for PHP scripts is set to 128 megabytes, but you can change this value by using the memory_limit directive.
By default, the maximum number of input variables for PHP scripts is set to 1000, but you can change this amount by using the max_input_vars directive.
You can use the expose_php directive in a custom php.ini file to control whether or not PHP sends version information in the HTTP headers.
Learn more here.
Learn how the output_buffering directive works, and how to set it in a custom php.ini file.
You can disable specific PHP functions by using the disable_functions directive in a custom php.ini file.
This article shows you how.
Web Hosting
WordPress Hosting
Managed WordPress Hosting
Reseller Hosting
VPS Hosting
Managed VPS Hosting
Cloud VPS Hosting
Dedicated Server Hosting
Email Hosting
Developer Friendly Hosting
Domain Registration
Domain Transfer
SSL Certificates
SwiftServers Fast Hosting
Compare Hosting
Blog Hosting
CMS Hosting
CRM Hosting
eCommerce Hosting
Forum Hosting
Linux Hosting
Wiki Hosting
Submit a Support Ticket
Contact Sales
Knowledgebase
A2 Hosting Blog
99.9% Uptime Commitment
Anytime Money Back Guarantee
About A2 Hosting
A2 Hosting Reviews
Affiliate Program
Careers
Data Centers
Policies
Terms Of Service
Privacy Policy
Locations
Refer-A-Friend
We use cookies to personalize the website for you and to analyze the use of our website.
You consent to this by clicking on "I consent" or by continuing your use of this website.
Further information about cookies can be found in our Privacy Policy.
How to set up custom .htaccess files for PHP settings
This article describes how to use custom .htaccess files to control PHP settings for your web site.
The information in this article only applies to certain types of hosting accounts.
To determine whether or not the information below applies to your account, please see this article.
Table of Contents
Setting up a custom .htaccess file
More Information
Related Articles
- Setting up a custom .htaccess file
You can use directives in an .htaccess file to control many PHP settings.
Additionally, you can set up one or multiple custom .htaccess files to define how your web site functions.
For example, you might have a custom file in your public_html directory for your web site's main pages, and a separate custom file in your public_html/images directory for your web site's image files.
To set up a custom .htaccess file, follow these steps:
Log in to your A2 Hosting account using SSH.
Use a text editor to create an .htaccess file in your web site's document root directory.
Alternatively, if you want to set PHP settings for a specific subdirectory, create the .htaccess file in that subdirectory.
For security reasons, the .htaccess file contents should not be visible to the public.
To do this, add the following lines to the .htaccess file:
# Prevent Apache from serving .ht* files:
<FilesMatch "^\.ht">
Order allow,deny
Deny from all
</FilesMatch>Save the changes and exit the text editor.
You are now ready to add specific PHP directives to the .htaccess file.
For more information about how do this, please see these articles.
More Information
To view a complete list of PHP directives, please visit http://www.php.net/manual/en/ini.list.php.
Related Articles
PHP script basicsCan I use short tags in my PHP scripts? What are the correct file permissions for my PHP scripts? If you’re new to A2 Hosting or PHP development in general, get answers to these essential questions and more here.
Using PHP directives in custom .htaccess filesThese articles explain how to set various PHP directives in an .htaccess file.
For example, you can control error logging, specify time zone information, and more.
Please note that these articles only apply to managed VPS or Flex Dedicated Server accounts.
Using PHP Directives in Custom .htaccess Files
PHP allows you to control many aspects of your web site by using directives in .htaccess files.
Learn about some of these directives here.
These articles assume that you have already set up a custom .htaccess file.
If you have not already done this, please read this article first.
You can control whether or not PHP displays error messages to web site visitors by using the display_errors directive in a custom .htaccess file.
Learn how here.
You can use the log_errors and error_log directives in a custom .htaccess file to configure PHP error logging, and this article shows you how.
By default, A2 Hosting's servers are set to use US Eastern Standard Time.
Learn how to change the PHP time zone setting for your web site here.
By default, the maximum execution time for PHP scripts is set to 30 seconds, but you can change this interval by using the max_execution_time directive.
You can enable and disable the register_globals directive in a custom .htaccess file.
This article only applies to PHP 5.3 and earlier versions.
You can enable and disable the magic quotes directive in a custom .htaccess file.
This article only applies to PHP 5.3 and earlier versions.
You can change the maximum upload file size for PHP scripts by using the upload_max_filesize and post_max_size directives in an .htaccess file.
You can set most PHP directives in an .htaccess file.
However, the allow_url fopen directive is a special case.
Please note that this article only applies to managed VPS and Flex Dedicated Server accounts.
By default, the memory limit for PHP scripts is set to 128 megabytes, but you can change this value by using the memory_limit directive in a custom .htaccess file.
By default, the maximum number of input variables for PHP scripts is set to 1000, but you can change this amount by using the max_input_vars directive in an .htaccess file.
By default, PHP does not buffer output during script processing.
However, you can modify this behavior by using the output_buffering directive in an .htaccess file.
Web Hosting
WordPress Hosting
Managed WordPress Hosting
Reseller Hosting
VPS Hosting
Managed VPS Hosting
Cloud VPS Hosting
Dedicated Server Hosting
Email Hosting
Developer Friendly Hosting
Domain Registration
Domain Transfer
SSL Certificates
SwiftServers Fast Hosting
Compare Hosting
Blog Hosting
CMS Hosting
CRM Hosting
eCommerce Hosting
Forum Hosting
Linux Hosting
Wiki Hosting
Submit a Support Ticket
Contact Sales
Knowledgebase
A2 Hosting Blog
99.9% Uptime Commitment
Anytime Money Back Guarantee
About A2 Hosting
A2 Hosting Reviews
Affiliate Program
Careers
Data Centers
Policies
Terms Of Service
Privacy Policy
Locations
Refer-A-Friend
We use cookies to personalize the website for you and to analyze the use of our website.
You consent to this by clicking on "I consent" or by continuing your use of this website.
Further information about cookies can be found in our Privacy Policy.
How to run PHP scripts from cron jobs
This article describes how to run PHP scripts from cron jobs.
Table of Contents
Running PHP scripts from cron jobs
More Information
Related Articles
- Running PHP scripts from cron jobs
A common method for running PHP scripts from a cron job is to use a command-line program such as curl or wget.
For example, the cron job runs a command similar to the following command:
curl http://example.com/script.php
In this command, curl retrieves the web page, which then runs the PHP script.
However, there is a better way to run PHP scripts on your web site from cron jobs.
You can run the script directly by using the PHP command-line interpreter.
This method is just as effective, and usually faster.
The following command shows how to run a script using the PHP command-line interpreter:
php -q /home/username/public_html/script.php
In this example, the PHP command-line interpreter runs the script.php file.
The -q option enables quiet mode, which prevents HTTP headers from being displayed.
Depending on the code in your PHP script, it may only run correctly when called from a specific directory.
For example, if the script uses relative paths to include files, it will only run if it is called from the correct directory.
The following command shows how to call a PHP script from a specific directory:
cd /home/username/public_html/; php -q script.php
If your script requires special configuration options, you can use a custom php.ini file.
The -c option allows you to call a PHP script using a custom php.ini file:
php -c /home/username/php.ini /home/username/public_html/script.php
More Information
For information about how to use cPanel to create a cron job, please see this article.
Related Articles
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
PHP include pathsThere are several ways you can set the include path in PHP.
Learn about them here.
Using PHP to send e-mail messages
This article discusses several methods for sending e-mail messages from PHP scripts.
You cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:
Web hosting (Lite, Swift, or Turbo)Reseller hosting
For these hosting packages, you must use A2 Hosting servers.
Other hosting packages have fewer restrictions, and can use some external SMTP servers to send e-mail messages.
Table of Contents
Method #1: Using the PHP mail() function
Method #2: Using the PEAR Mail class
Method #3: Using PHPMailer
Related Articles
- Method #1: Using the PHP mail() function
The easiest way to send an e-mail message in a PHP script is to use the built-in PHP mail() function.
The PHP mail() function has several required parameters:
Message recipient
Message subject
Message body
Additionally, there are two optional parameters that you should use to set the “From” e-mail address.
It is important to set the “From” address correctly so replies, bounce notifications, and other messages return to the appropriate address.
If the “From” address is not set, messages sent from your web site display a return address of [email protected], where example.com represents the A2 Hosting server where your web site is hosted.
The following sample code demonstrates how to send an e-mail message with the “From” address set correctly.
It also demonstrates how to set the Content-Type header so international (Unicode) characters are displayed correctly.
In your own code, replace [email protected] with the intended message recipient, and [email protected] with the return e-mail address:
<?php
mail("[email protected]",
"This is the message subject",
"This is the message body",
"From: [email protected]" .
"\r\n" .
"Content-Type: text/plain; charset=utf-8",
"-f[email protected]");
?>
Although the built-in PHP mail() function is easy to use, it has some limitations.
For example, it does not support SMTP authentication.
SMTP authentication enables you to set the envelope headers correctly, which can help prevent mail servers from marking your messages as spam.
See the following methods for ways to send messages from PHP using SMTP authentication.
To view the official documentation for the PHP mail() function, please visit http://php.net/manual/en/function.mail.php.
- Method #2: Using the PEAR Mail class
Although the PEAR Mail class requires more configuration than the PHP mail() function, it is also much more powerful.
With the PEAR Mail class, you can send e-mail messages using SMTP authentication, and specify many other e-mail settings.
To do this, follow these steps:
Install and configure the PEAR Mail package on your account.
For information about how to do this, please see this article.
Remember that after you install the PEAR Mail package using cPanel, you must configure the include_path setting in a custom php.ini file.
The following code snippet demonstrates one way to send an e-mail message using the PEAR Mail class.
You will have to modify the settings for your own application, particularly the server_name, username, and password SMTP authentication parameters:
<?php
require 'Mail.php';
// Define basic e-mail parameters:
$recipient = '[email protected]';
$headers['From'] = '[email protected]';
$headers['Reply-to'] = '[email protected]';
$headers['To'] = '[email protected]';
$headers['Subject'] = 'This is the message subject';
$headers['Date'] = date('r');
$headers['Message-Id'] = '<' .
uniqid() .
'@example.com>';
$headers['Content-Type'] = 'text/plain; charset=utf-8';
$body = 'This is the message body';
// Define SMTP authentication parameters:
$smtp_params['host'] = 'server_name';
$smtp_params['auth'] = true;
$smtp_params['username'] = 'username';
$smtp_params['password'] = 'password';
// Create a Mail class instance with the above parameters, and then send the message:
$message = Mail::factory('smtp', $smtp_params);
$message->send($recipient, $headers, $body);
?>
In the code example above, the Content-Type header is set so Unicode characters display correctly.
You can change this header value to any other character set that you want.
To view the official PEAR Mail documentation, please visit http://pear.php.net/manual/en/package.mail.mail.php.
- Method #3: Using PHPMailer
PHPMailer is a popular e-mail implementation that provides another way to send messages from PHP scripts.
It supports many features, including SMTP authentication, and makes formatting e-mail messages correctly much easier.
To learn to install PHPMailer with this article.
Related Articles
Viewing e-mail message headersYou can view a message’s raw e-mail headers to help troubleshoot e-mail problems.
Additionally, A2 Hosting support staff may ask you to send this information if you are experiencing certain types of e-mail issues.
This article describes how to view the e-mail message headers using a variety of client applications.
PEAR packagesPEAR packages are reusable PHP code libraries that you can use in your own projects.
Learn how to install and configure them here.
PHP include pathsThere are several ways you can set the include path in PHP.
Learn about them here.
Sending e-mail with PHPMailerLearn to send e-mail via SMTP with PHP and PHPMailer.
Using GitThe popular Git revision control system is installed on A2 Hosting’s servers, and this article provides a brief introduction to using it.
Using Git with PleskPlesk supports integration with the Git version control system.
Learn how to manage your repositories in this article.
PHP Include Path File Guide
This article describes several methods for setting the include path in PHP.
By using include paths, you can centralize code that your web site frequently uses.
Additionally, some features, such as PEAR, require you to set the include path so PHP can locate the appropriate files.
You should create the include directory at the username directory level (that is, one level above the public_html directory).
This ensures that sensitive files are not in the public_html directory, which anyone can access.
Table of Contents
#1 PHP Include Path Method: Use a custom php.ini file
#2 PHP Include Path Method: Use the set_include_path() function
#3 PHP Include Pah Method: Use the .htaccess file
More Information
Related Articles
- #1 PHP Include Path Method: Use a custom php.ini file
You can use a custom php.ini file to specify the include path.
If you have not already set up a custom php.ini file, please read this article before you proceed.
To set the include path using a custom php.ini file:
Open the php.ini file in an editor.
You can do this by logging into your account over SSH, or by using the cPanel File Manager.
Add the following line to the php.ini file.
Replace USERNAME with your A2 Hosting username and replace include_directory with the include directory's name:
include_path = ".:/home/USERNAME/include_directory"Save the file.
The include path is now set.
- #2 PHP Include Path Method: Use the set_include_path() function
Instead of setting the include path globally in a configuration file, you can set the path directly in a script file.
To do this, you use the set_include_path() function.
When you set the include path using this method, it is only effective for the duration of the script's execution.
The include path that you specify does not affect any other running scripts.
The following sample code demonstrates how to set the include path using the set_include_path() function:
<?php
set_include_path(".:/usr/lib/php:/usr/local/lib/php:/home/USERNAME/php");
?>
- #3 PHP Include Pah Method: Use the .htaccess file
A few of our VPS and dedicated servers use Apache modules instead of CGI binaries to run PHP.
If your server uses an Apache module to run PHP, you can modify the .htaccess file in your web site's document root directory.
To set the include path using the .htaccess file:
Open the .htaccess file in an editor.
You can do this by logging into your account over SSH, or by using the cPanel File Manager.
Add the following line to the .htaccess file.
Replace path with the include directory's path:
php_value include_path ".:/path"Save the file.
The include path is now set.
More Information
For more information about the include_path directive in php.ini files, please visit http://php.net/manual/en/ini.core.php.
For more information about the set_include_path() function, please visit http://php.net/manual/en/function.set-include-path.php.
Related Articles
PHP script basicsCan I use short tags in my PHP scripts? What are the correct file permissions for my PHP scripts? If you’re new to A2 Hosting or PHP development in general, get answers to these essential questions and more here.
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
Viewing PHP settingsYou can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
How to install and configure PEAR packages
PEAR (PHP Extension and Application Repository) packages are reusable PHP code libraries that can greatly simplify web site development.
There are PEAR packages for interacting with databases, handling e-mail, processing XML, and much more.
Table of Contents
Install a PEAR package
Method #1: Use cPanel
Method #2: Submit a support ticket
Configure the PHP include path
More Information
Related Articles
- Install a PEAR package
Before you try to install a PEAR package, make sure it is not already installed.
For example, our shared hosting servers already have several PEAR packages that are pre-installed systemwide.
To see which PEAR packages are currently available for your web account, log in using SSH and then type the following command:
pear list
If the PEAR package that you want is not listed, then you can install it using one of the following methods.
A2 Hosting does not support installation of PECL (PHP Extension Community Library) modules in our shared hosting environment.
PECL modules can only be installed on VPS and Dedicated hosting accounts.
Method #1: Use cPanel
If your A2 Hosting account includes cPanel, you can use it to install PEAR packages.
For information about how to install PEAR packages using cPanel, please see this article.
After the PEAR package is installed, you must configure the PHP include path.
To do this, follow this procedure.
Method #2: Submit a support ticket
If you have installed PEAR packages before on other systems, you may have used the pear install command.
However, this command requires superuser access, which is not available on shared hosting accounts.
If you are unable to install a PEAR package using cPanel, an A2 Hosting administrator can install it for you.
If you need further assistance, please open a support ticket with our Guru Crew on the Customer Portal at https://my.a2hosting.com.
After the PEAR package is installed, you must configure the PHP include path.
To do this, follow this procedure.
- Configure the PHP include path
After you install a PEAR package for the first time using one of the methods above, you must configure the PHP include path.
Otherwise, your PHP script files will be unable to locate the PEAR packages that you installed.
After you configure the PHP include path, you do not need to do so again for subsequent PEAR package installations.
If you are using pre-installed systemwide PEAR packages in your code, you do not need to follow this procedure.
This procedure only applies to PEAR packages that you have installed yourself.
To configure the PHP include path:
If you do not already have a custom php.ini file in the directory where your script file is located, set one up now.
For information about how to set up a custom php.ini, please see this article.
Open the php.ini file and locate the line that starts with:
include_pathAdd the PHP extension path.
For example, if the existing line is:
include_path = ".:/usr/lib/php:/usr/local/lib/php"
Change the line to:
include_path = ".:/usr/lib/php:/usr/local/lib/php:/home/USERNAME/php"You can now include the PEAR package in script files located in the same directory.
More Information
For more information about PEAR packages, please visit http://pear.php.net.
Related Articles
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
How to determine if a PHP function is available
This article describes how to use the function_exists() function to determine whether or not a function is available for your PHP installation.
Table of Contents
Determine if a PHP function is available
More Information
Related Articles
- Determine if a PHP function is available
You can determine whether or not a PHP function is enabled for your web site by using the function_exists() function.
To determine if a function is available:
Create a file that contains the following code.
This sample code checks to see if the fsockopen() function is available.
To check for another function, change the $function_name variable's value:
<?php
$function_name = "fsockopen";
if ( function_exists($function_name ) ) {
echo "$function_name is enabled";
}
else {
echo "$function_name is not enabled";
}
?>
Save the file as function.php or something similar.Upload the file to your public_html directory.Use your browser to go to http://example.com/function.php, where example.com represents your web site's domain name.
The page displays whether or not the function is enabled for your web site.
More Information
For more information about the function_exists() function, please visit http://us3.php.net/manual/en/function.function-exists.php.
Related Articles
PHP script basicsCan I use short tags in my PHP scripts? What are the correct file permissions for my PHP scripts? If you’re new to A2 Hosting or PHP development in general, get answers to these essential questions and more here.
Viewing PHP settingsYou can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
How To Fix HTTP Error 500 Internal Server Message
This article describes ways to minimize the occurrence of "500 Internal Server Error" messages.
Table of Contents
HTTP Error 500 Problem
HTTP Error 500 Resolution
Set correct permissions
Check .htaccess directives
Related Articles
- HTTP Error 500 Problem
Visitors to your web site receive “500 Internal Server Error” messages when they access a page that uses PHP.
- HTTP Error 500 Resolution
Almost all of our servers run PHP as a CGI binary.
One of the side effects of running PHP as a CGI binary is that internal server errors can occur if the permissions on files and directories are set incorrectly.
Internal server errors can also occur if there are certain PHP directives defined in an .htaccess file.
If your web site is experiencing internal server errors, the first thing you should do is check the server logs.
The server logs provide valuable information about which files are causing the errors, and potential causes.
If you have a shared hosting account, you can view your web site's error logs in cPanel.
If you have a VPS or dedicated server, you can view your web site's log files directly at the following paths:
/usr/local/apache/logs/error_log
/usr/local/apache/logs/suphp_log
Set correct permissions
If permission settings are causing internal server errors, you may see entries in the server logs similar to any of the following lines:
SoftException in Application.cpp:357: UID of script "/home/username/public_html/.htaccess" is smaller than min_uid
SoftException in Application.cpp:146: Mismatch between target UID (511) and UID (510) of file "/home/username/public_html/index.php"
SoftException in Application.cpp:256: File "/home/username/public_html/index.php" is writeable by others
These errors are all caused by permission issues.
The first two lines indicate that the file's owner or group is set incorrectly.
For example, if the owner of a PHP file is the nobody or root account instead of your user account, visitors receive an internal server error when they try to view the page.
If you have a shared hosting account, our Guru Crew can change the owners and groups for your files.
If you need further assistance, please open a support ticket with our Guru Crew on the Customer Portal at https://my.a2hosting.com.
The third line indicates that file permissions for the index.php file are too permissive.
For example, if your web site has a directory or file whose permissions are set to 777 (full permissions), anyone can read, write, or execute it.
Additionally, visitors receive an internal server error when they try to view the page.
To resolve this problem, change the permissions to 755 for directories and 644 for files.
For example, to set the correct permissions for all directories and files in the public_html directory, type the following commands:
cd public_html
find .
-type d -exec chmod 755 {} \;
find .
-type f -exec chmod 644 {} \;
Do not change permissions on the public_html directory itself! Doing so may make your web site inaccessible.
Check .htaccess directives
Servers that run PHP as a CGI binary cannot use the php_flag or php_value directives in an .htaccess file.
If directives in an .htaccess file are causing internal server errors, you will see entries in the server logs similar to the following line:
/home/username/public_html/.htaccess: Invalid command 'php_flag', perhaps misspelled or defined by a module not included in the server configuration
To resolve this problem, you must place any PHP directives into a custom php.ini file on your account, and remove or comment out any PHP directives in the .htaccess file.
For information about how to set up a custom php.ini file, please see this article.
Related Articles
PHP script basicsCan I use short tags in my PHP scripts? What are the correct file permissions for my PHP scripts? If you’re new to A2 Hosting or PHP development in general, get answers to these essential questions and more here.
Viewing PHP settingsYou can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
'String could not be parsed as XML' error message in PHP
This article describes how to troubleshoot the "String could not be parsed as XML" error message in PHP.
Table of Contents
Problem
Resolution
Related Articles
- Problem
When you try to parse XML data in a PHP script, you receive the following error message:
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in FILENAME on line X
- Resolution
The most likely cause of this error is that the XML data you are trying to process is not valid.
Alternatively, the XML data may contain non-standard characters or use a different character set encoding.
There are several websites that can validate XML data.
For example, the website http://www.xmlvalidation.com can check basic XML syntax, as well as validate XML data against external schema, such as DTD (Document Type Definition) documents.
Related Articles
Viewing PHP settingsYou can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
‘500 Internal Server Error’ while running PHPThere are several reasons why visitors to your web site might receive “500 Internal Server Error” messages.
Learn about these causes and how to minimize them here.
How to use the Exif extension in PHP
This article describes how to use PHP to process Exif (Exchangeable image file format) data.
Table of Contents
Using the Exif extension in PHP
More Information
- Using the Exif extension in PHP
A2 Hosting's shared servers include PHP support for processing Exif data.
The Exif PHP extension enables you to work with metadata from images taken by digital devices.
For example, one common use of PHP's Exif functions is to process photographs taken by a digital camera.
Depending on the image file format, you can also retrieve embedded thumbnails of images.
To extract all of the Exif headers from an image file, use the exif_read_data() function.
The following PHP code sample demonstrates how to do this for an image file named photograph.jpg:
$my_exif_data = exif_read_data( 'photograph.jpg' );
print_r( $my_exif_data );More Information
For more information about Exif-related functions available in PHP, please visit http://php.net/manual/en/book.exif.php.
How to resolve some common issues after a PHP upgrade
Newer versions of PHP offer greater stability, security, and features.
However, you may experience problems with your web site after a PHP version upgrade.
This article discusses some ways to resolve them.
Table of Contents
Reverting to the previous version
Copying an updated version of php.ini
“500 Internal Server Error” messages after changing the PHP version
Zend Optimizer and Zend Guard
Using cron with PHP version 5.2.17
Still not working?
Related Articles
- Reverting to the previous version
If you experience problems after a PHP upgrade, you can always revert to an earlier version.
To do this, use the PHP version tool in cPanel.
For more information about how to change the PHP version for your web site, please see this article.
You may receive deprecation errors when you view your web site after a PHP upgrade.
This is because functions or features that your site uses have been removed in the newer version.
You can either modify your web site's code to make it compliant with the new version, or you can revert PHP to the previous version.
- Copying an updated version of php.ini
If you use a custom php.ini file, make sure you use the correct file for your PHP version.
For example:
If you are using PHP 5.3, copy the php.ini file located at /usr/local/lib/php.ini.
This is the default php.ini file.
If you are using PHP 5.2.17, copy the php.ini file located at /opt/php/php-5.2.17/lib/php.ini.
You can find additional PHP versions in the /opt/php directory.
To copy the updated php.ini file to your public_html directory, log in to your account using SSH and then use the cp command.
For example, the following command copies the default php.ini file to a user's public_html directory.
Replace USERNAME with your own account username:
cp /usr/local/lib/php.ini /home/USERNAME/public_html/php.ini
To make the php.ini settings apply to all subdirectories, add the following line to your .htaccess file.
Replace USERNAME with your own account username:
SuPHP_ConfigPath /home/USERNAME/public_html/
If you are using custom settings, such as allow_url_fopen or register_globals, you must reconfigure these settings after you copy the updated php.ini file.
For more information about how to use custom php.ini files, please see this article.
- “500 Internal Server Error” messages after changing the PHP version
If you receive “500 Internal Server Error” messages after changing the PHP version on your web site, check the .htaccess file in your public_html directory.
At the end of the .htaccess file you should see text similar to the following:
# Start A2 Switcher Block
# Do not remove or modify this block! Added by PHP Switcher from cPanel to use an alternate PHP version.
Contact Support for details.
<IfModule mod_suphp.c>
AddHandler application/x-httpd-php-5.4.13 .php
</IfModule>
# End A2 Switcher Block
The AddHandler value determines the PHP version for your site.
Make sure that the correct version is specified, and that the line formatting is correct.
Depending on how the .htaccess file was previously configured, you may find that lines of text have been merged, which could cause problems.
You may also see php_flag values configured in the .htaccess file.
Any php_flag values should now be configured directly in a custom php.ini file.
For more information about how to use custom php.ini files, please see this article.
- Zend Optimizer and Zend Guard
In PHP 5.3, Zend Optimizer changed to Zend Guard Loader.
If you already have a custom php.ini file that calls the old Zend Optimizer libraries, you may receive the following error message:
Fatal error: Incompatible file format: The encoded file has format major ID 2, whereas the Loader expects 4
To resolve this issue, you must invoke the correct Zend libraries for your PHP version:
For PHP 5.3, add the following line to your php.ini file:
zend_extension="/usr/local/Zend/lib/Guard-5.5.0/php-5.3.x/ZendGuardLoader.so"For PHP 5.2.17, add the following lines to your php.ini file:
zend_extension=/opt/php/build/ZendOptimizer-3.3.9-linux-glibc23-x86_64/data/5_2_x_comp/ZendOptimizer.so
zend_loader.enable=1
If you are receiving Ioncube loader errors, you may need to set your site to use PHP version 5.2.17.
Copy over the php.ini file located at /opt/php/php-5.2.17/lib/php.ini, and then add the following line to the top of your php.ini file:
zend_extension="/usr/local/IonCube/ioncube_loader_lin_5.2.so"
To make the php.ini settings apply to all subdirectories, add the following line to your .htaccess file.
Replace USERNAME with your own account username:
SuPHP_ConfigPath /home/USERNAME/public_html/
- Using cron with PHP version 5.2.17
If you experience problems using cron with PHP version 5.2.17, make sure you set the path to PHP to /opt/php/php-5.2.17/bin/php.
The libraries for PHP version 5.2.17 are located in the /opt/php/php-5.2.17/include/php directory.
- Still not working?
If you encounter problems using any of these methods, or are still experiencing issues, please open a support ticket on the A2 Hosting Customer Portal at https://my.a2hosting.com.
Related Articles
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
‘500 Internal Server Error’ while running PHPThere are several reasons why visitors to your web site might receive “500 Internal Server Error” messages.
Learn about these causes and how to minimize them here.
How to use a custom php.ini file for cPanel cron jobs
This article describes how to use a custom php.ini file for cPanel cron jobs.
This article assumes that:
You know how to set up a cron job using cPanel.
For information about how to do this, please see this article.
You know how to set up custom php.ini files.
For more information about how to do this, please see this article.
Table of Contents
Using a custom php.ini file for cPanel cron jobs
Related Articles
- Using a custom php.ini file for cPanel cron jobs
Different php.ini files, and therefore different settings, are used depending on how you run a PHP script.
For example, scripts run from the command line, and scripts run from a cPanel cron job, run under different environments.
To specify the exact environment for a PHP script in a cPanel cron job, you can define the path to a custom php.ini file.
To do this, use the -c option.
For example, the following command runs the script.php file using the php.ini file located in the user's config directory:
php -c /home/username/config/php.ini /home/username/script.php
A common scenario where you need a custom php.ini file is when a script must access remote URLs.
To do this, you must enable the allow_url_fopen directive.
For more information about how to do this, please see this article.Related Articles
Cron jobsYou can use cPanel to configure cron jobs so commands run at user-defined intervals.
Run PHP scripts from cron jobsLearn how to run PHP scripts from cron jobs using the PHP command-line interpreter.
This method is faster and more efficient than using curl or wget to run a PHP script.
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
Using php.ini directivesThe directives in php.ini files allow you to control many settings for your web site.
For example, you can control error logging, specify time zone information, and more.
Getting Started with cPanelAre you new to A2 Hosting or web hosting in general? Need help with cPanel? Start here!
How To Use PHP Sessions
This article describes what PHP sessions are, and how to implement them in your PHP applications.
Table of Contents
About PHP sessions
PHP Session Start
Working with PHP session variables
Ending a session
Session settings in php.ini
Session security
More Information
Related Articles
- About PHP sessions
HTTP is a stateless protocol.
In other words, a web site does not maintain any information about a visitor from one page visit to the next.
This is a problem if you want to keep track of individual visitors as they navigate through a web site.
For example, a shopping cart or online banking application is a common scenario where this functionality is necessary.
PHP sessions work around the stateless limitations of HTTP, and enable you to store data associated with each visitor to your web site.
With just a few lines of code, your web site can maintain data about visitors as they navigate your site.
- PHP Session Start
When you start a session, the web server generates a session identifier that uniquely identifies the visitor.
By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier).
To start a session, use the PHP session_start() function.
Because the PHP session_start() function sends information in the HTTP headers, though, you must call this function before the page generates any content.
The following sample code demonstrates how to do this:
<?php
session_start();
?>
<html>
<head>
<title>Test page</title>
</head>
<body>
<p>Hello</p>
</body>
</html>
By itself, the session_start() function doesn't add much functionality to a web page.
You need to use PHP session variables to really tap into the potential of PHP sessions.
For more information about the session_start() function, please visit http://www.php.net/manual/en/function.session-start.php.
- Working with PHP session variables
The $_SESSION associative array allows you to store session data in variables.
For example, suppose you want to implement a very simple hit counter for a page.
You can't do this with a generic HTML page.
With PHP sessions, though, it's easy.
The following code sample demonstrates one way to do this:
<?php
session_start();
$_SESSION['hits']++;
?>
<html>
<head>
<title>Simple page hit counter</title>
</head>
<body>
<p>You have visited this page <?php print $_SESSION['hits'] ?> times.</p>
</body>
</html>
In this example, we define a variable named hits that is stored in the $_SESSION array.
Each time the page is loaded, the hits value increases by one.
It is easy to add additional PHP session variables to the $_SESSION array.
All you have to do is reference the variable name you want to use, and PHP takes care of the rest, tracking its value across multiple HTTP connections.
For more information about the $_SESSION array, please visit http://www.php.net/manual/en/reserved.variables.session.php.
- Ending a session
To end a session, use the session_destroy() function.
This function deletes all server-side data for the current session.
For example, adding the session_destroy() function to our page counter sample code above wouldn't make much sense, because the value of the hits variable would be destroyed every time the page is loaded.
(The page would always display “You have visited this page 1 times” no matter how many times you load it.)
On the other hand, suppose your site has a shopping cart application.
A user adds products to the cart on one page, fills out the order information on another page, and finally checks out.
After the user checks out and completes the purchase, you probably want to empty the cart and reset the PHP session variables.
The session_destroy() function enables you to do this.
For more information about the session_destroy() function, please visit http://www.php.net/manual/en/function.session-destroy.php.
- Session settings in php.ini
There are numerous settings available in the php.ini file that enable you to configure how sessions function on your web site.
For example, the session.cookie_lifetime setting enables you to control how long a session cookie remains valid, and the session.auto_start setting enables you to start sessions automatically instead of calling the session_start() function explicitly.
Another important option is the session.save_path setting, which enables you to specify where PHP stores session files (by default, they are stored in the server's /tmp directory, but you may prefer to store them somewhere in your own directory instead).
For detailed information about all of the session settings available in the php.ini file, please visit http://www.php.net/manual/en/session.configuration.php.
- Session security
There are several security considerations you should keep in mind when working with PHP sessions.
For example, you must protect against possible session hijacking and fixation attacks.
For more information about PHP session security, please visit http://www.php.net/manual/en/session.security.php.
More Information
To view the official PHP documentation about sessions, please visit http://www.php.net/manual/en/book.session.php.
Related Articles
Custom php.ini filesYou can use php.ini files to customize a wide range of PHP settings for your web site.
Learn how here.
ionCube PHP Loader support
This article discusses support for the ionCube PHP Loader on A2 Hosting servers.
The ionCube Loader is a PHP extension that decodes and executes encoded files at run-time.
Table of Contents
Shared hosting and reseller servers
Managed VPS and managed Flex Dedicated Servers
Cloud VPS, unmanaged VPS, and Flex Dedicated Servers
More Information
Related Articles
- Shared hosting and reseller servers
All shared hosting and reseller servers have the ionCube PHP Loader enabled by default.
You can verify this yourself by viewing the server's PHP configuration settings.
For more information about how to do this, please see this article.
On some shared and reseller hosting accounts, you must follow additional configuration steps to use ionCube Loader with non-default PHP versions.
For more information, please see this article.
- Managed VPS and managed Flex Dedicated Servers
Managed VPS and managed Flex Dedicated Servers have the ionCube PHP Loader enabled by default.
You can verify this yourself by viewing the server's PHP configuration settings.
For more information about how to do this, please see this article.
- Cloud VPS, unmanaged VPS, and Flex Dedicated Servers
If you use Webuzo to install a LAMP (Linux, Apache, MySQL, and PHP) stack, it installs and configures the ionCube Loader as well.
No further configuration is required on your part.
If you install Apache and PHP yourself, however, then you must download and configure the ionCube Loader.
To do this, follow these steps:
Log in to the VPS using SSH.
At the command prompt, type the following command:
wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gzTo extract the files, type the following command:
tar xvzf ioncube_loaders_lin_x86-64.tar.gzMove the ioncube directory to the /usr/local directory by typing the following command:
mv ioncube /usr/localAdd the following line to the /etc/php.ini file.
Replace x.y with the PHP version number that you are running (for example, 5.4):
zend_extension = /usr/local/ioncube/ioncube_loader_lin_x.y.soRestart Apache.
To do this, type the appropriate command for your Linux distribution:
For CentOS and Fedora, type:
service httpd restartFor Debian and Ubuntu, type:
service apache2 restartMore Information
For more information about the ionCube PHP Loader, please visit http://www.ioncube.com.
Related Articles
Viewing PHP settingsYou can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
How to specify the MySQL character set in PHP
This article describes how to specify the character set when you connect to a MySQL database using one of the following methods:
MySQL Improved (mysqli) PHP extension
PDO (PHP Data Objects)
This article assumes that you already know how to connect to a MySQL database using PHP.
For information about how to do this, please see this article.
Table of Contents
Determining which character sets are available on a server
Setting the character set using the MySQL Improved extension
Setting the character set using PDO (PHP Data Objects)
More Information
Related Articles
- Determining which character sets are available on a server
To determine which character sets are available on your server for MySQL, log in to your account using SSH, and then type the following command:
grep "charset name" /usr/share/mysql/charsets/Index.xml | awk -F \" '{print $2}'
This command displays the list of available values that you can use in the methods described below.
To view additional information about a character set, such as its detailed description, open the /usr/share/mysql/charsets/Index.xml file in a text editor.
- Setting the character set using the MySQL Improved extension
If you are using the MySQL Improved (mysqli) extension, use the set_charset method to specify the character set.
For example, the following sample code demonstrates how to specify the Windows Arabic character set using mysqli:
$mysqli = new mysqli("localhost", "dbuser", "password", "database");
$mysqli->set_charset("cp1256");
- Setting the character set using PDO (PHP Data Objects)
To specify the character set using PDO, all you have to do is include the charset setting in the connection string.
For example, the following sample code demonstrates how to specify the UTF-8 Unicode character set using PDO:
$myPDO = new PDO('mysql:host=localhost;dbname=database;charset=utf8;', 'dbuser', 'password');More Information
To view the official online documentation for the MySQL Improved extension, please visit http://www.php.net/manual/en/book.mysqli.php.
To view the official online documentation for PDO, please visit http://www.php.net/manual/en/book.pdo.php.
Related Articles
Connecting to MySQL using PHPThere are several ways you can connect to a MySQL database using PHP.
Learn how here.
Convert a MySQL database to UTF-8Learn how to convert a MySQL database’s character set to UTF-8 (Unicode) encoding.
'Unable to allocate memory for pool' error message in PHP
This article discusses the "Unable to allocate memory for pool" error message in PHP.
Table of Contents
Problem
Resolution
More Information
Related Articles
- Problem
When you try to view a page running PHP, you receive the following error message:
Unable to allocate memory for pool
- Resolution
To resolve this problem, our Guru Crew must adjust APC (Alternative PHP Cache) configuration settings for your account.
Please open a support ticket on the A2 Hosting Customer Portal at https://my.a2hosting.com.
In the ticket, please provide the complete text of the error message you receive, as well as a reference to this Knowledge Base article.
More Information
For more information about APC, please visit http://www.php.net/apc.
Related Articles
Viewing PHP settingsYou can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
How to optimize Symfony using APC
This article describes how to optimize Symfony PHP framework code on a managed VPS using APC (Alternative PHP Cache).
Table of Contents
Optimizing Symfony with APC
More Information
- Optimizing Symfony with APC
You can optimize your Symfony PHP framework code on a managed VPS to run faster.
To do this, use the APC (Alternative PHP Cache) ApcClassLoader class in your code as follows:
$loader = new ApcClassLoader('namespace_prefix', $loader);
$loader->register(true);
The APC namespace prefix can be any value you want.More Information
For more information about how to optimize Symfony, please visit http://symfony.com/doc/current/book/performance.html.
For more information about the Symfony ApcClassLoader class, please visit http://api.symfony.com/2.5/Symfony/Component/ClassLoader/ApcClassLoader.html.
For more information about APC, please visit http://php.net/manual/en/book.apc.php.
How to use the internationalization (intl) extension in PHP
This article describes how to use the internationalization (intl) extension in PHP to process and display locale-specific information.
Table of Contents
Using the internationalization extension
More Information
- Using the internationalization extension
A2 Hosting's shared servers include support for PHP's internationalization extension, which is named intl.
This extension enables you to specify a locale for your PHP applications.
A locale is simply is a collection of the following region-specific settings:
The alphabetical order used to sort text strings.
Character classification and conversion, such as between uppercase and lowercase letters.
Currency formatting.
Decimal separator.
Date and time formatting.
To view a list of locales available on your server, log in to your A2 Hosting account using SSH, and then type the following command:
locale -a
Most locales are in the format xx_YY, where xx is a two-letter designation for the language, and YY is a two-letter designation for a country or region.
Some locales also have an additional identifier that indicates the character set.
For example, en_US represents English in the United States, while zh_TW.big5 represents Chinese in Taiwan using the Big5 character set.
The following code example demonstrates how to set locales using the setlocale() function.
It uses an HTML table to display the current locale setting, as well as the date, time, and a number represented in the current locale:
<html>
<head><title>PHP intl example</title></head>
<body>
<table>
<tr><th>Encoding</th><th>Date/time</th><th>Number</th></tr>
<?php
// Set the locale to French in France:
setlocale( LC_ALL, 'fr_FR' );
printRow();
// Set the locale to English in the United States:
setlocale( LC_ALL, 'en_US' );
printRow();
// Set the locale to Russian in Russia:
setlocale( LC_ALL, 'ru_RU' );
printRow();
// This function generates a table row that displays various text using the current locale setting:
function printRow()
{
$number = 133.52;
print "<tr>";
print "<td>" .
setlocale( LC_ALL, 0 ) .
"</td>"; // Calling setlocale() with locale '0' returns the current locale
print "<td>" .
strftime( '%c' ) .
"</td>";
print "<td>" .
$number .
"</td>";
print "</tr>";
}
?>
</table>
</body>
</html>
If you run this example, you see the date and time format displayed differently for each locale.
Similarly, the $number variable is displayed differently: in French and Russian, the decimal separator is a comma (133,52), whereas in English it is a period (133.52).
Depending on the locale, you may also have to change the page's character encoding set to ensure that text displays correctly.
In the example above, the Cyrillic characters may not display correctly when the locale is set to Russian.
One way to fix this is to specify the character set directly in the HTTP header as follows:
<?php
header('Content-Type: text/html;charset=iso-8859-5');
?>
More Information
For more information about the setlocale() function, please visit http://php.net/manual/en/function.setlocale.php.
How to install composer
This article describes how to install and configure PHP Composer, the dependency manager solution.
PHP Composer enables you to manage dependencies for each project independently, and is used by frameworks such as Laravel.
Table of Contents
PHP Composer Install
More Information
Related Articles
- PHP Composer Install
To install Composer PHP, follow these steps:
Log in to your account using SSH.
To configure your shell environment to use PHP Composer, type the following commands.
Replace username with your account username:
echo 'alias composer="php -d allow_url_fopen=On /home/username/composer.phar"' >> ~/.bashrc
source ~/.bashrc
You can use composer with alternate PHP versions as well:
On an account that has CageFS enabled, all you need to do is select the PHP version in cPanel that you want to use.
For information about how to do this, please see this article.
On an account that does not have CageFS enabled, you must manually specify the path to the PHP executable in the ~/.bashrc file.
For example, if you want to run composer using PHP 5.5, type the following commands instead:
echo 'alias php="/opt/php/php-5.5.0/bin/php"' >> ~/.bashrc
echo 'alias composer="/opt/php/php-5.5.0/bin/php -d allow_url_fopen=On /home/username/composer.phar"' >> ~/.bashrc
source ~/.bashrc
For more information about how to use alternate PHP versions on A2 Hosting servers that do not use CageFS, please see this article.
To download the PHP Composer installer, type the following commands:
cd ~
curl -k -O https://getcomposer.org/installer
To install Composer PHP, type the following command:
php -d allow_url_fopen=On installer
To confirm composer installed correctly, type the following command:
composer -V
Composer should display the version number.
If it does not, repeat steps 2 to 4, paying close attention to the paths.
To update the composer installation to the newest version in the future, type the following command:
composer self-update
More Information
For more information about PHP Composer, please visit https://getcomposer.org.
Related Articles
LaravelLearn how to manually install the Laravel PHP framework on your account.
How to determine if APC is installed on a server
This article describes how to determine if the Alternative PHP Cache (APC) is currently installed on your server.
APC is supported on the following hosting packages:
Turbo Web Hosting
VPS (managed, unmanaged, and Cloud VPS)
Flex Dedicated server (managed and unmanaged)
Table of Contents
About APC
Determining if APC is installed
More Information
Related Articles
- About APC
The Alternative PHP Cache (APC) is a PHP extension that provides opcode caching.
By caching the compiled operation codes (opcodes) of PHP scripts, APC enables sites to serve page content significantly faster.
APC runs on PHP 5.4 and earlier versions.
Because APC is a separate extension, it may or may not already be installed on your server.
The following procedure explains how to determine if your server has the APC extension installed.
For PHP 5.5 and later versions, OPcache replaces APC.
OPcache is a core PHP component that does not require the installation of a separate extension.
- Determining if APC is installed
To determine if the APC extension is installed on your server, follow these steps:
Create a file that contains the following PHP code:
<?php
phpinfo();
?>Save the file as info.php.Upload the file to the public_html directory on your hosting account.Use your browser to go to http://example.com/info.php, where example.com represents your web site's domain name.
The page displays a large amount of information about the server's PHP installation.Use your browser to search for the term apc.
If APC is installed, you see a section that resembles the following:
If you do not see this section, then APC is not installed.
If you have a managed VPS or managed Flex Dedicated server, please open a support ticket on the Customer Portal at https://my.a2hosting.com, and we will install and configure it for you.
If you have an unmanaged server, you can install the APC extension yourself.
For security reasons, you should remove the info.php file after you are done testing.
More Information
For more information about APC, please visit https://php.net/manual/en/book.apc.php.
For more information about OPcache, please visit https://php.net/manual/en/book.opcache.php.
Related Articles
Viewing PHP settingsYou can use the phpinfo() function to view detailed information about the PHP environment, configuration settings, and more.
Using the MailChimp API with PHP
This article discusses how to use the MailChimp API (application programming interface) with PHP.
This article assumes that you have already signed up for and configured a MailChimp account.
For information about how to do this, please see this article.
Table of Contents
About the MailChimp API
Using the official API wrapper
More Information
Related Articles
- About the MailChimp API
The MailChimp API (application programming interface) provides a way to work with campaigns and lists programmatically.
This article demonstrates various ways to use the API with PHP.
- Using the official API wrapper
To access your MailChimp account using PHP, you use code that provides a “wrapper” to the API.
There are several unofficial API wrappers for MailChimp that use a simplified interface.
The procedure below, however, demonstrates how to use the official MailChimp API wrapper.
To do this, follow these steps:
Log in to your A2 Hosting account using SSH.
At the command prompt, type the following commands:
cd ~
git clone https://bitbucket.org/mailchimp/mailchimp-api-php.git
After the git clone command completes, there is a new mailchimp-api-php directory.
Type the following command:
cp -R mailchimp-api-php/src mailchimp
This command copies the API source code to the separate mailchimp directory (you can actually use any directory name you want).
Doing so enables you to pull future updates in the mailchimp-api-php directory for further testing, without overwriting source code referenced directly by your applications.To use the MailChimp API in your PHP code, you must include the Mailchimp.php file.
Then you can create a Mailchimp object and work with the API.
For example, the following code sample demonstrates how to add a new subscriber to an existing list:
<?php
require('mailchimp/Mailchimp.php'); // You may have to modify the path based on your own configuration.
$api_key = "Add your Mailchimp API key here";
$list_id = "Add your list ID here";
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
try
{
$subscriber = $Mailchimp_Lists->subscribe(
$list_id,
array('email' => '[email protected]'), // Specify the e-mail address you want to add to the list.
array('FNAME' => 'Kelly', 'LNAME' => 'Koe'), // Set the first name and last name for the new subscriber.
'text', // Specify the e-mail message type: 'html' or 'text'
FALSE, // Set double opt-in: If this is set to TRUE, the user receives a message to confirm they want to be added to the list.
TRUE // Set update_existing: If this is set to TRUE, existing subscribers are updated in the list.
If this is set to FALSE, trying to add an existing subscriber causes an error.
);
}
catch (Exception $e)
{
echo "Caught exception: " .
$e;
}
if ( ! empty($subscriber['leid']) )
{
echo "Subscriber added successfully.";
}
else
{
echo "Subscriber add attempt failed.";
}
?>
More Information
For more information about the MailChimp API, please visit https://apidocs.mailchimp.com.
Related Articles
Signing up for a MailChimp accountLearn how to sign up for a free MailChimp account so you can efficiently send large numbers of e-mail messages.
'Allowed memory size exhausted' error message in PHP
This article describes how to resolve a problem that may occur when you run a PHP-based web application, such as WordPress or Drupal.
Table of Contents
Problem
Cause
Resolution
Related Articles
- Problem
When you try to view a PHP-enabled page, you receive an error message that resembles the following (the exact values and paths vary):
Fatal error: Allowed memory size of 134217728 bytes exhausted
(tried to allocate 33554432 bytes) in /home/example/public_html/filename.php on line 1622
- Cause
This problem occurs when an application uses more memory than PHP allows.
- Resolution
If you develop a site using your own PHP code, you can try to make your code more memory-efficient.
For example, you can use the unset() function to destroy variables and objects that you no longer need, thereby freeing up memory.
However, if you are running a third-party web application, such as WordPress or Joomla, it is highly inadvisable to modify the code directly.
In such scenarios, you may need to increase the PHP script memory limit.
The method to do this depends on the type of hosting account you have.
To determine which of the following methods applies to your account, please see this article.
If your account uses php.ini files to change PHP settings, for information about how to change the script memory limit, please see this article.
If your account uses .htaccess files to change PHP settings, for information about how to change the script memory limit, please see this article.
If your account uses CageFS, follow these steps:
Log in to cPanel.
If you do not know how to log in to your cPanel account, please see this article.
In the Software/Services section of the cPanel home screen, click Select PHP Version:
Click Switch to PHP Settings.Locate the memory_limit row, select the new value for the script memory limit, and then click Apply.Click Save to save the settings.
The new PHP setting takes effect immediately.
Related Articles
PHP script memory limitBy default, the memory limit for PHP scripts is set to 128 megabytes, but you can change this value by using the memory_limit directive.
Setting the PHP script memory limit in an .htaccess fileBy default, the memory limit for PHP scripts is set to 128 megabytes, but you can change this value by using the memory_limit directive in a custom .htaccess file.
How to use ionCube Loader with different PHP versions
This article describes how to use ionCube Loader with different PHP versions.
The ionCube Loader is a PHP extension that decodes and executes encoded PHP script files at run-time.
The steps and procedures to use in this article depend on whether or not your hosting account uses CageFS.
To determine if CageFS is enabled for your account, please see this article.
Table of Contents
Using ionCube on CageFS servers
Using ionCube on non-CageFS servers
Related Articles
- Using ionCube on CageFS servers
If your account is on a server that has CageFS enabled, ionCube is already loaded for the default PHP version (currently PHP 5.5).
When you change the active PHP version for your account, the corresponding ionCube loader is automatically configured as well.
You do not have to do any special steps to use ionCube with different PHP versions on your account.
- Using ionCube on non-CageFS servers
If your account is on a server that does not have CageFS enabled, ionCube is already loaded for the default PHP version (currently 5.3.29).
However, if you use a different PHP version, you must manually download and install the appropriate ionCube Loader.
Using alternate PHP versions with ionCube Loader should work, but please note that we do not provide support for these configurations.
To do this, follow these steps:
Use your web browser to go to http://www.ioncube.com/loaders.php.
Download the zip file for the stable release of the Linux (64 bits) platform to your local computer.
Using FTP or the cPanel File Manager, upload the zip file to the home directory of your A2 Hosting account.
Using the command line or the cPanel File Manager, extract the contents of the zip file.
After the file is unzipped, there should be an ioncube directory that contains several files.
If you have not already done so, create a php.ini file in your public_html directory.
For information about how to set up a custom php.ini file, please see this article.
Add the following line to the php.ini file.
Replace username with your A2 Hosting account username, and replace X.Y with the PHP version you are using:
zend_extension="/home/username/ioncube/ioncube_loader_lin_X.Y.so"
For example, if your username is example, and you are running PHP 5.5, you would type the following:
zend_extension="/home/example/ioncube/ioncube_loader_lin_5.5.so"
Save your changes to the php.ini file.
The new configuration is now active.
To verify the new configuration, you can create a PHP file that runs the phpinfo() function.
Load the file in your web browser, and then search for the text ionCube Loader.
It should appear in the Additional Modules section.
Related Articles
ionCube PHP Loader supportLearn about ionCube PHP Loader support on A2 Hosting servers.
Do you support ionCube PHP Encoder?Yes, we support ionCube Encoder for your PHP applications.
Read this article for more information.
How to enable PHP opcode caching
This article describes how to enable PHP opcode caching.
Although many server accounts already have opcode caching enabled by default, some accounts (for example, on Turbo Reseller servers) do not.
Table of Contents
About APC and OPcache
Enabling opcode caching
More Information
Related Articles
- About APC and OPcache
The Alternative PHP Cache (APC) and OPcache provide opcode caching.
By caching the compiled operation codes (opcodes) of PHP scripts, APC and OPcache enable sites to serve page content significantly faster.
APC is a PHP extension that runs on PHP 5.4 and earlier versions.
Because it is a separate extension, it may or may not already be installed on your server.
For information about how to determine if APC is installed on your server, please see this article.
OPcache is a drop-in replacement for APC that runs on PHP 5.5 and later versions.
OPcache is a core component built into PHP, so it does not require separate installation like APC does.
- Enabling opcode caching
To enable opcode caching for your account, follow these steps:
Log in to cPanel.
If you do not know how to log in to your cPanel account, please see this article.
In the SOFTWARE section of the cPanel home screen, click Select PHP Version.
Select the check box next to the opcode caching extension you want to enable:
If you are using PHP version 5.4 or older, select apc.
If you are using PHP version 5.5 or newer, select opcache.
Click Save.
Opcode caching is now enabled.
More Information
For more information about APC, please visit https://php.net/manual/en/book.apc.php.
For more information about OPcache, please visit https://php.net/manual/en/book.opcache.php.
Related Articles
Using APC or OPcache with WordPressThis article discusses using APC or OPcache with WordPress.
Using APC or OPcache with DrupalThis article discusses using APC or OPcache with Drupal.
Using APC or OPcache with JoomlaThis article discusses using APC or OPcache with Joomla.
Using APC or OPcache with MagentoThis article discusses using APC or OPcache with Magento.
Using APC or OPcache with PrestaShopThis article discusses using APC or OPcache with PrestaShop.
Sending e-mail using PHPMailer with PHP & SMTP
This article shows several methods for installing and sending mail with PHP and PHPMailer.
You cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:
Web hosting (Lite, Swift, or Turbo)Reseller hosting
For these hosting packages, you must use A2 Hosting servers.
Other hosting packages have fewer restrictions, and can use some external SMTP servers to send e-mail messages.
Table of Contents
Install PHPMailer with Composer
Testing the PHPMailer installation
Install PHPMailer with Git
Testing the PHPMailer install
Windows PHPMailer install using the Git graphical interface
Testing the PHPMailer install
More Information
Related Articles
- Install PHPMailer with Composer
Composer is a dependency manager for PHP that you can use to install packages required by a PHP project.
To install PHPMailer using Composer, follow these steps:
Log in to your account using SSH.
Change the directory to where you want to use the PHPMailer project.
The location can be the document root of a domain as shown, or a subdirectory:
[email protected] [~] cd public_htmlTo install PHPMailer, type the following command:
[email protected] [~/public_html] composer require phpmailer/phpmailer
Composer suggests other packages to install, but they are not needed for this example.
Testing the PHPMailer installation
To test the installation, follow these steps:
Create a PHP file to test the installation.
You can create the file with the editor of your choice.
In this procedure we use vi, but you can also use other command line editors or the graphical editor in your control panel.
The file is named mailer.php in this example, but it can be any name ending in .php.
[email protected] [~/public_html] vi mailer.phpType (or paste) the following code into the editor.
Replace the items in red with the actual values for your account and save the file:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = gethostname();
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the body.';
$mail->send();
?>Test the setup by running the file from the command line or visiting the page in your web browser.
The command line is shown below.
[email protected] [~/public_html] php mailer.php
The file does not produce any visible output, but a mail is sent to the recipient.
- Install PHPMailer with Git
Git is a version-control system that can be also be used to install software.
To install PHPMailer using Git, follow these steps:
Log in to your account using SSH.
Change the directory to where you want to use the PHPMailer project.
The location can be the document root of a domain as shown, or a subdirectory:
[email protected] [~] cd public_htmlTo install PHPMailer, type the following command:
[email protected] [~/public_html] git clone https://github.com/PHPMailer/PHPMailer.git
Testing the PHPMailer install
To test the installation, follow these steps:
Create a PHP file to test the installation.
You can create the file with the editor of your choice.
In this procedure we use vi, but you can also use other command line editors or the graphical editor in your control panel.
The file is named mailer.php in this example, but it can be any name ending in .php.
[email protected] [~/public_html] vi mailer.phpType (or paste) the following code into the editor.
Replace the items in red with the actual values for your account and save the file:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = gethostname();
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the body.';
$mail->send();
?>Test the setup by running the file from the command line or visiting the page in your web browser.
The command line is shown below.
[email protected] [~/public_html] php mailer.php
The file does not produce any visible output, but a mail is sent to the recipient.
- Windows PHPMailer install using the Git graphical interface
Windows hosting packages do not have a command line, so you must do the installation using the graphical tools in Plesk.
To install PHPMailer using Plesk, follow these steps:
Log in to Plesk.
If you do not know how to log in to your Plesk account, please see this article.
Click the Git icon to open the Git tool:
Select Remote at the top of the screen:
In the Remote Git Repository text box, paste the URL for the PHPMailer Git repository (https://github.com/PHPMailer/PHPMailer.git):
Add a destination directory for the PHPMailer files:
Click the destination link under Your Website.
Click Create Folder.
Type PHPMailer as the new folder name.Click the green checkmark to create the folder.Click Ok to set the new folder as the destination.Click OK to install PHPMailer.
Testing the PHPMailer install
To test the installation, follow these steps:
Using the File Manager, create a new file named mailer.php.
Click on the new file in file manager to open it for editing.
Paste the following code into the file.
Replace the values in red with the correct values for your account:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the body.';
$mail->send();
?>To save the file, click OK.Open the page in a web browser.
The file does not produce any visible output, but an e-mail is sent to the recipient.
More Information
PHPMailer supports many more features than are shown here.
For more information about PHPMailer, please visit https://github.com/PHPMailer/PHPMailer.
Related Articles
Using PHP to send e-mail messagesThere are several ways you can send e-mail messages from your hosting account by using PHP.
Learn more in this article.
Using Python to send e-mail messagesLearn how to send e-mail messages from your hosting account using Python.
Using Git with PleskPlesk supports integration with the Git version control system.
Learn how to manage your repositories in this article.