/    Sign up×
Community /Pin to ProfileBookmark

Sending POST variables

How do you send post variables in php using header? If you can’t, how do you do it some other way? I do not want to use forms or javascript or anything.

I got this code off of a [URL=http://www.faqts.com/knowledge_base/view.phtml/aid/12039/fid/51]website[/URL], but there are two problems:
– It’s unacceptably slow, and
– It doesn’t actually redirect when I call the function!

Here’s the code:

[code=php]
function sendToHost($host,$method,$path,$data,$useragent=0)
{
// Supply a default method of GET if the one passed was empty
if (empty($method)) {
$method = ‘GET’;
}
$method = strtoupper($method);
$fp = fsockopen($host, 80);
if ($method == ‘GET’) {
$path .= ‘?’ . $data;
}
fputs($fp, “$method $path HTTP/1.1rn”);
fputs($fp, “Host: $hostrn”);
fputs($fp,”Content-type: application/x-www-form- urlencodedrn”);
fputs($fp, “Content-length: ” . strlen($data) . “rn”);
if ($useragent) {
fputs($fp, “User-Agent: MSIErn”);
}
fputs($fp, “Connection: closernrn”);
if ($method == ‘POST’) {
fputs($fp, $data);
}

while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
return $buf;
}
[/code]

What should I do??????

to post a comment
PHP

31 Comments(s)

Copy linkTweet thisAlerts:
@bokehJul 10.2006 — It's not possible to do that because [B]header('Location: [...]');[/B] diverts to a URL and URLs don't contain POST data. Why would you want to do such a thing anyway?
Copy linkTweet thisAlerts:
@shane_carrauthorJul 10.2006 — Why would you want to do such a thing anyway?[/QUOTE]

I don't want to pass them by GET, and I need variables passed to activate appropriate code on the page.


It's not possible to do that because header('Location: [...]'); diverts to a URL and URLs don't contain POST data.[/QUOTE]

So is there any other method?
Copy linkTweet thisAlerts:
@sridhar_423Jul 10.2006 — y dont u use simpe HTML instead of header if you're very particular to [B]POST[/B] the variables.. (if it suits.)

echo "<body onload='document.formName.submit()'>";

echo "<form name='formName' action='$URL' method='POST'>";

exit;

would like to know if there's some equivalent.
Copy linkTweet thisAlerts:
@shane_carrauthorJul 10.2006 — I do not want to use forms or javascript or anything.[/QUOTE]
• Don't respond saying "Why don't you ..." when I state in my previous post "I don't want to ..."[/QUOTE]

I'll use that as a last resort, but thanks for the thought. ?


Any more suggestions?
Copy linkTweet thisAlerts:
@sridhar_423Jul 10.2006 — hmm.. missed those sentences in your post.. sorry
Copy linkTweet thisAlerts:
@bokehJul 10.2006 — I need variables passed to activate appropriate code on the page.[/QUOTE]Why is the script using the POST method? Is the outcome of the script idempotent?
Copy linkTweet thisAlerts:
@shane_carrauthorJul 10.2006 — [code=php]
if($_POST['whattodo'] == "functiona"){
echo "Hello! How are you today?";
} else {
echo "Goodbye!";
}
[/code]


That's like the code on the headered page.
Copy linkTweet thisAlerts:
@bokehJul 10.2006 — [code=php]
if($_POST['whattodo'] == "functiona"){
echo "Hello! How are you today?";
} else {
echo "Goodbye!";
}
[/code]


That's like the code on the headered page.[/QUOTE]
Well that is not idempotent so should be using the GET method.
Copy linkTweet thisAlerts:
@felgallJul 10.2006 — You can use sessions to pass things between pages server side without them showing in the URL.
Copy linkTweet thisAlerts:
@sridhar_423Jul 10.2006 — here what's required is to pass some variables without any "Javascript"/"forms" to a diffrent page. -- Nothing to do with vaiables stored on Server/ already posted by another page.

Adde to misery--

A Header are used to navigate to a different page.

As far as my knowledge is concerned, this is not possible.
Copy linkTweet thisAlerts:
@NogDogJul 10.2006 — You can add a 307 header, but the browser will ask the user if he wants to resend post data, which could be confusing/problematic:
[code=php]
header("HTTP/1.0 307 Temporary redirect");
header("Location: http://www.example.com/filename");
exit();
[/code]
Copy linkTweet thisAlerts:
@shane_carrauthorJul 11.2006 — Thanks everyone! The more specific problem is that I [I]already have working code on the targeted page[/I], and I don't want to change it. I am simply adding a feature. Otherwise, GET data would be okay (not great) (the data being passed includes usernames, passwords, etc. I know I should probably use something more secure, but post data is [I]somewhat[/I] more secure that get, and, no one ever uses my site anyway, so it doesn't really matter).


