how to redirect to a page without a trip to the client

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to redirect to a page without a trip to the client

Post by jasongr »

Hello

While processing page A in my server, I realize that I need to redirect the user to page B (both under the same domain own by me).
If I trigger the following code:

Code: Select all

header("Location: http:// <my domain> /B.php");
exit;
while this indeed works, it triggers a second trip to the server. In fact page A returns to the client where the redirect is triggered and then I return to the server to serve page B.

I was wondering if there is a possiblity to redirect to a page without performing the extra trip to the server. Here is my rationale: since I am already in the server when I am processing page A and page B is a page under the same domain as page A, then I don't see a reason why I should be able to do it.

I would appreciate any thoughts and help on this issue
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

if you want to redirect them, you will have another page request.

you could also [php_man]include[/php_man]/[php_man]require[/php_man] the page to avoid that.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

timvw wrote:if you want to redirect them, you will have another page request.

you could also [php_man]include[/php_man]/[php_man]require[/php_man] the page to avoid that.
or perform Apache virtual subrequest.
Reference:
  • [php_man]virtual[/php_man]
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

I cannot rely on apache because my "redirects" are application specific and should be triggered on very specific scenarios

How can include or require help me here?
I need to reset the $_GET and _POST arrays, also the $_SERVER array and basically any other global information that is automatically generated by PHP for me.
I need to simulate a real redirect without causing a page request from the client

Also, I have already used require_once and include_once in my code and I need to "reset" PHP memory so it will allow me to require or include them again (once again I need to have a real "redirect on the server side")

I cannot change a require_once to a require because my code depends on the fact that classes cannot be inluded more than once
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

jasongr wrote:I cannot rely on apache because my "redirects" are application specific and should be triggered on very specific scenarios
Have you followed the link I gave to you? [php_man]virtual[/php_man] is called from within PHP.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

jasongr wrote: I need to reset the $_GET and _POST arrays, also the $_SERVER array and basically any other global information that is automatically generated by PHP for me.
I need to simulate a real redirect without causing a page request from the client
Use [php_man]curl[/php_man] to request the page with the proper POST/GET vars and then otuput the result of that page. This way, the extra hit is generated by PHP and not the client.
Post Reply