Match an array name with a post value

I’m having trouble matching a post with a session array.
the session name holds the post value.

so

$_SESSION['forms']['inputname'] = '123abc456xyz';
<input type="submit" name="123abc456xyz" value="abc123xyz456" />

which is a result of

<input type="submit" name="<?php echo $_SESSION['forms']['inputname']; ?>" value="" />

i have three names. i tried to check for a match using array options from php manual. i could read only one name. all three posts echo the same name.

for example:

$input = $_SESSION['forms'];
  foreach ($_POST as $input) {
      if (in_array($_POST, $input)) {
          $request = array_search($_POST, $input);
      }
  }

i had it matching only the first session, so it isn’t matching the post that was submitted. I am not very experienced with arrays, so i am struggling to figure out a method that works.

astonecipher and benanamen know my code all ready, for anyone else that can help: the name and value of the input is randomized, so only the session key can separate the posted form.

i need a way to match the post with the session key, so value to name.

okay, so i restructured my code because it is obviously stupid code. now i see a match but how can i get the name of the key?

$input = $_SESSION['forms'];
foreach ($_POST as $key) {
    if (in_array($key, $input)) {
        echo $key . '<br><br>';
        print_r($input); exit;
    }
}

i now see the post value and the session name with that value.
so i need to get the name of that key. i thought that array_search will get that for me.

i got it to work by adding the following code:

$request = array_search($key, $input);

i guess this post is solved, unless there is a better method.
Thank you.

You shouldn’t unconditionally loop over all the $_POST data. Anyone/anything can submit thousands (php has put in a limit, but it could be set to anything) of pieces of data in an attempt to trigger errors or cause your code to do unnecessary processing.

You should loop over an array of expected form fields. Your $_SESSION[‘forms’] is such an array, with the real name as the key and the random/field name as the value.

What is the goal of the current code? To produce an array with the real names and the submitted values for use in the rest of the code?

$post = []; // a working copy of the submitted post data, with with the real names as keys/indexes.

// loop over the defining array
foreach($_SESSION['forms'] as $real_name=>$field_name)
{
	if(isset($_POST[$field_name]))
	{
		$post[$real_name] = $_POST[$field_name];
	}
}

Elements in $post would be used in the rest of the code.

1 Like

Hello phdr and i hope that you are having a pleasant day,

you should know by now that i always implement any code that you offer to solve a problem. I didn’t think about setting another array. i wish you could see my code but i don’t want to disturb you with pms. astonecipher has a copy of my blueprint code if you are curious.

i actually set a session variable that i use to test if the particular form was submitted. so i submit my form to processRequest.php which then passes the verified request to what i call indexRouter, which in turn loads the appropriate page. like so:

if (isset($_SESSION['Path']) && !empty(isset($_POST[$_SESSION['Path']['url']]))) {
  HandleRequest();
}

then HandleRequest function is where i am trying to implement this array check. I have three forms which correspond to the three taxonomical ranks that i am working with: Animalia, Fungi and Plantae Kingdoms. Each form has a random name and value, so the only way that i will know which form was submitted is via the session variable key. The session variable key holds the input name. Then i use this info to call a function which verifies the random input name and value, then a second function which verifies the csrf token. If everything passes, then i set a session variable for the index router. The key of the session variable is actually a directory path and filename. so the session key is named “Animalia/Chordata/Aves” for example. Then i can fetch the associated page. Ultimately i eliminate the need for query strings, input value tampering and directory traversal, as well as not needing hyperlinks or directories. So, i guess, my own prg form style router framework. I like it but i find it difficult to accomplish certain tasks. I lack enough backend experience.

so you think that testing if one of the posts isset is not good enough?
i’ve entered your code into my function but i’m a little stumped as how to call the other two functions because i still don’t have the session key name. I’ll tinker with your code and see what i am missing.

Thank you so much for taking time to help me. I am always honoured when you weigh in on a post. I doff my hat to you, Sir.

so i tried your code everyway that i could think of to get the name of the session key to no avail. I added the array search and the code is working well. is it okay to use the search? am i missing something?

so now my code is this:

function HandleRequest() {
  $post = [];
  foreach ($_SESSION['Path'] as $key=>$value) {
      if (isset($_POST[$value])) {
          $post[$key] = $_POST[$value];
          $request = array_search($post[$key], $_SESSION['Path']);
          //check button and csrftoken
          break;
      }
  }
  return;
}

Hello phdr and i hope that you are having a pleasant day,

your code does store the key, so i am able to retrieve the name/key of the session variable without the array search. I had an error in my code which prevented this from working, therefore i assumed that the name was not available. my apologies for this error. your code is working as intended. However, i have no need to store the post data because i can access it with this name/key. So my final working code looks like this:

function HandleRequest() {
  foreach ($_SESSION['Path'] as $name=>$value) {
      if (isset($_POST[$value])) {
          list($request) = explode("-", $name);
          //validate button and csrf token
          //assign name to session variable for index router (showPage)
          break;
      }
  }
  return;
}

Thank you very much. I now have a working animal router. so i am able to traverse my taxonomy. I’ve finally reached this stage of development, which makes me very happy. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service