So, after reading everything, I realize that I can't use header(). Is there some other function I could use? What about that monstrosity of code in the first post? How do I use the 307 header?
Copy linkTweet thisAlerts:
@shane_carrauthorJul 11.2006 — ?

Thanks, everybody! Your comments are vary much appreciated! ?


:mad: My problem still isn't solved. Can anybody show me even how to transfer [I]one or two[/I] POST variables? It is obviously possible; it has been done before.


NogDog, the confirmation boxes, although not ideal, would be satisfactory. The only thing is, how do you send them using that method???


I think the code that I posted in the first post will work, but how do you make it actually reload the browser???


Thanks again for all of the help so far! ?



Other members, feel free to help me out too!

Visitors, go ahead and join if you know the answer!




[COLOR=SlateGray]_[/COLOR]
Copy linkTweet thisAlerts:
@SkywoolfJul 12.2006 — I am very much a beginner but I have actually just done something similar for my users to use a large database. It took days and I still don't understand it all so please understand if my explanation is not so good.

If the user will click a button - just set up a form with all or some hidden fields containing the data. Form action"page.php" opens page.php and "post" sends the data. The posted data is picked up on the target page with something like

$Family=$_POST['Family'];

$Vehicle=$_
POST['Vehicle'];

There is a simple example here: http://www.freewebmasterhelp.com/tutorials/phpmysql

I needed to generate drop down menus using data from the database and managed to get the selected data to pass on to the next page but could not figure out how to also send the hidden fields with other essential variables. I eventually solved that by using cookies.
Copy linkTweet thisAlerts:
@shane_carrauthorJul 12.2006 — That sounds good, but is there a way to dynamically submit the forms without using JavaScript?


[color=white].[/color]
Copy linkTweet thisAlerts:
@bokehJul 12.2006 — No!

The Javascript is simple.
Copy linkTweet thisAlerts:
@SkywoolfJul 12.2006 — That sounds good, but is there a way to dynamically submit the forms without using JavaScript?


[color=white].[/color][/QUOTE]


I didn't use any Java Script. It is all HTML. It just needs a click on a button.
Copy linkTweet thisAlerts:
@bokehJul 12.2006 — I didn't use any Java Script. It is all HTML. It just needs a click on a button.[/QUOTE]Wellthat could be combined with a javascript auto submit so the button only shows if javascript is disabled.

It seems to me though that something is very wrong with the logic if any of this is necessary.
Copy linkTweet thisAlerts:
@shane_carrauthorJul 12.2006 — I don't know why, but some people have javascript disabled. I want my program to work for almost everybody. If the form is the only option, I guess I could do that, with the javascript auto-submit; if someone doesn't have javascript enabled, I guess I could do [U]<noscript><input type='submit' value='Submit' /></noscript>[/U], but the feature is almost pointless if it doesn't submit automatically. And what about that [U]sendToHost();[/U] function? It seems like it should work; I guess the speed is okay, but what function do I have to put it in to make it redirect? Do I use it first then header the page ?
Copy linkTweet thisAlerts:
@bokehJul 12.2006 — Some people have javascript disabled. I want my program to work for almost everybody.[/QUOTE]Well almost everyone has javascript enabled. Javascript is considered part of the default interface. Do you really want to be programming your site so it works flawlessly for people that delibrately disable components of the default interface? It just means these "clever" individuals will need to press a button. And don't believe any rubbish about accessibility. All modern software to aid people with accessibility problems does not have any problem running Javascript.
Copy linkTweet thisAlerts:
@SkywoolfJul 12.2006 — i am sure there are much better ways and that Java Script may well be the best solution but for me...

I am 62 so my memory is getting bad. I have built a number of very successful sites with only a basic understanding of HTML but I cannot do it without a WYSIWYG program like Dreamweaver.

