change php error output to custom message with link - php

How do I change the error output of a php error? For example if its a syntax error, or a server side time out, i want to echo a message that allows the user to refresh the page.
Heres the code I want to add my error message to:
$XML->registerXPathNamespace('tree','www.tree.com'); <--occasionally errors here, so I want to output my own error message.

Use the function set_error_handler to define a custom function to be called when there is an error. You can then decide to do whatever you want within that function with the error.
If you only want it for a specific duration, you can restore it afterwards with restore_error_handler.
set_error_handler('yourHandler');
...
$XML->registerXPathNamespace('tree','www.tree.com');
...
restore_error_handler();
function yourHandler(int $errno , string $errstr) {
//show link to refresh page, whatever. full signature can be found on PHP manual page
}

Well, quick check in php manual for registerXPathNamespace shows that it returns TURE or FALSE. Which is perfect for my solution.
if (!#$XML->registerXPathNamespace('tree','www.tree.com')) {
echo '<b>ERROR:</b> Could not register xml path. Please reload the page!';
}
Not that, if it returns FALSE, then the message will be displayed. And the # in front of $XML will disable the original errors that, that action may cause.

Related

Php change fatal error format

I would like to try to change the formatting of fatal error but I can only modify the values of message, file and line.
I would expect to be able to change the css and the html of the message.
Thanks in advance.
Fatal errors in PHP are catchable, so you can make your own exception handler or even use a third party package like https://github.com/filp/whoops
You can catch fatal errors in PHP. Refer to this answer here:
https://stackoverflow.com/a/4410769/9051466
After determining if its a fatal error, you can just create a custom page, redirect the user to that custom page, then display what error message you want the user to see.

Output error messages from abort on custom error pages in Laravel 5

I've created a custom error template for 401 and 404 errors by following the documentation. They display fine with a fixed error message, but when triggering the error with abort() you can pass in an error message as the second argument. What variable do I need to output to show the message passed through abort? Nothing in the documentation tells you how to output that message on the page and any search for error messages either shows info about the validation function or goes on about logging and triggering errors, but not how to output them on the page.
In your view there will be an $exception variable, that is an instance of HttpException.
You can use $exception->getMessage() method to get your message.

wp_redirect & header re-directs prevent me from using Wordpress functions

None of the functions in my functions.php file work after I perform a redirect.
To be more specific, what I have done is use a first php file to do an inital API call, write an array into a txt file, then redirects to another php file that starts off by reading the txt file and then making another API call based on the array of the first. Then parsing that data, and putting it into a wp database in a certain format.
I know that the problem lies with the re-direct because I have tested multiple scenarios as can be seen below and they all point to the re-direct being the culprit.
For example, on my first php file I have the below.
if (function_exists('is_wp_version')){
echo "is_wp_version exists!! <br>";
} else {
echo "is_wp_version, it does not exist <br>";
}
if (function_exists('fopen')){
echo "fopen exists!! <br>";
} else {
echo "fopen, it does not exist <br>";
}
... More code...
... More code...
wp_redirect( 'PHPfile2.php', 301 ); exit; // I have also tried the header
//function with same results
?>
And I will always see printed the below output if I comment out the redirect line .
is_wp_version exists!!
fopen exists!!
However, in PHPfile2.php, I always see the below and get some error and the below output.
is_wp_version, it does not exist
fopen exists!!
Fatal error: Call to undefined function get_header() in PHPfile2.php on line 22
And it gives me some sort of Fatal error. If I remove or comment out the code with the get_header function, then I get a Fatal error on the next wordpress function.
Can someone point me in the right direction? Am I going about this the wrong way? I just need to be able to do 2 API calls with the second dependent on what I get back from the first one.
Thank you!
This is due to the fact that you're leaving the wordpress framework and using your own custom files (which is highly inadvisable).
You need to require_once('wp-blog-header.php');. in PHPfile2.php.

Show PHP errors as a string for display

I'm trying to find out how I would put a PHP error into a variable to display.
Similar to the mysql_error(); I'd like to be able to have displayed the error that PHP gives?
I'm using json_encode to send the testing user back a value from the requested PHP page but if an error happens I can get the mysql_error(); fine but how would I get the php error?
I've tryed $phpError = error_reporting(); but I just get error 22527 like this same Error. I just want to put the php error into a string to send back to the user.
You can write your own error handler: http://www.php.net/manual/en/function.set-error-handler.php
And return a json encoded string.
you can create an custom error handler with set_error_handler and from that point on you can do anything you want
Check out $php_errormsg. I hope it helps.

How do I override the not found page in CodeIgniter?

I have a CodeIgniter application that's generally working how I'd like it to, but occasionally a user will go to a page that does not exist and is greeted with an unfriendly error. I'd like to detect the error automatically and display useful information to the user (not PHP errors). I read the user guide of CodeIgniter, but I couldn't find any relevant section.
How do I handle a page-not-found error in CodeIgniter and display my own custom content?
If you're looking at handling errors with your own custom page, you can modify the error templates found in application/errors. If you have a reason to based on your own code, you can manually send the user to one of these pages using show_404 or show_error - check out the Error Handling page in the official docs.
Try these codeigniter functions
show_404('Your error message');
show_error('Your error message');
you can find more detail at http://codeigniter.com/user_guide/general/errors.html
example:
if ($some_error) //condition
{
show_error('Error');
}
You should test for error return values and catch exceptions. This is a general programming concept - not something specific to Conigniter or PHP.
Testing for error return values:
if (!sort($array))
{
echo "Could not sort $array.";
}
Catching exceptions:
try
{
$someFunction($data);
}
catch (Exception $e)
{
echo "Something went wrong";
}
Of course write useful error messages with pertinent info that helps the user find their problem, and/or helps you fix your bug. You could get advanced and use something like set_error_handler():
http://php.net/manual/en/function.set-error-handler.php
I found this interesting article:
http://www.derekallard.com/blog/post/error-handling-in-codeigniter/
I'm not sure it reflects the current CI release as it's from 2007.

Categories