Pass $_post Variables Without Forms

by on Jan.11, 2008, under PHP Code


Ever wanted to send variables to another script without clogging up the URI? Well the following is a function which allows you to do just that:


To use this function just call it 😉

 $something = sendPost($URL_of_the_file_you_want_to_pass_the_variables_to, $an_array_containing_the_variables_you_wish_to_send)

Note: The array needs to be made up in the following way:

$context = array(
	'post_variable1' => 'post_value1',
	'post_variable2' => 'post_value2',
	'post_variable3' => 'post_value3'


function sendPost($url, $context)
	$context = array_change_key_case($context, CASE_LOWER);
	$contextSize = sizeof($context);		
	$contextKeys = array_keys($context);
	$data = "";
	foreach ($contextKeys as $key)
		$amp = "&";
		if (next($contextKeys) === FALSE) { $amp = ""; }
		$data .= urlencode($key)."=".urlencode($context[$key]).$amp;

	list($host,$url) = explode('/',$url,2);
	$sock = fsockopen($host,80,$errno,$errstr,1);
	if (!$sock) { die("$errstr ($errno) in ".__FILE__." (".__LINE__.")\n"); }
	$http_request = "POST /".$url." HTTP/1.1\r\n";
	$http_request .= "Host: ".$host."\r\n";
	$http_request .= "Content-type: application/x-www-form-urlencoded\r\n";
	$http_request .= "Content-length: " . strlen($data) . "\r\n";
	$http_request .= "Accept: */*\r\n";
	$http_request .= "\r\n";
	$http_request .= "$data\r\n";
	$http_request .= "\r\n";
	$response_headers = array();
	while ($str = trim(fgets($sock)))
	$errors = true;
	$errorMsg = "";
	foreach($response_headers as $response_header){
		if($response_header == 'HTTP/1.1 200 OK'){
			$errors = false;
		}elseif($response_header == 'X-UFU: OK'){
			$errors = false;
	if (!$errors)
		return true;
		return false;

Enjoy and please free to leave a comment if you use this code

VN:F [1.9.22_1171]
Rating: 5.0/5 (2 votes cast)
Pass $_post Variables Without Forms, 5.0 out of 5 based on 2 ratings

2 Comments for this entry

  • tyllee

    >$context = array(
    > ‘post_variable1’ = ‘post_value1’,
    > ‘post_variable2’ = ‘post_value2’,
    > ‘post_variable3’ = ‘post_value3’

    Is this realy correct? Shouldn’t there be a ; in the end? When I’m running the code it complains about =.

    error msg: Parse error: syntax error, unexpected ‘=’, expecting ‘)’

    I’ve tryed to put “” around the array elements like this. “‘post_variable1’ = ‘post_value1′”, This makes it run but stops whith this

    error msg: Parse error: syntax error, unexpected T_VARIABLE poiting to the line where I have $something = sendPost($URL, $context)

    my code

    include “../scripts/sendPost.php”;


    $context = array (
    “‘test1’ = $test1”,
    “‘test2’ = $test2”,
    “‘test3’ = $test3″


    $something = sendPost($URL, $context)


    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • GizzmoAsus

    You are indeed correct the array does need the ; at the end of it. The other thing I forgot to add into it was the > signs which are used to assign the values to the keys.

    So the context array should look like:
    $context = array (
    ‘test1’ => $test1,
    ‘test2’ => $test2,
    ‘test3’ => $test3

    But you can also define it in the following way.
    $context[‘test1’] = $test1;
    $context[‘test2’] = $test2;
    $context[‘test3’] = $test3;

    The quotes around the values denotes that the values are strings if you are using variables then you don’t need the quotes. So your variable declarations need to be:
    $test1 = ‘test1’;
    $test2 = ‘test2’;
    $test3 = ‘test3’; offers some good documentation on arrays that you might find useful

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!