I am now building a very large site that seamlessly combines a Portal, Forums and Classifieds from three different software companies, The site runs in English, two versions of Chinese and Tagalog with user switching of all at once (when all the translation is complete) there are multiple themes and about 8 MySQL databases so far. ?

I am under pressure to get much more done in a short time so I have to look for shortcuts. There is no time for learning Java Scripts, CSS, PHP, CGI, etc etc. I just learn as much as I have to to get the job done.

Forums like this where I can get advice from more experienced developers is very valuable to me.
Copy linkTweet thisAlerts:
@shane_carrauthorJul 13.2006 — I greatly appreciate bokeh and Skywoolf's help. However, I think my main question still awaits to be answered.

I got this code off of a website, but there are two problems [U][B][I]etc...[/I][/B][/U][/QUOTE]

I think the code that I posted in the first post will work, but how do you make it actually reload the browser???[/QUOTE]

And what about that [U]sendToHost();[/U] function? It seems like it should work; I guess the speed is okay, but what function do I have to put it in to make it redirect? Do I use it first then header the page ?[/QUOTE]
Copy linkTweet thisAlerts:
@bokehJul 13.2006 — There is one other way you can do it. Have a page proxy the request. It's pretty simple reallybut you would have to add a base tag to the output so all the relative links still worked.

The client recieves a header() containing the variables in GET form. Your proxy script retrieves the page based on these GET variable using the POST method and then displays it to the client. I could show you the code but want to know this method is acceptable to you first.
Copy linkTweet thisAlerts:
@NogDogJul 13.2006 — Thanks everyone! The more specific problem is that I [I]already have working code on the targeted page[/I], and I don't want to change it.[/QUOTE]
That may be so, but now that you've spent all this time trying to find a work-around to changing that page and are still not satisfied, it begs the question: would you already have a working application if you'd just gone ahead and modified that page with the additional functionality that you need?
Copy linkTweet thisAlerts:
@bokehJul 13.2006 — would you already have a working application if you'd just gone ahead and modified that page with the additional functionality that you need?[/QUOTE]
I must say I agree. Before we continue looking for solutions can you explain why that page cannot be modified? Is it a page you do not own?
Copy linkTweet thisAlerts:
@balloonbuffoonJul 13.2006 — You might be able to use the CURL extension, but it probably isn't already installed on your server, and I'm not sure if its exactly what you're looking for. This post from the documention of the CURL ext in the manual seems like an easy way to implement it:

http://us2.php.net/manual/en/ref.curl.php#65700

But nonetheless, it seems you should focus on editting the other page to accomodate what you're trying to do.

--Steve
Copy linkTweet thisAlerts:
@shane_carrauthorJul 14.2006 — Okay, I guess I will adapt the file to use get variables. However, POST variables would still be the greatest option, as for the variables passed are usernames and passwords to my system. I don't really want those showing in the address bar. I don't plan to spend much time learning sessions. So, I'll use GET data until someone replies with a good way to pass POST variables. Also, NogDog, could you kindly take the " using header()" out of the name of this thread? Thanks for your help everyone!
Copy linkTweet thisAlerts:
@balloonbuffoonJul 14.2006 — Well, like I said, the CURL extension supports passing POST variables, its just up to you to install it on your server and figure out how to use it.

--Steve
Copy linkTweet thisAlerts:
@shane_carrauthorJul 14.2006 — I might try that; I've already tried it, and it was an undefined function, probably because CURL isn't instaled. I could probably figure out how to do it, but this is a fairly small problem and I don't want to spend a very long time doing it. However, that function in my first post didn't throw any errors, it just didn't do anything. How do I make it refresh to another page?


I'm still open to suggestions! ?


[color=White].[/color]
Copy linkTweet thisAlerts:
@shane_carrauthorJul 28.2006 — Okay, I resorted to just using the [echo "<script>document.forms[0].submit()</script>"] method. Thanks everyone! ?
Copy linkTweet thisAlerts:
@bokehJul 28.2006 — Okay, I resorted to just using the [echo "<script>document.forms[0].submit()</script>"] method. Thanks everyone! ?[/QUOTE]The legacy DOM!
×

Success!

Help @shane_carr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...