Jump to content

Adding to $_POST array is not reflected in $_REQUEST array


snype

Recommended Posts

Hey guys. Just got a quick question that I sure someone has the answer to.

 

Recently I wrote this:

<? echo 'action = '.$_REQUEST['action'].'<br />'; ?>

<?php $_POST['action'] = 'newmember_split'; echo 'action = '.$_REQUEST['action'].'<br />'; ?>

 

My expected output was:

action =

action = newmember_split

 

However my output was:

action =

action =

 

How is this so. As far as I know $_REQUEST is supposed to reflect both $_POST and $_GET. I tried to find some information on it but failed.

 

Can any of you cats help me out?

 

Link to comment
Share on other sites

$_REQUEST An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.

 

in your question logically there request should have a value but (dont know how to explain it properly) request is being set once the value of post data and get  is pass true other page(form action or link using query string ) not by setting it literally

 

sample

when you refresh the page and you see the warning saying about postdata for sure your request will be set.

Link to comment
Share on other sites

The $_REQUEST array will only pick up on variables once a request is made. If you change your code too...

 

<?php

  echo 'action = ' . $_POST['action'];
  $_POST['action'] = 'newmember_split';
  echo 'action = ' . $_POST['action'];

?>

 

You will see the changes.

Link to comment
Share on other sites

The $_REQUEST array will only pick up on variables once a request is made. If you change your code too...

 

<?php

  echo 'action = ' . $_POST['action'];
  $_POST['action'] = 'newmember_split';
  echo 'action = ' . $_POST['action'];

?>

 

You will see the changes.

i guess that was the thread starter trying to say

his confuse because he thought that once the post is set the request should also be set

 

eg..

$_POST['x']='test';

echo $_POST['x'];// test

echo $_REQUEST['x'];// <-- he expect this to echo  test also

Link to comment
Share on other sites

Ok so the $_POST and $_REQUEST Arrays are constructed before this code is invoked. Hence they will not equal each other if a value is changed in one.

 

I get this now. Cheers. But is there a function or process in php to do this? IE postAdd('action', 'newmember_split') which would update all relevant arrays, or would I need to create a custom function to do this?

Link to comment
Share on other sites

ut is there a function or process in php to do this? IE postAdd('action', 'newmember_split') which would update all relevant arrays, or would I need to create a custom function to do this?

 

Why on earth would you need to add data to the $_POST array in the first place? Something sounds illogical.

Link to comment
Share on other sites

No there isn't a function to do this, but you could rewrite the $_REQUEST superglobal to be a reference to $_POST, but then you lose all the information from the $_GET inside it.

Really there should be NO reason under the sun to do this, but what the hey, here's the code so you can mess your scripts up and then ask why they don't work as expected ;)

 

$_REQUEST =& $_POST;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.