Page 1 of 2
Posting VARS via PHP header?
Posted: Mon Jan 29, 2007 4:30 pm
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.
Posted: Mon Jan 29, 2007 4:32 pm
by feyd
header() cannot post to another page.
cURL,
Snoopy,
fsockopen() and similar functions or libraries are you choices.
Posted: Mon Jan 29, 2007 4:39 pm
by RobertGonzalez
cURL.
Posted: Mon Jan 29, 2007 4:46 pm
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?
Posted: Mon Jan 29, 2007 4:49 pm
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".
Posted: Mon Jan 29, 2007 5:14 pm
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?
Posted: Mon Jan 29, 2007 5:22 pm
by tail
Maybe using GET to put it in the URL would be a better solution.
Re: Posting VARS via PHP header?
Posted: Mon Jan 29, 2007 5:25 pm
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.
Posted: Mon Jan 29, 2007 5:26 pm
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.
Posted: Mon Jan 29, 2007 5:38 pm
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.
Posted: Mon Jan 29, 2007 5:48 pm
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.
Posted: Mon Jan 29, 2007 6:23 pm
by RobertGonzalez
There are size limitations on GET params. Will your app be able to accommodate that if it grows?
Posted: Mon Jan 29, 2007 6:38 pm
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.
Posted: Mon Jan 29, 2007 7:04 pm
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.
Posted: Mon Jan 29, 2007 8:24 pm
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.