Code Translation: ASP.NET Server.Transfer in PHP

后端 未结 3 659
花落未央
花落未央 2020-12-21 06:11

How would I do this in PHP?

Server.Transfer(\"/index.aspx\")

(add \';\' for C#)

EDIT:

It is important to h

3条回答
  •  既然无缘
    2020-12-21 07:07

    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();

提交回复
热议问题