Solved

if not

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

785 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