0

I have the following code:

<?php
    define('ENVIRONMENT', 'tests');
    $_POST['id']='AccountPagesView.a_book/45';
    $_POST['old_value']='1';
    $_POST['value']='2';
    header("Location: http://localhost/index.php/welcome/update_record");
?>

I need to set $_POST array in this script and load script by url. But the script from url tells me that $_POST array is null. Why? How can I set the $_POST array and send it to script by url? Thank you in advance.

UPDATE: I have some code which must be tested, and there is some script on the url "http://localhost/index.php/welcome/update_record", and it uses values from $_POST array; so, I can't change this script, and I want to test it. How can I do it?

UPDATE2:

<?php
    //include ('\application\controllers\welcome.php');
    define('ENVIRONMENT', 'tests');
    $_POST_DATA=array();
    $_POST_DATA['id']='AccountPagesView.a_book/45';
    $_POST_DATA['old_value']='1';
    $_POST_DATA['value']='2';
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/index.php/welcome/update_record');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST_DATA);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_exec($ch);
?>
8
  • Did you mean to override the parameters, and instead include() the update script from the current webserver? Then don't use the URL, use the filesystem path to do so. Commented Jul 1, 2012 at 14:57
  • Please, are there any ways to send $_POST array into another script without forms? May be JS, or other mean? Commented Jul 1, 2012 at 15:57
  • Yes, there are. Please refrain from comment spamming though. Commented Jul 1, 2012 at 15:59
  • OK, sorry, but I'm confused. Could you describe this mean please? Commented Jul 1, 2012 at 16:02
  • No, you have to enhance your question first. Explain why you need that specific data transfer, can't use the answers given so far, and how the two application parts are to be connected. Else your question is too broad. -- Use the edit link. -- To get higher quality answers, write a higher quality question by including sample code that is complete yet concise, etc. Commented Jul 1, 2012 at 16:04

5 Answers 5

4

You cannot. A redirect will always result in the target page loaded via GET.

However, you could use the session to store these values. Call session_start(); on both pages and use the superglobal array $_SESSION instead of $_POST.

Sign up to request clarification or add additional context in comments.

2 Comments

Please, are there any ways to send $_POST array into another script without forms? May be JS, or other mean?
If you want it to be redirect-ish you need to create a form (possibly with only hidden fields) and then submit it via JavaScript.
1

I believe this is what you need to send POST values from one PHP script to another, without using JS, If you absolutely don't want to use $_SESSION though that is what you should be using.

$ch = curl_init();
$data = array('id' => 'AccountPagesView.a_book/45', 'old_value' => '1', 'value' => '2',);

curl_setopt($ch, CURLOPT_URL, 'http://path-to/other.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

Comments

0

$_POST only exists if the request was sent through the POST method, which means a form was sent.

You could use $_SESSION instead.

1 Comment

Please, are there any ways to send $_POST array into another script without forms? May be JS, or other mean?
0

This is one of the prime reason why $_SESSION's should be used. See this other question for an explanation: PHP - Pass POST variables with header()?

<?php
session_start();
define('ENVIRONMENT', 'tests');
$_SESSION['id']='AccountPagesView.a_book/45';
$_SESSION['old_value']='1';
$_SESSION['value']='2';
header("Location: http://localhost/index.php/welcome/update_record");

Then on index.php/welcome/update_record

<?php
session_start();
define('ENVIRONMENT', 'tests');
$id = $_SESSION['id'];
$old_value =  $_SESSION['old_value'];
$value = $_SESSION['value'];

//do something

1 Comment

Please, are there any ways to send $_POST array into another script without forms? May be JS, or other mean?
0

Two answers. If the following does not work:

<?php
    define('ENVIRONMENT', 'tests');
    $_POST['id']='AccountPagesView.a_book/45';
    $_POST['old_value']='1';
    $_POST['value']='2';
    require("/index.php/welcome/update_record");
?>

(I am a bit flabbergasted about the page URL.)

Then:

As you insist on POST (as is your right in asking), you can do:

<html>
  <head>
  </head>
  <body>
    <form action="/index.php/welcome/update_record" method="post">
      <input type="hidden" name="id" value="AccountPagesView.a_book/45">
      <input type="hidden" name="old_value" value="1">
      <input type="hidden" name="value" value="2">
      <input type="hidden" name="ENVIRONMENT" value="tests">
    </form>
    <script type="text/javascript">
       document.forms[0].submit();
    </script>
  </body>
</html>

Where that define of ENVIRONMENT needs to be solved somehow.

If the target script uses $_REQUEST which is $_POST + $_GET (i.o. $_POST) then you do a HTTP GET URL: ...-?id=...&old_value=1&value=2 which would be the simplest solution.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.