-5

I am looking for a way to tell the server to die when there is an error.

Examples: Let's say there are 2SQL queries. If one of them does an error, I want none of them to work, as if nothing has been clicked.

Example 2: When you miss a semicolon or similar on your code, but the other parts of your code still works and displays something that isn't supposed.

Something like

if (error) { do nothing and go back to index page; }

Dididi
  • 63
  • 6
  • 1
    transaction -> rollback => google it – Federkun Jun 04 '16 at 18:30
  • I am not only talking about SQL @Federico – Dididi Jun 04 '16 at 18:32
  • 1
    @Hugop: Then what exactly *are* you talking about? Can you provide a more complete example? What sort of error-checking are you having trouble with? – David Jun 04 '16 at 18:34
  • Leverage MySQL Transaction DB query. It will revert back the data in previous state if there is any error in your query. For programming related error try and catch come handy. – Ranjeet Singh Jun 04 '16 at 18:35
  • @David I have written more examples. It could be anything that we forget and turns into an error while coding it. – Dididi Jun 04 '16 at 18:41
  • 1
    @Hugop: How is an `iframe` an error? It sounds like you're asking, "How do I write code to handle all possible problems?" That's *a bit* broad. Do you have a specific example of code which produces an error that you'd like to correct? – David Jun 04 '16 at 18:42
  • _Example 3: ..._ -> so, you want erase the output buffer when you need to handle a fatal error? – Federkun Jun 04 '16 at 18:52
  • @David It is a XHTML issue, doing it and closing with /> makes the browser ignore the rest of the code, including php back when I did that on MyBB. – Dididi Jun 04 '16 at 18:53
  • @Hugop: That doesn't sound entirely correct. The browser doesn't have anything to do with the execution of PHP code. And invalid markup isn't going to concern the PHP interpreter. It seems like this question may be based on some false premises, contributing to its unanswerability. – David Jun 04 '16 at 18:56
  • @David I know, but it doesn't get read, it gets ignored. Here is an image of the index page when I close an iframe with /> https://i.gyazo.com/e0a6ebc4e5bcd842391ad39134edca7d.png Of course it may be a MyBB problem, but ignoring the code isn't right. – Dididi Jun 04 '16 at 19:07
  • @Hugop: A screenshot of a web page doesn't really say anything about the PHP code which was executed or the HTML which was produced. Are you instead asking how to *diagnose* problems, basically looking for introductory tips for PHP development and debugging? Like, what general steps to take to find where a problem is happening? Also a bit broad, but perhaps more answerable. – David Jun 04 '16 at 19:16
  • @David No, for short, I don't want the page to open or the code do anything at all when there is a php error. Instead of reporting it to the errors log file, I want it to stop the entire script, even the ones that don't contain an error. – Dididi Jun 04 '16 at 19:21
  • @Hugop: That doesn't make any sense. This question seems to be based on an incorrect understanding of how PHP (or any language really) works. For one thing, there are many different kinds of errors which affect the system in different ways. For example, syntax errors *will* "stop the entire script" already. There are different ways to handle different kinds of errors. "How do I handle all possible errors" is simply a non-starter. There is no magic wand. (Also, *not logging the error* makes no sense, because how can you expect to ever *correct* the problem if you never know about it?) – David Jun 04 '16 at 19:26
  • @David I think you are getting it wrong. I don't want to fix the errors (only later), I want the page to not open when there is somewhere an error. By errors, I mean everything that gets reported on our errors file (iframe one gets reported). Of course I want the errors to get logged, but logging the error means that the server found an error, and I want it to log it AND not open the page. Let's say you turn your username into a SQL injection, most parts of our website reports an error. I want it to block access at the same time. – Dididi Jun 04 '16 at 19:33
  • @Hugop: So something like this?: http://stackoverflow.com/questions/5033436/php-custom-error-page – David Jun 04 '16 at 19:36
  • @David Thanks, that question is something I was looking for, however, I am not sure if it is what I am looking for. Regarding throw new Exception(), does it block the entire code? Let's say I have on the top of my php code a visitor counter (example), would it still count him as a visitor? – Dididi Jun 04 '16 at 20:03
  • @Hugop: Nobody here can know what theoretical code you haven't shown us is going to do under unknown error conditions. In general however, if that code executes, then afterward it's safe to assume that the code has executed. – David Jun 04 '16 at 20:16
  • @David It doesn't matter what the code is going to look like, because I clearly said on the title, on the post and on the comments many times that I want to block access and not let anything execute but the redirect code when there is a php error, no matter how big or small it is. – Dididi Jun 04 '16 at 22:13
  • @Hugop: You seem to have some fundamental misconceptions of how code works. It absolutely does matter how the code is designed and written, and what manner of error condition is taking place, to determine how to handle that error. What you're looking for is a magic wand. PHP hasn't implemented magic yet, nor is it on the development roadmap. – David Jun 04 '16 at 23:04

2 Answers2

1

Exception handling is available in PHP since version 5. It allows you to have a more fine-grained control over code when things go wrong ie, when exceptions occur.

Put your codes inside try block. if an error occur then the code inside your catch block will run. There is also one more bock. i.e finally (PHP 5.5) which will be called every time your code will run. You can hierarchically use these blocks.

Useful link: https://adayinthelifeof.nl/2013/02/12/php5-5-trycatchfinally/

try {

//Your codes

} catch (Exception $e) {
   //when above codes gives error
   do nothing and go back to index page;
}
Ranjeet Singh
  • 588
  • 5
  • 12
0

You can always send a redirect and call die()/exit() to stop execution of the current script.

if (error) {
header("Location: http://example.com/index.php");
die();
}

Or surround your code with try/catch and send the redirect inside the catch block:

 try {
      //code where an error could occur

 } catch (Exception $e) {
     header("Location: http://example.com/index.php");
    die();
 }
eol
  • 23,236
  • 5
  • 46
  • 64
  • 1
    It would be pretty useful in the `catch` block to not completely ignore the exception. In case one wanted to actually correct the error at some point. – David Jun 04 '16 at 18:38
  • Is there a friendly way to do this for the entire code? I am not familiar with catch, but thanks for mentioning it. – Dididi Jun 04 '16 at 19:16