简体   繁体   English

PHP如何将“ http请求”对象从一个PHP文件传递到另一个PHP文件

[英]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 通常当我想从一个php页面重定向到我正在使用的同一项目的另一个php页面时

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 . 我猜想jsp可以使用这种功能。 Is same thing available in php which i don't know? 是不知道在php中可用的同一事物吗?

As @DanSherwin commented, you probably want to use include . 正如@DanSherwin所说,您可能要使用include You might do something like this: 您可能会执行以下操作:

firstpage.php: 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 . 这将立即从somepage.php运行代码,就像将其剪切并粘贴到firstpage.php **中一样,然后立即退出,就好像您从firstpage.php重定向一样。

** caveat: watch out for variable scope . **警告:请注意可变范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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