?
Solved

if not

Posted on 2014-12-01
4
Medium Priority
?
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

741 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