Difference between revisions of "WebArea/Compat/PHP/Fr"

From TuxFamilyFAQ
Jump to navigationJump to search
(First draft.)
 
Line 9: Line 9:
 
Since this is easier said than done (after all TuxFamily paths can sometimes be a little counter-intuitive), you may use the following PHP script:
 
Since this is easier said than done (after all TuxFamily paths can sometimes be a little counter-intuitive), you may use the following PHP script:
  
  <?php
+
  <nowiki>
function nope_the_fuck_out_of_here($message) {
+
<?php
    echo $message . ' Aborting.';
+
function nope_the_fuck_out_of_here($message) {
    exit();
+
    echo $message . ' Aborting.';
}
+
    exit();
$ini_file = php_ini_loaded_file();
+
}
if (!is_file($ini_file)) nope_the_fuck_out_of_here('Unable to determine php.ini location.');
+
$ini_file = php_ini_loaded_file();
$base_path = implode(array_slice(explode('/', realpath(getcwd()), 8), 0, 7), '/');
+
if (!is_file($ini_file)) nope_the_fuck_out_of_here('Unable to determine php.ini location.');
if (!is_dir($base_path)) nope_the_fuck_out_of_here('Non-existing webarea base path.');
+
$base_path = implode(array_slice(explode('/', realpath(getcwd()), 8), 0, 7), '/');
$error_dirpath = $base_path . '/php-include';
+
if (!is_dir($base_path)) nope_the_fuck_out_of_here('Non-existing webarea base path.');
if (!is_dir($error_dirpath)) nope_the_fuck_out_of_here('No php-include directory.');
+
$error_dirpath = $base_path . '/php-include';
$error_filepath = $error_dirpath . '/error.log';
+
if (!is_dir($error_dirpath)) nope_the_fuck_out_of_here('No php-include directory.');
if (!is_file('php.ini')) {
+
$error_filepath = $error_dirpath . '/error.log';
    $copy = copy($ini_file, 'php.ini');
+
if (!is_file('php.ini')) {
    if (!$copy) nope_the_fuck_out_of_here("Copying ${ini_file} failed.");
+
    $copy = copy($ini_file, 'php.ini');
} else echo "php.ini already exists, skipping copy.\n<br>";
+
    if (!$copy) nope_the_fuck_out_of_here("Copying ${ini_file} failed.");
$config = file_put_contents('php.ini', "error_log = ${error_filepath}\n", FILE_APPEND);
+
} else echo "php.ini already exists, skipping copy.\n<br>";
if (!$config) nope_the_fuck_out_of_here("Error while setting error_log.");
+
$config = file_put_contents('php.ini', "error_log = ${error_filepath}\n", FILE_APPEND);
echo "Success! Copied ${ini_file} to the current directory and adjusted error_log to log errors into ${error_filepath}.";
+
if (!$config) nope_the_fuck_out_of_here("Error while setting error_log.");
 +
echo "Success! Copied ${ini_file} to the current directory and adjusted error_log to log errors into ${error_filepath}.";
 +
</nowiki>
  
 
Place it in the same directory as your application's entry point (typically index.php), name it own-error-log.php or own-error-log.php7 and execute it through your browser.
 
Place it in the same directory as your application's entry point (typically index.php), name it own-error-log.php or own-error-log.php7 and execute it through your browser.
 
You should get a message stating "Success!" along with details about where PHP errors will end up.
 
You should get a message stating "Success!" along with details about where PHP errors will end up.
 
Once you are done troubleshooting, remove both php.ini  (or at least its error_log directive) and php-includes/error.log (otherwise you will probably fill up your project quota).
 
Once you are done troubleshooting, remove both php.ini  (or at least its error_log directive) and php-includes/error.log (otherwise you will probably fill up your project quota).

Revision as of 21:29, 16 October 2020

PHP on TuxFamily

Accessing error logs

TuxFamily sysadmins are very nice. But sometimes, they are simply not available RIGHT NOW, i.e. when you are in a dire need to access PHP error logs. You can work around this issue by instructing PHP to log errors to a file of yours.

To do so, copy the adequate php.ini file in the same directory as your application's entry point (typically index.php) then append error_log = /absolute/path/to/some/file.log. Since this is easier said than done (after all TuxFamily paths can sometimes be a little counter-intuitive), you may use the following PHP script:

<?php
function nope_the_fuck_out_of_here($message) {
    echo $message . ' Aborting.';
    exit();
}
$ini_file = php_ini_loaded_file();
if (!is_file($ini_file)) nope_the_fuck_out_of_here('Unable to determine php.ini location.');
$base_path = implode(array_slice(explode('/', realpath(getcwd()), 8), 0, 7), '/');
if (!is_dir($base_path)) nope_the_fuck_out_of_here('Non-existing webarea base path.');
$error_dirpath = $base_path . '/php-include';
if (!is_dir($error_dirpath)) nope_the_fuck_out_of_here('No php-include directory.');
$error_filepath = $error_dirpath . '/error.log';
if (!is_file('php.ini')) {
    $copy = copy($ini_file, 'php.ini');
    if (!$copy) nope_the_fuck_out_of_here("Copying ${ini_file} failed.");
} else echo "php.ini already exists, skipping copy.\n<br>";
$config = file_put_contents('php.ini', "error_log = ${error_filepath}\n", FILE_APPEND);
if (!$config) nope_the_fuck_out_of_here("Error while setting error_log.");
echo "Success! Copied ${ini_file} to the current directory and adjusted error_log to log errors into ${error_filepath}.";

Place it in the same directory as your application's entry point (typically index.php), name it own-error-log.php or own-error-log.php7 and execute it through your browser. You should get a message stating "Success!" along with details about where PHP errors will end up. Once you are done troubleshooting, remove both php.ini (or at least its error_log directive) and php-includes/error.log (otherwise you will probably fill up your project quota).