PHP: Difference between revisions

From My Wiki
Jump to navigation Jump to search
Created page with "= Opcode Cache = Zend OPcache is preferred. Other opcode caches are/were: *eAccelerator *APC *XCache And they conflict with each other, so check: php -m | grep -i -E "eaccel..."
 
Added test pages
Line 8: Line 8:
<p>PHP handlers that can use opcache: DSO (mod_php), FCGI, PHP-FPM </p>
<p>PHP handlers that can use opcache: DSO (mod_php), FCGI, PHP-FPM </p>
<p>PHP handlers that cannot use opcache: CGI, SuPHP </p>
<p>PHP handlers that cannot use opcache: CGI, SuPHP </p>
= PHP Examples =
== Test Page ==
<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
<?php
Echo "This is only a test";
?>
</div>
== PHPinfo page ==
<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>
</div>
== Show site user's IP address ==
<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
<?php
echo $_SERVER['REMOTE_ADDR'
?>
</div>
== Show date in the default time zone ==
<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
<?php
echo " " . date("j-M-Y G:i:s e");
echo " \n"; 
?>
</div>
== Cron test ==
<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
<?php
echo "The time is " . date("h:i:sa");
echo PHP_EOL ;
?>
</div>
Then set up a cron job like this:
*/5 * * * * /usr/local/bin/php /home/USER/crontest/phpcron.php >> /home/USER/crontest/phpcronlog.log
== List installed classes in alphabetical order ==
<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
<?php
$classes = get_declared_classes();
sort($classes);
print_r($classes); 
?>
</div>
== Basic PHP mail test ==
<div class="toccolours mw-collapsible" style="width:800px; overflow:auto;">
<?php $to = 'lwtest2@WHATEVER.com'; $subject = 'Test e-mail using PHP'; $message = 'This is a test e-mail message'; $headers = 'From: lwtest@WHATEVER.com' . "\r\n" . 'Reply-To: lwtest@WHATEVER.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers, '-flwtest@WHATEVER.com'); ?>
</div>
== PEAR Mail test with SMTP Auth & SSL ==
# Make sure PEAR Mail is installed.
# Make sure that /usr/local/lib/php (on EA3 servers) is in the include path.
# Change the username, password, and host.
<div class="toccolours mw-collapsible" style="width:800px; overflow:auto;">
<?php
require_once "Mail.php";
$from = "LW Test <lwtest@whatever.com>";
$to = "Jim Murphy <jmurphy@liquidweb.com>";
$subject = "Advanced PHP Mail Test";
$body = "Hello,\n\nThis is a test of the PEAR mail system.\n\nThis is only a test.";
$host = "ssl://hostname";
$port = "465";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
  $smtp = Mail::factory('smtp',
    array ('host' => $host,
      'port' => $port,
      'auth' => true,
      'username' => $username,
      'password' => $password));
  $mail = $smtp->send($to, $headers, $body);
  if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
  } else {
    echo("<p>Message successfully sent!</p>");
  }
?>
</div>

Revision as of 02:03, January 12, 2021

Opcode Cache

Zend OPcache is preferred. Other opcode caches are/were:

  • eAccelerator
  • APC
  • XCache

And they conflict with each other, so check:

php -m | grep -i -E "eaccelerator|apc|xcache|zend"

PHP handlers that can use opcache: DSO (mod_php), FCGI, PHP-FPM

PHP handlers that cannot use opcache: CGI, SuPHP

PHP Examples

Test Page

<?php 
Echo "This is only a test";
?>

PHPinfo page

<?php 
// Show all information, defaults to INFO_ALL
phpinfo();
?>

Show site user's IP address

<?php 
echo $_SERVER['REMOTE_ADDR'
?>

Show date in the default time zone

<?php 
echo " " . date("j-M-Y G:i:s e");
echo " \n";   
?>

Cron test

<?php 
echo "The time is " . date("h:i:sa");
echo PHP_EOL ; 
?>

Then set up a cron job like this:

*/5 * * * * /usr/local/bin/php /home/USER/crontest/phpcron.php >> /home/USER/crontest/phpcronlog.log

List installed classes in alphabetical order

<?php 
$classes = get_declared_classes();
sort($classes);
print_r($classes);   
?>

Basic PHP mail test

<?php $to = 'lwtest2@WHATEVER.com'; $subject = 'Test e-mail using PHP'; $message = 'This is a test e-mail message'; $headers = 'From: lwtest@WHATEVER.com' . "\r\n" . 'Reply-To: lwtest@WHATEVER.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers, '-flwtest@WHATEVER.com'); ?>

PEAR Mail test with SMTP Auth & SSL

  1. Make sure PEAR Mail is installed.
  2. Make sure that /usr/local/lib/php (on EA3 servers) is in the include path.
  3. Change the username, password, and host.
<?php

require_once "Mail.php";

$from = "LW Test <lwtest@whatever.com>";
$to = "Jim Murphy <jmurphy@liquidweb.com>";
$subject = "Advanced PHP Mail Test";
$body = "Hello,\n\nThis is a test of the PEAR mail system.\n\nThis is only a test.";

$host = "ssl://hostname";
$port = "465";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'port' => $port,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 $mail = $smtp->send($to, $headers, $body); 
 if (PEAR::isError($mail)) {
echo("

" . $mail->getMessage() . "

");
  } else {
echo("

Message successfully sent!

");
  }
?>