views:

119

answers:

8

I am trying to get this form to:

if any $_POST vars equals any other of the $_POST vars throw an error.

if it was just a few it wouldnt be an issue but I have about 20 or so so if i wanted to do it I would have to go like

    <?php 
    if($_POST['input1']==$_POST['input2'] || $_POST['input1']==$_POST['input3']){
die('whatever');
}

    ?>

But that's not good coding (and it would take forever) I thought about arrays and different ways...

but I am brain dead atm so I thought I could get some help.. thanks in advance

ps it would be nice to do it in PHP (server side) but Jquery is always an option.

+14  A: 

Delete duplicate values with array_unique() and check if it still equals to your array:

if($_POST != array_unique($_POST))
    die("...");
Colin Hebert
+1! That's the money right there!
LeguRi
I like the looks of that.... so I can use it EXACTLY as you have it there?? or do I have to do if($_POST['a']!=array_unique($_POST))????
Luke3butler
You can directly use this
Colin Hebert
K im trying it.....
Luke3butler
I will give a warning, the function does cast each element as a string before doing a === operation. This will only be good for post variables , but you might get a few problems if you wander into other arrays with casted types or special formatting.
ok works great... only 1 problem.... I forgot that SOME can be the same..... any suggestions??... would a $post_a=$_POST['a']; unset($_POST['a']); and then set it back after the check work??
Luke3butler
You can still do an `array_diff($_POST, array_unique($_POST))` and check duplicate keys.
Colin Hebert
for some odd reason that is NOT working...
Luke3butler
Can you give more details about the thing that doesn't work?
Colin Hebert
answered... posted my code below
Luke3butler
Again, do `$duplicatesArray = array_keys (array_diff($_POST, array_unique($_POST)))` check these values, if they're on your blacklist, then you call the die() method.
Colin Hebert
+1  A: 
function testPost(){
foreach ($_POST as $keya=>$vala){
    foreach($_POST as $keyb=>$valb){
        if ($keya==$keyb){
            continue;
        } else {
            if ($vala == $valb){
                 return FALSE;
            }
        }
    }
}
return TRUE;
}
akellehe
A: 
$postValues = array_values($_POST);

if (array_unique($postValues)) != $postValues) {
    die('error!');
}

not so good, but

bobrik
A: 

I'd say you have to do it in a double-loop; while it's O(n^2), it'll not be problematic in practice

foreach($_POST as $keya=>$vala) {
    foreach($_POST as $keyb => $valb) {
        if($vala == $valb && $keya != $keyb) {
            die('whatever');
        }
    }
}
zebediah49
+2  A: 

if ($_POST == array_unique($_POST)) {}

What does `[array_unique][1]($_POST)` do? Haven't seen that syntax.
Rocket
produces parse error for me under PHP 5.3
Yorirou
Oh, I see why the post is written as `[array_unique][1]($_POST)`.The `[array_unique][1]` is supposed to be a link, but Sarfraz edited the post and messed up the formatting.
Rocket
rolled it back ;)
nikic
Hoolagins messing with my posts!!! Shoo! Shoo!!
A: 

You can do:

$count_array = array_count_values($_POST);
foreach($_POST as $post) {
  if($count_array[$post] >1 ) {
    die();
 }
}
codaddict
A: 

This is only an answer to zebediah49's post. A more effective implementation would be:

$post = array_value($_POST);
$count = count($post);
for ($i = 0; $i < $count; ++$i) {
    for ($j = $i + 1; $j < $count; ++$j) {
        if ($post[$i] == $post[$j]) {
            die();
        }
    }
}

This saves all the multiple checks. So it results in O(2*n) instead of O(n^2) (if I got that O stuff right). Though I don't know how much this is slowed down by the additional array_values.

nikic
A: 

This is my final code... so basically i'm putting the ones I want into an array

credits will go to @akellehe because his was the closest to my end result....

works great

$titles=array();
        $num=1;
        while($num!=15){
        $set1='title'.$num;
        $set2=$_REQUEST["title$num"];
        $titles[$set1]=$set2;
        unset($set1);
        unset($set2);
        $num+=1;
        }
        foreach($titles as $d => $p){
            foreach($titles as $e =>$q){
            if($p==$q && $d!=$e){
            if(!empty($p)){
            $_SESSION['error']='Duplicates not allowed!';
            }
            }
            }
        }
Luke3butler