Solved

Regex for URL

Posted on 2013-11-18
7
356 Views
Last Modified: 2013-11-19
Hello,

I need to use preg_match('REGEX', $_SERVER['REQUEST_URI']) to report true when the following is true:
The REQUEST_URI string contains */clients/
[a-zA-Z0-9].php*

Where * is an infinite wildcard. The point is that it only matches URLs where it is a .php file in the /clients directory being accessed.

What is the proper REGEX value?

Thank you
0
Comment
Question by:cc01
[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
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39658625
You can use:

^.*?/clients/[a-zA-Z0-9]\.php\b.*$

Open in new window

0
 

Author Comment

by:cc01
ID: 39658694
No go; that .*? should work for a / in front, correct?

This is what I am currently using:
if(preg_match('^.*?/clients/[a-zA-Z0-9]\.php\b.*$', $_SERVER['REQUEST_URI'])) {
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39659084
I think you need the + after the character match. You also need start and end delimiters:

if(preg_match('|^.*?/clients/[a-zA-Z0-9]+\.php\b.*$|', $_SERVER['REQUEST_URI'])) { 

Open in new window

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39659105
hi.. this is your solution

(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

use it...
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39659268
When there is a question like this, there is usually a backstory with a business rule or rule set that the programmer is trying to implement with REGEX, and that's often a bad idea, especially when the subject matter is the REQUEST_URI.  Can you please step back from the technical details and just tell us in plain language what you want to achieve?  There may be better ways to get to the end result.  

And in any case, the rules for URLs are not really quite as simple as you may think.  If you control the creation of the URLs, that is a good step in the right direction, but with REQUEST_URI your rules must also account for (or deliberately ignore) the request variables.

This article has some ideas about how to test the REGEX, but I still think that your business-rules explanation will get you a better approach.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

<?php // RAY_temp_cc01.php
error_reporting(E_ALL);


// http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_28297813.html
// The REQUEST_URI string contains */clients/[a-zA-Z0-9].php*


// A REGULAR EXPRESSION
$rgx
= '#'           // REGEX DELIMITER
. '/clients/'   // EXPLICIT CHARACTER STRING
. '[A-Z0-9]'    // CHARACTER CLASS - LETTERS AND NUMBERS
. '{1,}'        // AT LEAST ONE CHARACTER
. '\.php'       // WITH A PHP SUFFIX
. '#'           // REGEX DELIMITER
. 'i'           // CASE-INSENSITIVE
;

// SOME TEST DATA
$uri = array
( '/clients/xyz.php?admin=1'
, '/clients/login.php?uid=ABC+OR+1+=+1'
, '/admin/clients/delete.php?id=23'
, 'clients.php'                                         // FAILS BECAUSE NO SLASH BEFORE CLIENTS
, '/clients/'                                           // FAILS BECAUSE NO .PHP
, '/clients/healthcare.gov/blunder/obamacare/fail.php'  // FAILS BECAUSE . AND SLASH NOT PERMITTED AFTER /CLIENTS/
, '/clients/images/index.php?q=gooseball'               // FAILS BECAUSE SLASH NOT PERMITTED AFTER /CLIENTS/
)
;

// TEST THE REGEX
echo '<pre>';
foreach ($uri as $str)
{
    echo PHP_EOL . $str;
    if (preg_match($rgx, $str))
    {
        echo " OK";
    }
    else
    {
        echo " FAIL";
    }
}

Open in new window

0
 

Author Closing Comment

by:cc01
ID: 39660923
Thank you for the solution that works and a detailed explanation as well as other advisories.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39660933
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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