Solved

validate php url parameter list

Posted on 2014-02-02
2
430 Views
Last Modified: 2014-02-02
Hi,
I need help determining if the list of parameters being passed in a url are valid

I have a list of valid parameters
define("validParms","item1,item2,item3");

I want a test to check the parameters being passed in a url
dosomething.php?item1=abc&item2=data
are legitimate.

ie
I want this to cause an error because it has an 'illegal' param
dosomething.php?item1=data&item2=data&noParam=data
0
Comment
Question by:joomla
[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 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 100 total points
ID: 39828631
So you want to throw an error if the query string (the $_GET array) contains a value that you are not expecting?

Try this:

<?php

$validParms = array("item1", "item2", "item3");
$invalidParms = array_diff(array_keys($_GET), $validParms);

if(count($invalidParms) > 0)
{
  echo "The following parameters are invalid:\n";
  print_r($invalidParms);
  die();
}


?>
0
 

Author Closing Comment

by:joomla
ID: 39828641
perfect
great example
thanks
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

615 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