3

How would I do this in PHP?

Server.Transfer("/index.aspx")

(add ';' for C#)

EDIT:

It is important to have the URL remain the same as before; you know, for Google. In my situation, we have a bunch of .html files that we want to transfer and it is important to the client that the address bar doesn't change.

jrcs3
  • 2,790
  • 1
  • 19
  • 35

3 Answers3

5

As far as I know PHP doesn't have a real transfer ability, but you could get the exact same effect by using include() or require() like so:

require_once('/index.aspx");
TravisO
  • 9,406
  • 4
  • 36
  • 44
  • I was actually worked this out and was going to self answer this one but you beat me to it. – jrcs3 Jan 22 '09 at 22:48
  • Require_once would be more appropriate as you won't be adding any other require statements if your are intending this to work like Server.Transfer. Also, in addition, at the very least you need to add a exit or die to the end of your statement otherwise this is simply a PHP require statement and not Server.Transfer – TroySteven Apr 08 '19 at 16:50
2

The easiest way is to use a header redirect.

header('Location: /index.php');

Edit: Or you could just include the file and exit if you don't want to use HTTP headers.

TJ L
  • 23,914
  • 7
  • 59
  • 77
  • Not actually the same as Server.Transfere, since Server.Transfere doens't change the header. – Filip Ekberg Jan 22 '09 at 22:08
  • For the way I use Request.Transfer(url); in ASPNET the only equivalent in PHP is to do whatever processing you're going to do without writing anything to the headers or body and then calling header('Location: url'); – Mike C. Jun 21 '11 at 21:31
0

Using require will be similar to server.transfer but it's behavior will be slightly different in some cases. For instance when output has already been sent to the browser and require is used, the output already sent to the browser will be shown as well as the path you are requiring.

The best way to mimic C#/ASP.NET Server.Transfer() is to properly setup PHP Output Buffering and then use the following function I wrote.

function serverTransfer($path) { 
    if (ob_get_length() > 0) { 
        ob_end_clean(); 
    }
    require_once($path); 
    exit; 
}

Setting up output buffering is as simple as using ob_start() as the first line called by your PHP application. More information can be found here: http://php.net/manual/en/function.ob-start.php

ASP.NET enables output buffering by default, which is why this isn't necessarily when using Server.Transfer();

TroySteven
  • 4,885
  • 4
  • 32
  • 50