Jump to content

[SOLVED] using regex define an array of allowed $_GET values


cprowitt

Recommended Posts

I'm very very new to php and i've been using this code from a tut i found online for one of my pages.  I have a feeling there is a very simple solution for what I want to do.  The code explains itself so i'll get right into it...

 

<?php
// 1. Define an array of allowed $_GET values:
    $pass = array('p01s01','p02s01','p03s01','p04s01','p06s01','p07s01','p10s01','p12s01','p14s01');

// 2. If the page is allowed, include it:
    if (in_array($_GET['id'], $pass)) {
     include ('includes/AprilStories/' . $_GET['id'] . '.php');
    }

// 3. If there is no $_GET['id'] defined, then serve the homepage:
    elseif (!isset($_GET['id'])) {
     include ("includes/blank.php");
    }

// 4. If the page is not allowed, send them to an error page:
    else {
     // This sends the 404 header:
      header("HTTP/1.0 404 Not Found");
     // This includes the error page:
      include ("includes/error.php");
} 
?>

 

I simply want to use regex for the very first part of the code where it defines an array of allowed $_GET values.  Right now i have every page 'id' listed.  I'd like to simplify that with regex so it will match "p[:digit:]{2}s[:digit:]{2}"

 

I've tried everything and still cant get it to work....i think im trying to put the ereg in the wrong place.

 

also here is the url for the page im trying to use regex on... www.obsrep.com/businessjournal

 

thanks!

Link to comment
Share on other sites

ended up doing this...

 

<?php
// Define allowed $_GET values and if allowed, include it:
    if (preg_match( '/p\d{2}s\d{2}/', $_GET['id'] ) && file_exists('includes/AprilStories/' . $_GET['id'] . '.php')) {
     include ('includes/AprilStories/' . $_GET['id'] . '.php');
    }
    
// If there is no $_GET['id'] defined, then serve the homepage:
    elseif (!isset($_GET['id'])) {
     include ("includes/blank.php");
    }

// If the page is not allowed, send them to an error page:
    else {
     // This sends the 404 header:
      header("HTTP/1.0 404 Not Found");
     // This includes the error page:
      include ("includes/error.php");
}
?> 

 

...if anyone cares

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.