0

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?

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
KaareZ
  • 615
  • 2
  • 10
  • 22

2 Answers2

4

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

qwertynl
  • 3,912
  • 1
  • 21
  • 43
0

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.

Andresch Serj
  • 35,217
  • 15
  • 59
  • 101
Dean Whitehouse
  • 884
  • 1
  • 8
  • 25