开发者

print statement after php redirect

开发者 https://www.devze.com 2023-03-24 05:58 出处:网络
I\'m working on several forms right now for a website, and I would like to have a custom pass/fail notification after submission on the same page as the form:

I'm working on several forms right now for a website, and I would like to have a custom pass/fail notification after submission on the same page as the form:

$get = $_GET;
$post = $_POST;

ob_start();
header('Location: ' . $_SERVER['PHP_SELF']);
ob_end_flush();

submit_form($get, $post) ? print('passed') : print('failed');

The submit form function works fine, but the print statements are not showing up - I'm not sure what the flaw in my logic is here, and would really appreciate some assis开发者_JAVA百科tance.


So, basically this is a question of messaging. If your redirect is within the same site and you'll have access to the php of the ending page, then you should do the following:

Use the $_SESSION variable to store a custom variable called "system-message" or whatever you want to call it. (remember, to use $_SESSION, you need to call "session_start()" at the beginning of each php file.)

Upon success of your form, say

$_SESSION['system-message'] = "Form was successfully submitted"; 

THEN call your header redirect.

In the ending page, have it look for a possible system message by doing something like this:

if(isset($_SESSION['system-message'])) { echo "<div class="system-message">".$_SESSION['system-message']."</div>";

Then empty out that variable so it doesn't show up every time you go to a new page... (you want it to only show one time...)

unset($_SESSION['system-message']);


I don't believe that headers get buffered (http://php.net/manual/en/function.ob-start.php) so generally after a header('Location: xyz') call the browser is immediately sent to the page specified by Location. Sometimes you need a call to exit(); after a header('Location: xyz') call to prevent further processing but that is not always the case and can't be relied upon.

Because you can't output anything to the screen before you send headers you will need to use some sort of non-screen logging to notify you of success like storing the message in a database, a text file, or emailing yourself the notification.


By default, PHP uses HTTP 302 status code to redirect by Location header, which has no "body". So, you need to pass some information to script where you will output some message using GET (url), session, cookies or something else.

0

精彩评论

暂无评论...
验证码 换一张
取 消