Search code examples
phphttp-redirectlocationrequest-object

PHP how to pass `http request` object from one PHP file to another PHP file


usually when i want to redirect from one php page to another php page of same project i'm using

header("location:somepage.php");

This will cause more calls between client and server. What i want to do is instead of sending redirect header, i want to stop execution of requested page and pass request object or request information to the another page which i want to redirect. In this case single request will be enough. I guess this kind of functionality available in jsp. Is same thing available in php which i don't know?


Solution

  • As @DanSherwin commented, you probably want to use include. You might do something like this:

    firstpage.php:

    if(/* Some condition when you want to do a redirect */){
        include 'somepage.php';
        exit;
    }
    

    This runs the code from somepage.php immediately, as though it was cut and pasted into firstpage.php**, and then it exits right afterward as though you redirected away from firstpage.php.

    ** caveat: watch out for variable scope.