Question

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?

Was it helpful?

Solution 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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top