10 Replies - 19729 Views - Last Post: 20 July 2009 - 10:38 AM Rate Topic: -----

#1 nemosis  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 24-June 09

how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 08:03 AM

I have been having some difficulties of finding a solution using PHP and POST data.
I've looked at php.net and searched through several websites without any luck.

I want my php page to send post data like a html form can do. Does anyone know how and if you do can you show me some php that will accomplish the task.

This is some code that doesn't work the way I need it to.
  function do_post_request($url, $data, $optional_headers = null)
  {
	 $params = array('http' => array(
				  'method' => 'POST',
				  'content' => $data
			   ));
	 if ($optional_headers !== null) {
		$params['http']['header'] = $optional_headers;
	 }
	 $ctx = stream_context_create($params);
	 $fp = @fopen($url, 'rb', false, $ctx);
	 if (!$fp) {
		throw new Exception("Problem with $url, $php_errormsg");
	 }
	 $response = @stream_get_contents($fp);
	 if ($response === false) {
		throw new Exception("Problem reading data from $url, $php_errormsg");
	 }
	 return $response;
  }



Is This A Good Question/Topic? 0
  • +

Replies To: how do I send post data to a server and redirect to it?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,082
  • Joined: 08-August 08

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 08:23 AM

Use session variables.
Was This Post Helpful? 0
  • +
  • -

#3 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 08:41 AM

Are you sending it to your own site or another site?
If you're using your own site then just forget the whole POST data thing because it'd be a waste of time, use sessions like CT said. Otherwise you'd probably wanna use CURL.

NOTE: CURL is NOT enabled by default on most webservers. So this will not work unless you've customized your mods.

I use a function something like this:
function send_post($url, $data){
$ch = curl_init();	// initialize curl handle 
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to 
curl_setopt($ch, CURLOPT_FAILonerror, 1); //Fail on error
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable 
curl_setopt($ch, CURLOPT_POST, 1); // set POST method 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // add POST fields
$result = curl_exec($ch); // run the whole process 
curl_close($ch);
return $result;
}


Then you could say like...
$url = "http://somesite.tld/someForm.php";
$post = "foo=bar&someOtherVariable=2&OneMoreVariable=3";
$foobar = send_post($url, $post);
//print $foobar; //Uncomment if you wanna show the page after the post data is sent.


And that'll basically send the post variables "foo" "someOtherVariable" and "OneMoreVariable" as "bar" "2" and "3" (respectively) to the url http://somesite.tld/someForm.php.

I hope that helps.

Yours,
Shane~

This post has been edited by ShaneK: 19 July 2009 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 nemosis  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 24-June 09

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 10:33 AM

I want to send the user from one site to another, then back to the orginal site.

Is there another way without using curl or session variables?

All I would like is to pass POST data from site.com/index.php to different.com/redirect.php to site.com/redirected.php

I have used this code to redirect a user, but it only works when javascript is turned on. Sorry about the code not being neat.
echo "<script type=\"text/javascript\">";
echo "document.write(\"<form method=\\\"post\\\" action=\\\"" . $redirect . "\\\"><input type=\\\"text\\\" value=\\\"" . $_POST[$user] . "\\\" name=\\\"" . $user . "\\\"><input type=\\\"text\\\" value=\\\"" . $_POST[$pass] . "\\\" name=\\\"" . $pass . "\\\"><input type=\\\"submit\\\" value=\\\"Login\\\"></form>\");";
echo "document.forms[0].submit();";
echo "</script>";



I need code that works when javascript is turned off.
That's my dilemma.

Are there any other ways I could use POST like I would with a <form></form> without using curl or session variables?
Was This Post Helpful? 0
  • +
  • -

#5 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 11:30 AM

Well, my change site function is like:
function change_site($w_site){
	return '<script type="text/javascript">window.location = "'.$w_site.'";</script>
	<noscript>
	<meta http-equiv=\'refresh\' content=\'0;url='.$w_site.'\'/>
	</noscript>';
}


The Javascript part isn't even needed, I don't know why I keep it there, but yeah...
Anyway, just output:
<meta http-equiv='refresh' content='0;url=http://redirectedSite.com'/>


Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#6 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 11:52 AM

You can send a user from your site to another, but unless you control the other site you can't send him back, you should be able to send a post request though, here's a guide a quick google search came up with:
http://www.jonasjohn...ost-request.htm. Haven't tested it, but I can't see why it shouldn't work! Try it! :)

View Postnemosis, on 19 Jul, 2009 - 07:33 PM, said:

I want to send the user from one site to another, then back to the orginal site.

