Solved

if not

Posted on 2014-12-01
4
71 Views
Last Modified: 2014-12-01
Trying to do a simple test to see if a page has a / at the end of it:-
        if (!substr($_SERVER['REQUEST_URI'], -1) == "/") {
             die("unknown Request");
        }

Open in new window


However every time it runs with or without a / it never runs the code. Even if I echo out the response I can see a / (or not, depending on how Im trying to debug) so its reading it right, but not running the if if it doesnt exist.

What am I missing here?
0
Comment
Question by:tonelm54
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
Comment Utility
Try this:

        if (substr($_SERVER['REQUEST_URI'], -1) != "/") {
             die("unknown Request");
        }

Open in new window

0
 

Author Closing Comment

by:tonelm54
Comment Utility
That works, but still dont understand why mine didnt work :-S

But thanks anyway
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Look here:
        if (! (substr($_SERVER['REQUEST_URI'], -1) == "/") ) {
             die("unknown Request");
        }

Open in new window

The operator not (!) must be paced in front of the entire expression:

                                       if ( not ( is last char equal to slash? ) ) then...

This way it works, but it can be a bit confusing.
Thanks for points!
0
 
LVL 9

Expert Comment

by:Brian Tao
Comment Utility
I know you've got an accepted solution, but I would just like to let you know what happened in your not-working code:
// in your code, PHP is evaluating !substr($_SERVER['REQUEST_URI'], -1) and compares it to "/", 
// so no matter what is in your REQUEST_URI, the left side evaluates to FALSE and never equals "/"
// this never "die" ... ^^
if (!substr($_SERVER['REQUEST_URI'], -1) == "/") {
     die("unknown Request");
}

// wrap the equation with a pair of parentheses and put the ! outside of it, and then it should work correctly
if (!(substr($_SERVER['REQUEST_URI'], -1) == "/")) {
     die("unknown Request");
} 

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now