Posting VARS via PHP header?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

aluminumpork
Forum Newbie
Posts: 14
Joined: Mon Oct 24, 2005 8:54 pm

Posting VARS via PHP header?

Post by aluminumpork »

I'm looking for a way to post variable to a page via PHP. It has to POST like a normal form does though, POST while redirecting to the page. Posting in the background via fopen (or similar) is of no use to me.

I can't pass via get (header('Location: page.php?variable=data')) because this is data sensitive. Is there a way to do with without any javascript?

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

header() cannot post to another page.

cURL, Snoopy, fsockopen() and similar functions or libraries are you choices.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

cURL.
aluminumpork
Forum Newbie
Posts: 14
Joined: Mon Oct 24, 2005 8:54 pm

Post by aluminumpork »

Thanks. Before I look into those libraries though, can any of those post to a page AND redirect to it at the same time?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

POST is used to send data to a server. A redirection is a header sent to the client.
Therefore the answer is probably "no".
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

aluminumpork wrote:Thanks. Before I look into those libraries though, can any of those post to a page AND redirect to it at the same time?
POSTing to a page is going to return you the contents of that page. It sounds like you might be trying to send POST data from within PHP, and then redirect the client's browser or something like that?
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Maybe using GET to put it in the URL would be a better solution.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Posting VARS via PHP header?

Post by RobertGonzalez »

From the original poster's original post:
aluminumpork wrote:I can't pass via get (header('Location: page.php?variable=data')) because this is data sensitive.
aluminumpork
Forum Newbie
Posts: 14
Joined: Mon Oct 24, 2005 8:54 pm

Post by aluminumpork »

Okay, here's the entire issue. I submit a form on page a to php script, the php script grabs data from mysql and then must return the data to the original page. Under normal circumstances, I wouldn't do it this way, but I'm testing out a way to have a fail-safe for AJAX methods. The form (when javascript is enabled) posts to the php script via xmlhttprequest, the php scripts returns the mysql data via xml and javascript makes the necessary changes to the page. If javascript is disabled, the form submits as normal to the php script, currently the php script return the same data (that would have normally been return via xml) with GET (header(Location: originalPage.php?vars)), then original page checks to see if any of the variables exists in GET (but i would prefer POST) and outputs them. Therefore older browsers, text based browsers or people that have javascript turned off get the same page no matter what.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

how much data is being changed, if it is almost the entire page you should just reload the page entirely and lose the javascript.

ie. <form method="POST" name="whetever"> No action, so it posts to itself.
aluminumpork
Forum Newbie
Posts: 14
Joined: Mon Oct 24, 2005 8:54 pm

Post by aluminumpork »

Well right now, only a few pieces of data are being changed on the page. But I'm building to work in any situation. It's just simply a backup mechanism to AJAX.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There are size limitations on GET params. Will your app be able to accommodate that if it grows?
aluminumpork
Forum Newbie
Posts: 14
Joined: Mon Oct 24, 2005 8:54 pm

Post by aluminumpork »

That's why I'm asking if there's any way PHP can simulate a POST. I have to use GET right now because I know of no other way to doing it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No, PHP cannot simulate a post. It happens on the server, where as posting originates on the client. Javascript can simulate post, but it is not reliable as not all users have it enabled.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

why not something like

Code: Select all

<html>
	<head>
		<title>---</title>
		<script type="text/javascript">
			function foo() {
				...
				xmlHttp.open("post","bar.php?xmlrequest=true",true);
				xmlHttp.send(data);
				return false;
			}
		</script>
	</head>
	<body>
		<form method="post" action="bar.php" onsubmit="javascript:foo()">
			<input type="text" name="xyz" />
			<input type="submit" />
		</form>
	</body>
</html>
?
If bar.php is invoked with ?xmlrequest=true it sends the ajax-version, if there's no such GET parameter it sends the complete html document.
Post Reply