Is there another way without using curl or session variables?

All I would like is to pass POST data from site.com/index.php to different.com/redirect.php to site.com/redirected.php

I have used this code to redirect a user, but it only works when javascript is turned on. Sorry about the code not being neat.
echo "<script type=\"text/javascript\">";
echo "document.write(\"<form method=\\\"post\\\" action=\\\"" . $redirect . "\\\"><input type=\\\"text\\\" value=\\\"" . $_POST[$user] . "\\\" name=\\\"" . $user . "\\\"><input type=\\\"text\\\" value=\\\"" . $_POST[$pass] . "\\\" name=\\\"" . $pass . "\\\"><input type=\\\"submit\\\" value=\\\"Login\\\"></form>\");";
echo "document.forms[0].submit();";
echo "</script>";



I need code that works when javascript is turned off.
That's my dilemma.

Are there any other ways I could use POST like I would with a <form></form> without using curl or session variables?

Was This Post Helpful? 0
  • +
  • -

#7 nemosis  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 24-June 09

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 12:02 PM

I figured out that I don't need to worry about the users not having javascript on. I'm going to make the site check the login form with javascript, so they need javascript anyway.

Thanks for your input.

I also found that I could use
 function do_post_request($url, $data, $optional_headers = null)
  {
	 $params = array('http' => array(
				  'method' => 'POST',
				  'content' => $data
			   ));
	 if ($optional_headers !== null) {
		$params['http']['header'] = $optional_headers;
	 }
	 $ctx = stream_context_create($params);
	 $fp = @fopen($url, 'rb', false, $ctx);
	 if (!$fp) {
		throw new Exception("Problem with $url, $php_errormsg");
	 }
	 $response = @stream_get_contents($fp);
	 if ($response === false) {
		throw new Exception("Problem reading data from $url, $php_errormsg");
	 }
	 return $response;
  }



Then use a meta redirect if they don't have javascript.
If I was going to do that then I shouldn't even need javascript in the first place.

Thanks for your input ShaneK.
Was This Post Helpful? 0
  • +
  • -

#8 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 12:17 PM

If it is possible I would suggest to use PHP for the redirection instead of meta or javascript, since it's much cleaner and faster. :)

View Postnemosis, on 19 Jul, 2009 - 09:02 PM, said:

I figured out that I don't need to worry about the users not having javascript on. I'm going to make the site check the login form with javascript, so they need javascript anyway.

Thanks for your input.

I also found that I could use
 function do_post_request($url, $data, $optional_headers = null)
  {
	 $params = array('http' => array(
				  'method' => 'POST',
				  'content' => $data
			   ));
	 if ($optional_headers !== null) {
		$params['http']['header'] = $optional_headers;
	 }
	 $ctx = stream_context_create($params);
	 $fp = @fopen($url, 'rb', false, $ctx);
	 if (!$fp) {
		throw new Exception("Problem with $url, $php_errormsg");
	 }
	 $response = @stream_get_contents($fp);
	 if ($response === false) {
		throw new Exception("Problem reading data from $url, $php_errormsg");
	 }
	 return $response;
  }



Then use a meta redirect if they don't have javascript.
If I was going to do that then I shouldn't even need javascript in the first place.

Thanks for your input ShaneK.

Was This Post Helpful? 0
  • +
  • -

#9 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 12:25 PM

View PostWimpy, on 19 Jul, 2009 - 12:17 PM, said:

If it is possible I would suggest to use PHP for the redirection instead of meta or javascript, since it's much cleaner and faster. :)


If you're suggesting headers('Location:~~~~'); then also know that it has to be the first thing output (which can be a real pain in the ass).

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#10 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: how do I send post data to a server and redirect to it?

Posted 19 July 2009 - 12:41 PM

It can most certainly be a real pain in the ass but it's a really great way of doing redirection business! :)

View PostShaneK, on 19 Jul, 2009 - 09:25 PM, said:

View PostWimpy, on 19 Jul, 2009 - 12:17 PM, said:

If it is possible I would suggest to use PHP for the redirection instead of meta or javascript, since it's much cleaner and faster. :)


If you're suggesting headers('Location:~~~~'); then also know that it has to be the first thing output (which can be a real pain in the ass).

Yours,
Shane~

Was This Post Helpful? 0
  • +
  • -

#11 nemosis  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 24-June 09

Re: how do I send post data to a server and redirect to it?

Posted 20 July 2009 - 10:38 AM

Thanks for input.
I used
http://www.jonasjohn...ost-request.htm
and
header("Location: http://www.site.com/posted.php");
To post the form, and to redirect the user.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1