Alternative to die

Hi. Say I have an if statement which detects and error. If this is activated, this happens

die('ERROR');

Now using die does what I want in that it doesnt allow anything beyond that point to be executed. However, it doesnt do what I want in that the error is printed on a new page.

How could I instead use php to output a simple H1 error whilst using something similar to die to exit the script?


   if($result == 1){
      die('ERROR');
   }
   else if($result == 2){
      die('ANOTHER ERROR');
   }
   //only do this if none of the above are true.  But if they are true, print error here.
   echo "<meta http-equiv=\\"Refresh\\" content=\\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\\">";

cheers

You could echo the message and then die()

Its difficult to post the code, because there is quite a lot. I will try to explain something though.

I have a php file which uses standard html to define the structure of the site using divs. Inside one of the divs, I call up a php function within another php file. This function renders a html login form within this div. Underneath the html code for this login form is some more php, which check if submit button isset. If it is, it does some error checking.

Now printing out the error code within this space would be difficult, as I would need to position it around my form, or remove the form. I decided instead to use javascript to print the message in a message box.

if($result == 1){
	  echo "<script>alert(\\"ERROR.\\");</script>";
   }

The problem with this is it still directs the user to a blank page just to display this message box, and when they click ok, they go back to the form. I have a feeling its the echo which makes this happen. Is there anyway I can run these scripts within php without using an echo?

cheers

You must var_dump a little more, until you isolate the problem. Otherwise it’s hard to understand where your error comes from.

About your script question, you can do something like:


<?php //some php here.. if something... { ?>
<script></script>
<?php }//end of some if for example ?>

Like this, you don’t have an echo.

My vote don’t could much however… Just giving a try.

Regards,
Márcio

Instead of using die, throw an exception. You can then use [fphp]set_exception_handler[/fphp] to determine a global handle to the exception if it makes it out to the interpreter uncaught.

A powerful ability of this approach is your outside code can determine what it wants to do if an exception occurs using try/catch blocks.

Read up on Exceptions for more details as what you are wanting and needing to do here touches on a much larger section of the language than can be easily covered in one post.

I’m not sure what you are trying to do from your description, but hopefully I’ve got the gist of it.

what about something similar to this as a general process which doesn’t require javascript and so you won’t have problems with javascript disabled browsers

  1. say your login form is in index.php

  2. when submit is clicked, the entered username and password are sent to authenticate.php

  3. authenticate.php checks if the username/password are correct. if they are, set a session variable and redirect the user to their home page.

  4. if the username and/or password are invalid for some reason, set an error flag either as a session var or as a parameter in the url redirecting back to index.php

  5. each time index.php is loaded it checks if the error flag is set and has the correct value and if it is set, use php to change the normally hidden div containing an error message to be displayed by changing its css display style. then unset the error flag if it is stored as a session variable.

exit;

:rolleyes: Study more padawan and while you’re at it read the first post thoroughly.