473,226 Members | 1,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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($iserror)
{
if($iserror)
{
header("Location:http://x.com/errorpage.php");
return true;
}
else
return false;
}

//========
// USAGE EXAMPLE
//========
fm_assert(!mysql_query($sql));
....
header("Location: 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(!mysql_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($iserror) 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!

Jul 17 '05 #1
11 2165
"Kelvin Jones" <ke**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.


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:

http://php.net/exit

You are sure that it is not doing this?
Jul 17 '05 #2

"Kelvin Jones" <ke**********@gmail.com> wrote
function fm_assert($iserror)
{
if($iserror)
{
header("Location:http://x.com/errorpage.php");
return true;
}
else
return false;
}

//========
// USAGE EXAMPLE
//========
fm_assert(!mysql_query($sql));
...
header("Location: http://second-page.com");


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("Location:http://x.com/errorpage.php");
} else {
header("Location: 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

Jul 17 '05 #3
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.

Jul 17 '05 #4

"Timin Uram" <so*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.


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
Jul 17 '05 #5
Hi Kelvin,

The follown combination is usually quite effective:

$url = "http://x.com/errorpage.php";
@header("Location:$url");
print "<script> document.location='$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,
http://www.phppeanuts.org/site/index...unit.PntAssert
Kelvin Jones wrote:
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($iserror)
{
if($iserror)
{
header("Location:http://x.com/errorpage.php");
return true;
}
else
return false;
}

//========
// USAGE EXAMPLE
//========
fm_assert(!mysql_query($sql));
...
header("Location: 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(!mysql_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($iserror) 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!

Jul 17 '05 #6
.oO(Kelvin Jones)
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($iserror) and not have to put the 'if' or the 'exit' in
there?
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($iserror) {
if ($iserror) {
header('Location: http://x.com/errorpage.php');
exit;
}
}
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.


exit terminates the entire script.

Micha
Jul 17 '05 #7
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 :)

Jul 17 '05 #8
.oO(Kelvin Jones)
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 :)


It's ugly and shouldn't be necessary.

header(...);
exit;

always worked here.

Micha
Jul 17 '05 #9
On 4 Mar 2005 09:53:30 -0800, "Kelvin Jones" <ke**********@gmail.com>
wrote:
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.


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

http://ca3.php.net/manual/en/function.exit.php

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

Jul 17 '05 #10
Michael Fesser wrote:
It's ugly and shouldn't be necessary.
Ha, ha, i agree, all code should allways be 100 % bugfree :-)))

header(...);
exit;

always worked here.

Micha


I suppose your code never fails after the headers have been sent :>
Henk.
Jul 17 '05 #11
.oO(Henk Verhoeven)
I suppose your code never fails after the headers have been sent :>


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
Jul 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: damien morton | last post by:
Hi, I need some help... Im trying to use XmlValidatingParser with a custom XmlResolver. Im finding, however, that the custom resolver never gets called, and that the XmlValidatingParser acts as...
1
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
7
by: Andrea | last post by:
Hi there - I'm hoping someone can help me; I've been struggling with this for a few days! :-) I have a webpage that is comprised of many forms containing questions. As the user answers one...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
8
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could...
2
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not...
9
by: pereges | last post by:
Hi, I'm trying to write a macro for the relative difference function which is used to check the close enough floating point values. Is this correct way to write it ? : #define max(x, y) ((x)...
14
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.