Can you execute a post command from code?

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

User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Can you execute a post command from code?

Post by icarpenter »

Hi I was wondering if there is any way that you can execute a post command from code...

For example I would like to be able to send a variable value to another page without physically pushing a form button.

I can do this via a <meta> appending the variable to the URL see below.

Code: Select all

echo"<META HTTP-EQUIV=Refresh CONTENT='0; URL=http://mypage.php?variable=value'>";
But I would like to keep the variables hidden.

I would be greatful for any ideas!
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

you could always use sessions to pass the varialbes around if you need to.
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

Yeah I could write to a session variable and call the page via the <meta> method, but would me much cleaner to use a POST method if one exists.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I was once saw this somewhere - someone was doing this but had problems - I was very surprised to find that it could be done that way.
I copied some code but didn't copy the link.
This is the code.

Code: Select all

$ReqHeader =
      "POST $URI HTTP/1.0\n".
      "Host: $Host\n".
      "User-Agent: PostIt\n".
      "Content-Type: application/x-www-form-urlencoded\n".
      "Content-Length: $ContentLength\n\n".
      "$ReqBody\n";

//     echo $ReqHeader;
Let me try searching for the exact link.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

use a <div> and refresh the content by using javascript's submit() function.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

patrikG wrote:use a <div> and refresh the content by using javascript's submit() function.
Would this method only refresh the information that is in the current <div> and would yo have a code example?
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

[quote="anjanesh"]I was once saw this somewhere - someone was doing this but had problems - I was very surprised to find that it could be done that way.
[quote]

Anjanesh have tried this but just seems print a new page and doesn't post any values...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

fsockopen() is used to send HTTP requests amongst other things ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

or curl
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Code: Select all

header("Location: mypage.php?variable=value");
exit;
I've never had any problems with that. But a clever person could still read the variable..
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

[quote="onion2k"]

Code: Select all

header("Location: mypage.php?variable=value");
exit;
Tried that but got an error:
Warning: Cannot modify header information - headers already sent by (output started at C:\mypage.php:9)

Do I have to kill the existing header somehow?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

that is correct. the header() must be the first thing that is being sent.
do a search on this site there are lots of questions and anwsers about using header()
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Make sure theres no output (to the browser) before header() call.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

Check out this link for some functions I wrote to do a POST using fsockopen viewtopic.php?t=7063 You could also use CURL or snoopy.
Post Reply