We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Setting HTTP status code from plugin

Development and Programming

Ben Kimball (UT)'s avatar
Ben Kimball (UT)
33 posts
17 years ago
Ben Kimball (UT)'s avatar Ben Kimball (UT)

I’d like to be able to set the HTTP status code from my plug-in. I’m invoking it from prototype’s Ajax.Updater method, which will behave differently depending on whether it gets a 200 OK or 4xx/5xx error code back from the server. When I tested my code outside of an ExpressionEngine context, the following worked fine:

if ($email->Send()) {
    $this->return_data = 'Email sent!';
} else {
    header('HTTP/1.0 400 Bad request');
    $this->return_data = 'There was a problem sending your message. Please try again.';
}

However, once I put that inside my plugin and invoked it from a template, it stopped working. It looks like EE is overriding my 400 status code with its own 200 OK status code. Can I get around that?

Thanks, Ben

       
Ben Kimball (UT)'s avatar
Ben Kimball (UT)
33 posts
17 years ago
Ben Kimball (UT)'s avatar Ben Kimball (UT)

To put it another way: can I somehow avoid including any output from EE? Maybe by doing something like:

header('HTTP/1.0 400 Bad Request');
die('There was a problem with your email address.');

Thoughts?

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
17 years ago
Lisa Wess's avatar Lisa Wess

Hi, Ben – first a disclaimer: I’m not a programmer. But! This recently came up in regards to forcing a 404 header on a given template, and in the end Derek posted several code snippets. The last solution was this one but you may wish to review the entire thread.

I hope that is of some help to you, at least. =)

       
Ben Kimball (UT)'s avatar
Ben Kimball (UT)
33 posts
17 years ago
Ben Kimball (UT)'s avatar Ben Kimball (UT)

Ah! That global $OUT looks promising, I’ll give it a shot. Thanks Lisa!

Ben

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
17 years ago
Lisa Wess's avatar Lisa Wess

Sure! It was just a guess but I was following the thread and it seemed so similar to what you wanted that I figured it couldn’t hurt to point it out =)

       
Ben Kimball (UT)'s avatar
Ben Kimball (UT)
33 posts
17 years ago
Ben Kimball (UT)'s avatar Ben Kimball (UT)

Well, using $OUT didn’t work. But this did:

header("HTTP/1.0 400 Bad Request");
  print "Sorry, we couldn't send email to that address.";
  exit;

Cheers! Ben

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.