简体   繁体   中英

How to forward (not redirect) requests in PHP?

I am working on a simple PHP site that involves needing to be able to forward a request made by the user to another page (note that I said forward , and not redirect ). I am aware of how to redirect by manipulating the header variable, but I do not wish to do this, as explained below.

I am trying to write a very simple MVC-patterned mailing list app in PHP, drawing from my knowledge of the same in JSP. One of the things that I appreciated about JSP was that you could both forward or redirect a request. For my purposes, I need forward as I wish to keep the request parameters (whereas redirect will drop them).

Here is a description of what I wish to accomplish:

  • Retrieve input from a form ( ie. /add.php )
  • Process the input in the page called by the form's action ( ie. /process.php ) and add a success message to the request object
  • Forward to another page ( ie. /display.php ) to display the success message in the request object

The only way I am aware of passing the request message to display is to add it to the request object and then access it from the forwarded page. However, the only way I have had success in transitioning to another page is through using the header method, which drops the request object (from what I can tell). I want to find a way to forward the request (object) to the new page, so that I can access the request variables from the new page.

Is there actually anyway to do this in PHP? Java's getRequestDispatcher.forward() is so nice, but I can't find an equivalent through searching. I've tried several similar questions, including the following, but I've never actually found one where both the question and the answer were what I wanted. Most of the answers seem to have something to do with cURL, but I don't want to actually retrieve a file, but simply forward a request in order to access the request object from another page.

Does PHP have an equivalent of Java's getRequestDispatcher.forward()?

Let me know if I should include anything else?

I believe you can do this with include. Before submitting the form just use, as inclusion, in main page:

include ("add.php"); - where the input forms are

after processing the information, include the display.php in the same way; using this, display.php will use same parameters from header, because is included in the same main page.

briefly: add.php, process.php and display.php will be modules for the mother page, but loaded in different state of form processing.

Hope it helps!

use curl with different method get,post. it will sent a request and also get back the response.

The most common method I see of passing messages to the end user from page to page is called session flashing.

This is when you store a variable temporarily in the session until it is read.

Assuming you already have sessions in use:

On process.php:

$_SESSION['message'] = 'Your data has been saved!';

On display.php:

if (isset($_SESSION['message'])) {
    echo $_SESSION['message'];
    unset($_SESSION['message']);
}

You could also store the entire Request object in the session.

So if I am aware, PHP provides just basic set of tools in this case. And there is nothing like "forward" in HTTP originally. It is just frameworks' abstraction/idea. There are two ways to achieve that: copying all params from request and doing new real HTTP request (with redirect) or internal forward: so framework would create fake request and call another controller and action without issuing a new physical HTTP request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM