Solved

if not

Posted on 2014-12-01
4
73 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 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 40473573
Try this:

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

Open in new window

0
 

Author Closing Comment

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

But thanks anyway
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40473680
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
ID: 40473691
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

932 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