使用PHP Switch语句将表单数据发送到随机URL

So i am trying to wrap my head around this logic. this is code that is on a form processing script. What i want to do is send form data to one of these urls randomly. I was reading to use the switch case logic, but when i use the code you see below, it submits the form data to all 3 URLS. Is there a way so that it only sends to one of them?

function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) { 
  $fields .= $key . '=' . $value . '&'; 
}
rtrim($fields, '&');

$post = curl_init();

curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, 1);
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);


$result = curl_exec($post);

curl_close($post);
}



return $result;

$x = rand(1,3);

switch ($x) {
    case 1:
        post_to_url("http://examplesite1.com/cgi-bin/maxuseradmin.cgi", $data2);
        break;

    case 2:
        post_to_url("http://examplesite2?fid=6646588e54", $data3);
        break;

    case 3:
        post_to_url("http://examplesite1?fid=2fb44e3888", $data4);
        break;
    }

the $data variables are arrays --Thanks for the help

You're posting to all 3 pages, then setting the value of what it returns to $ar1 $ar2 and $ar3.

Store your options in an array, then call post_to_url() once.

$urls = array(
    array("http://examplesite1.com/cgi-bin/maxuseradmin.cgi", $data2),
    array("http://examplesite2?fid=6646588e54", $data3),
    array("http://examplesite1?fid=2fb44e3888", $data4)
);

$x = rand(0,2);
post_to_url ($urls[$x][0], $urls[$x][1]);

The issue here is that you are calling the functions before the switch statement even happens. This code should work for you as it calls the functions only inside the switch statement:

$x = rand(1,3);

switch ($x) {
    case 1:
        post_to_url("http://examplesite1.com/cgi-bin/maxuseradmin.cgi", $data2);
        break;

    case 2:
        post_to_url("http://examplesite2?fid=6646588e54", $data3);
        break;

    case 3:
        post_to_url("http://examplesite1?fid=2fb44e3888", $data4);
        break;
}

In the first 3 lines you calling your post_to_url function

Change your code to

$urls = array(
   "http://examplesite1.com/cgi-bin/maxuseradmin.cgi",
   "http://examplesite2?fid=6646588e54",
   "http://examplesite1?fid=2fb44e3888"
);

# don't know what's in $data* but just to give you an idea
$data = array(
    $data2,
    $data3,
    $data4
);
$x = rand(0,2);
post_to_url($urls[x], $data[x]);

Call to the function post_to_url should be inside the switch case statement. The reason it posts to all 3 is that you are calling this function 3 times even before going to switch case.

You are actually executing your post_to_url function three times on the first three lines. You would want to do something like:

$x = rand(1,3);

switch ($x)
{
case 1:
post_to_url("http://examplesite1.com/cgi-bin/maxuseradmin.cgi", $data2);
break;

case 2:
post_to_url("http://examplesite2?fid=6646588e54", $data3);
break;

case 3:
post_to_url("http://examplesite1?fid=2fb44e3888", $data4);
break;
}