简体   繁体   English

通过从外部文件提取URL进行动态PHP重定向

[英]Dynamic PHP Redirect via extracting URL from an external file

As a precaution I am wanting to use PHP to create an easily reusable/modifiable means to redirect users to a specified URL. 作为预防措施,我想使用PHP创建一种易于重用/可修改的方法来将用户重定向到指定的URL。

I have the usual php header redirect: 我有通常的php标头重定向:

<?php

   header( 'Location: http://www.stackoverflow.com/' ) ;

?>

What I'd prefer to do however, as this file will be placed across a lot of directories, to make life easier, and have the url extracted from an external file, eg 但是,我更喜欢这样做,因为此文件将放置在许多目录中,以简化工作,并从外部文件中提取网址,例如

http://www.stackoverflow.com/url.xml

This would of course contain the URL of the website in question, unless there is another way to capture the domain itself automatically? 当然,这将包含相关网站的URL,除非有另一种自动捕获域本身的方法? This I'm not sure. 我不确定。

Could anyone be kind enough to show how this would be done or provide the best approach? 有人能表现出如何做到这一点或提供最佳方法的人吗?

Thank you. 谢谢。

I was thinking in the same approach as @adam , i don't recommend you to extract urls from a file because it can be read from an attacker. 我在以与@adam相同的方式进行思考,我不建议您从文件中提取URL,因为它可以从攻击者那里读取。 It's better to include them in a php file as variables, an array or any other data structure. 最好将它们作为变量,数组或任何其他数据结构包含在php文件中。

Store the urls once in a file called config.inc.php: 将网址一次存储在一个名为config.inc.php的文件中:

<?php

define('USER_PATH', '/redirect/user/path');
define('ADMIN_PATH', '/redirect/admin/path');

?>

Then in your php file: 然后在您的php文件中:

<?php
include 'config.inc.php';
header( "Location: http://{$_SERVER['HTTP_HOST']} . USER_PATH ) ; //or whatever variable you want to use to redirect.
?>

If you want to redirect some folders due to security issues you can do it with an Apache's htaccess. 如果由于安全问题而要重定向某些文件夹,则可以使用Apache的htaccess进行。 Just put this at root folder then add the last line as many as you want for each redirection. 只需将其放在根文件夹中,然后为每个重定向添加最后一行即可。 Each folder mentioned here can only be accessed by scripts running on the server. 此处提到的每个文件夹只能由服务器上运行的脚本访问。
Using this every access request using HTTP gets redirected. 使用此方法,将重定向使用HTTP的每个访问请求。 So this only works for directories containing scripts to be included. 因此,这仅适用于包含要包含脚本的目录。 It doesn't work for ie image directories whose index shouldn't be shown, but the images should stay accessible. 它不适用于不应显示索引的图像目录,但图像应保持可访问性。

RewriteEngine on
RewriteBase /
RewriteRule ^includes/(.*) http://www.stackoverflow.com/ [R=301,L]
RewriteRule ^includes/(.*) http://www.stackoverflow.com/ [R=301,L]

If you aren't familiar with this you can use http://www.htaccessredirect.net/ 如果您对此不熟悉,可以使用http://www.htaccessredirect.net/

If the file containing the url is on a domain you don't control, there are security risks. 如果包含url的文件位于您无法控制的域中,则存在安全风险。

Instead, store the url once in a file called config.inc.php : 而是将网址一次存储在名为config.inc.php的文件中:

<?php

define('REDIRECT_URL', '/redirect/path');

?>

Then include it in your project: 然后将其包含在您的项目中:

<?php

include 'config.inc.php';

echo REDIRECT_URL;

// /redirect/path

?>

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

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