Domanda

Can any one please tell me how I can remove the slash from the end of a variable

In my index.php I have the following:

$url=$_SERVER['REQUEST_URI'];
include_once "sites/$url.php";

My problem is if I write example.com/test/somefile/ nothing comes but if I write example.com/test/somefile it works

So is there a way to remove the slash if the variable ends with a slash?

È stato utile?

Soluzione 2

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.

Altri suggerimenti

Please do not do this.

You are relying on your user being a goody two shoes and not futzing with requests.

In conclusion: DO NOT rely on browser requests to include a file in your code

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top