Samuel
Samuel

Reputation: 18586

Sending POST data without form

Can i send for example a string or another piece of information to another .php file without it being exposed [thus not by GET but by POST conform to what i know] without using a form?

Upvotes: 45

Views: 215724

Answers (8)

Mohamed Essam
Mohamed Essam

Reputation: 42

I have another solution, you can insert a form in your html code and just hide the input

<!-- HTML code -->
    
<?php

$passedDate = 'your date';


echo '

<form action="secondPage.php" method="post">
      <input style="display:none" type="text" name="data" value="' . $passedData . '">
      
  <button type="submit" value="submit">
  
  
  </form>'
?>

<!-- HTML code -->

then you can easly get that passed date in the second page without need input from the user

<?php

echo $_POST['data'];

Upvotes: 0

Abdulbasit
Abdulbasit

Reputation: 780

I would highly recommend using curl in such situation, file_get_content() does work but not at all times, and it could be troublesome to use it in some applications.

Though curl comes in different variations depending on what you want to send and in what method, here is the most common method of posting your data without HTML form using curl.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://example.com/request_uri');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    $post = array(
        'data1' => 'value1',
        'data2' => $value2
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    
    //do what you want with the responce
    var_dump($result)

Upvotes: 1

user11713698
user11713698

Reputation: 21

function redir(data) {
  document.getElementById('redirect').innerHTML = '<form style="display:none;" position="absolute" method="post" action="location.php"><input id="redirbtn" type="submit" name="value" value=' + data + '></form>';
  document.getElementById('redirbtn').click();
}
<button onclick="redir('dataToBeSent');">Next Page</button>
<div id="redirect"></div>

You can use this method which creates a new hidden form whose "data" is sent by "post" to "location.php" when a button[Next Page] is clicked.

Upvotes: 2

wpcoder
wpcoder

Reputation: 1054

Simply use: file_get_contents()

// building array of variables
$content = http_build_query(array(
            'username' => 'value',
            'password' => 'value'
            ));
// creating the context change POST to GET if that is relevant 
$context = stream_context_create(array(
            'http' => array(
                'method' => 'POST',
                'content' => $content, )));

$result = file_get_contents('http://www.example.com/page.php', null, $context);
//dumping the reuslt
var_dump($result);

Reference: my answer to a similar question:

Upvotes: 10

Gregor
Gregor

Reputation: 622

Send your data with SESSION rather than post.

session_start();
$_SESSION['foo'] = "bar";

On the page where you recieve the request, if you absolutely need POST data (some weird logic), you can do this somwhere at the beginning:

$_POST['foo'] = $_SESSION['foo'];

The post data will be valid just the same as if it was sent with POST.

Then destroy the session (or just unset the fields if you need the session for other purposes).

It is important to destroy a session or unset the fields, because unlike POST, SESSION will remain valid until you explicitely destroy it or until the end of browser session. If you don't do it, you can observe some strange results. For example: you use sesson for filtering some data. The user switches the filter on and gets filtered data. After a while, he returns to the page and expects the filter to be reset, but it's not: he still sees filtered data.

Upvotes: 12

Andreas Linden
Andreas Linden

Reputation: 12721

have a look at the php documentation for theese functions you can send post reqeust using them.

fsockopen()
fputs()

or simply use a class like Zend_Http_Client which is also based on socket-conenctions.

also found a neat example using google...

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

You could use AJAX to send a POST request if you don't want forms.

Using jquery $.post method it is pretty simple:

$.post('/foo.php', { key1: 'value1', key2: 'value2' }, function(result) {
    alert('successfully posted key1=value1&key2=value2 to foo.php');
});

Upvotes: 24

Stephen Holiday
Stephen Holiday

Reputation: 745

If you don't want your data to be seen by the user, use a PHP session.

Data in a post request is still accessible (and manipulable) by the user.

Checkout this tutorial on PHP Sessions.

Upvotes: 27

Related Questions