简体   繁体   English

如何处理不同环境的php重定向

[英]how to handle php redirects for different environments

I'm developing a php application that involves creating a session and redirecting a user to a dashboard page after the session is successfully created. 我正在开发一个PHP应用程序,其中涉及创建会话并在成功创建会话后将用户重定向到仪表板页面。 I currently use the following code: 我目前使用以下代码:

if ($success) {
    header("Location: http://www.mydomain.com/dashboard.php");
}

I'm working on making my application available for others to download and use on their own servers, so I'm looking for another way to write the redirect code above so that it's not restricted to only mydomain.com . 我正在努力让其他人可以在自己的服务器上下载和使用我的应用程序,因此我正在寻找另一种方法来编写上面的重定向代码,以便它不仅限于mydomain.com How do you handle php redirects for any domain? 您如何处理任何域的php重定向? Can header() work with relative paths such as header("Location: ./dashboard.php"); header()可以与相对路径一起使用吗,例如header("Location: ./dashboard.php"); ?

The best way for do this is to keep a configuration.php file where you can store all this information (customized on final user needs) and retrieve all you need from this file. 最好的方法是保留一个configuration.php文件,您可以在其中存储所有这些信息(根据最终用户需求定制)并从该文件中检索所有需要的信息。

This file could be "composed" with an init script or something like this (if you have a backoffice, even directly stored in DB or make writeable this file by your backoffice itself, with all risks of the case ) 该文件可以用初始化脚本或类似的东西“组成”(如果您有一个后台,甚至直接存储在数据库中,或者由后台自己将该文件可写,则存在所有风险

In the past I have used the $_SERVER global to get the domain of the current website. 过去,我使用$_SERVER全局$_SERVER来获取当前网站的域名。

echo $_SERVER['SERVER_NAME'];

One downside to this approach is that the application may not be running in the top directory. 这种方法的缺点是应用程序可能不在顶层目录中运行。 So, normally what I have done is in the index.php (all requests are routed through index.php) I defined a constant with the base path and the domain, so something like this: 因此,通常我所做的是在index.php中(所有请求都通过index.php路由),我使用基本路径和域定义了一个常量,如下所示:

define('BASE_PATH', 'http://' . $_SERVER['HTTP_HOST'] . pathinfo($_SERVER['PHP_SELF'])['dirname']);

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

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