Problems writing custom assert function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kelvin Jones

    Problems writing custom assert function

    Hey, I am trying to write a custom assert function that would be used
    to forward a user to an error page if the assertion fails. Here is the
    definition of this function and an example of its intended usage:

    function fm_assert($iser ror)
    {
    if($iserror)
    {
    header("Locatio n:http://x.com/errorpage.php") ;
    return true;
    }
    else
    return false;
    }

    //========
    // USAGE EXAMPLE
    //========
    fm_assert(!mysq l_query($sql));
    ....
    header("Locatio n: http://second-page.com");


    What should happen here is if there IS an error (assertion fails), then
    the user should be forwarded to the error page
    (http://x.com/errorpage.php). However, what DOES happen is that the
    user actually ends up at http://second-page.com not the error page
    (this is happening I guess because the header Location is being
    replaced by the 2nd header call).

    To prevent this, I added those return statements to the fm_assert
    function, and started using it like this:
    if(fm_assert(!m ysql_query($sql ))) exit;

    This works fine, but it looks ugly. My question is (finally) Is there
    some way to make this sleeker so I can call the assert function as just
    fm_assert($iser ror) and not have to put the 'if' or the 'exit' in
    there?

    PS- i tried to move the exit to the fm_assert, but obviously that only
    exited the fm_assert function itself, not the page that it was running
    in.

    Thanks!

  • Joshua Beall

    #2
    Re: Problems writing custom assert function

    "Kelvin Jones" <kelvin.jones@g mail.com> wrote in message
    news:1109958809 .994714.75150@g 14g2000cwa.goog legroups.com...[color=blue]
    > PS- i tried to move the exit to the fm_assert, but obviously that only
    > exited the fm_assert function itself, not the page that it was running
    > in.[/color]

    What version of PHP are you running, exactly? I have to think that you are
    mistaken. If you are not, then you have discovered a nasty bug in PHP.
    exit is supposed to terminate the entire script:

    Terminate the current script with a status code or message


    You are sure that it is not doing this?


    Comment

    • Dan Stumpus

      #3
      Re: Problems writing custom assert function


      "Kelvin Jones" <kelvin.jones@g mail.com> wrote
      [color=blue]
      > function fm_assert($iser ror)
      > {
      > if($iserror)
      > {
      > header("Locatio n:http://x.com/errorpage.php") ;
      > return true;
      > }
      > else
      > return false;
      > }
      >
      > //========
      > // USAGE EXAMPLE
      > //========
      > fm_assert(!mysq l_query($sql));
      > ...
      > header("Locatio n: http://second-page.com");[/color]

      Yeah, you're sending two headers; apparently the second one takes effect.

      I'd remove the header() statement from fm_assert$, and simply:

      if ( fm_assert(...) ) {
      header("Locatio n:http://x.com/errorpage.php") ;
      } else {
      header("Locatio n: http://second-page.com");
      }

      Since your function is designed to assert, just have it assert -- deal with
      what to do outside of it.

      This is a good design principle -- having a routine do just one thing makes
      it more versatile.

      -- Dan



      Comment

      • Timin Uram

        #4
        Re: Problems writing custom assert function

        Well in this case, there wouldn't be a point of having assert return
        only true or false..because then it would just return the conditional
        that was inputted. Then instead of versatile, it would be redundant and
        probably useless.

        Comment

        • Dan Stumpus

          #5
          Re: Problems writing custom assert function


          "Timin Uram" <somenimit@hotm ail.com> wrote in message
          news:1109972643 .881804.198560@ z14g2000cwz.goo glegroups.com.. .[color=blue]
          > Well in this case, there wouldn't be a point of having assert return
          > only true or false..because then it would just return the conditional
          > that was inputted. Then instead of versatile, it would be redundant and
          > probably useless.[/color]

          By god, you're right! Assert is now utterly useless (unless there's some
          murky PHP reason for it), but the suggested code would at least do what the
          OP wanted!

          Good catch,

          Dan


          Comment

          • Henk Verhoeven

            #6
            Re: Problems writing custom assert function

            Hi Kelvin,

            The follown combination is usually quite effective:

            $url = "http://x.com/errorpage.php";
            @header("Locati on:$url");
            print "<script> document.locati on='$url'; </script>";
            exit();

            print will force the header that has just been set to be output. But in
            case something already has been output, so the header will have no
            effect, the script may do the trick.

            Greetings,

            Henk Verhoeven,



            Kelvin Jones wrote:
            [color=blue]
            > Hey, I am trying to write a custom assert function that would be used
            > to forward a user to an error page if the assertion fails. Here is the
            > definition of this function and an example of its intended usage:
            >
            > function fm_assert($iser ror)
            > {
            > if($iserror)
            > {
            > header("Locatio n:http://x.com/errorpage.php") ;
            > return true;
            > }
            > else
            > return false;
            > }
            >
            > //========
            > // USAGE EXAMPLE
            > //========
            > fm_assert(!mysq l_query($sql));
            > ...
            > header("Locatio n: http://second-page.com");
            >
            >
            > What should happen here is if there IS an error (assertion fails), then
            > the user should be forwarded to the error page
            > (http://x.com/errorpage.php). However, what DOES happen is that the
            > user actually ends up at http://second-page.com not the error page
            > (this is happening I guess because the header Location is being
            > replaced by the 2nd header call).
            >
            > To prevent this, I added those return statements to the fm_assert
            > function, and started using it like this:
            > if(fm_assert(!m ysql_query($sql ))) exit;
            >
            > This works fine, but it looks ugly. My question is (finally) Is there
            > some way to make this sleeker so I can call the assert function as just
            > fm_assert($iser ror) and not have to put the 'if' or the 'exit' in
            > there?
            >
            > PS- i tried to move the exit to the fm_assert, but obviously that only
            > exited the fm_assert function itself, not the page that it was running
            > in.
            >
            > Thanks!
            >[/color]

            Comment

            • Michael Fesser

              #7
              Re: Problems writing custom assert function

              .oO(Kelvin Jones)
              [color=blue]
              >This works fine, but it looks ugly. My question is (finally) Is there
              >some way to make this sleeker so I can call the assert function as just
              >fm_assert($ise rror) and not have to put the 'if' or the 'exit' in
              >there?[/color]

              You have to put an exit; after the header() call or the script will
              continue to run (this is mentioned in the manual!) and overwrite the
              Location header.

              function fm_assert($iser ror) {
              if ($iserror) {
              header('Locatio n: http://x.com/errorpage.php') ;
              exit;
              }
              }
              [color=blue]
              >PS- i tried to move the exit to the fm_assert, but obviously that only
              >exited the fm_assert function itself, not the page that it was running
              >in.[/color]

              exit terminates the entire script.

              Micha

              Comment

              • Kelvin Jones

                #8
                Re: Problems writing custom assert function

                Thanks henk, this seemed to do the trick...though it looks a bit clunky
                when it has to be forwarded by the <script>, it works :)

                Comment

                • Michael Fesser

                  #9
                  Re: Problems writing custom assert function

                  .oO(Kelvin Jones)
                  [color=blue]
                  >Thanks henk, this seemed to do the trick...though it looks a bit clunky
                  >when it has to be forwarded by the <script>, it works :)[/color]

                  It's ugly and shouldn't be necessary.

                  header(...);
                  exit;

                  always worked here.

                  Micha

                  Comment

                  • Wayne

                    #10
                    Re: Problems writing custom assert function

                    On 4 Mar 2005 09:53:30 -0800, "Kelvin Jones" <kelvin.jones@g mail.com>
                    wrote:
                    [color=blue]
                    >PS- i tried to move the exit to the fm_assert, but obviously that only
                    >exited the fm_assert function itself, not the page that it was running
                    >in.[/color]

                    As stated before, exit() always exits the entire script.

                    Terminate the current script with a status code or message


                    In fact, I have a redirect() function which works very similarily to
                    your assert function which exits right after sending the location
                    header.

                    Comment

                    • Henk Verhoeven

                      #11
                      Re: Problems writing custom assert function

                      Michael Fesser wrote:[color=blue]
                      > It's ugly and shouldn't be necessary.[/color]

                      Ha, ha, i agree, all code should allways be 100 % bugfree :-)))
                      [color=blue]
                      >
                      > header(...);
                      > exit;
                      >
                      > always worked here.
                      >
                      > Micha[/color]

                      I suppose your code never fails after the headers have been sent :>


                      Henk.

                      Comment

                      • Michael Fesser

                        #12
                        Re: Problems writing custom assert function

                        .oO(Henk Verhoeven)
                        [color=blue]
                        >I suppose your code never fails after the headers have been sent :>[/color]

                        No. If PHP complains about "headers already sent" then you've made a
                        mistake and there's an error in the program flow. It's that simple.

                        You should first check everything that has to be checked before sending
                        any output to the user agent. Another way is to use output buffering.

                        Micha

                        Comment

                        Working...