谁能告诉我如何从变量末尾删除斜杠
在我的 index.php 中,我有以下内容:
$url=$_SERVER['REQUEST_URI'];
include_once "sites/$url.php";
我的问题是,如果我example.com/test/somefile/
什么都没写,但如果我写example.com/test/somefile
它就可以了
那么如果变量以斜杠结尾,有没有办法删除斜杠?
谁能告诉我如何从变量末尾删除斜杠
在我的 index.php 中,我有以下内容:
$url=$_SERVER['REQUEST_URI'];
include_once "sites/$url.php";
我的问题是,如果我example.com/test/somefile/
什么都没写,但如果我写example.com/test/somefile
它就可以了
那么如果变量以斜杠结尾,有没有办法删除斜杠?
请不要这样做。
你依赖于你的用户是一个很好的两双鞋,而不是满足于请求。
结论:不要依赖浏览器请求在代码中包含文件
Try this
$url = rtrim($url, '/');
From PHP.net http://ua2.php.net/rtrim
> You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
While this will solve your problem, please take a few minutes to consider the warnings posted in the comments and the other answer(s) regarding code injection since it is a very serious security issue.