Solved

validate php url parameter list

Posted on 2014-02-02
2
424 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
2 Comments
 
LVL 34

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now