HTTP POST requests in PHP - php

How do you go about redirecting a browser and sending a HTTP POST request in PHP? A header("Location: file.php?foo=bar") of HTTP POST requests, if you will.

You can't - HTTP does not allow this - if you want to pass arguments via a redirect they have to be embedded into the URL as GET vars.
C.

This does not redirect the browser but it can perform a POST request.
Curl Manual
Curl POST Example
PHP POST Without Curl
To redirect the browser i'd suggest using Javascript.
An example form that does POST and redirect
<FORM action="http://somesite.com/prog/adduser" method="post">
<P>
<LABEL for="firstname">First name: </LABEL>
<INPUT type="text" id="firstname"><BR>
<LABEL for="lastname">Last name: </LABEL>
<INPUT type="text" id="lastname"><BR>
<LABEL for="email">email: </LABEL>
<INPUT type="text" id="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>

I'm not sure why you would have any need for this, however it is not possible in any server-side language.
You could use a javascript library such as jQuery to request a page using a post request

I don't believe you can get a browser to POST data by redirecting it in the middle of a request. You're limited to GET. If you want a browser to POST something you need to construct a <form> and submit it. (Or use an AJAX request.)

PHP doesn't have anything like this. To fulfill your example, you can just simply say $_GET['foo'] = 'bar'; include("file.php"), however the URL given to the browser will not be changed.
Similar question: Code Translation: ASP.NET Server.Transfer in PHP

This question was asked here How do you POST to a page using the PHP header() function?.
Someone commented that if you already had the data why do you need to post it anywhere, why can't you just act on the data in that page?

If you need to transfer data when you redirect without showing data in the URL, you can use $_SESSION, first store data into session then redirect the page, after redirection get data from session and destroy the session data..
OK, if you need to transfer data to other site without showing in the URL, u have to use Javascript instead... like transfer data to payPal. just make a form and write a javascript code to submit the form automatically on page load. below is the sample code:
<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
Search
</form>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>

Related

Cab PHP create an Input field?

I'm trying to create a cookie for a web page. The cookie value will vary based on the users name. Does PHP have an input type function? I just want to add an input field to the page an then the PHP will use that to define the users name for the page. I have the create cookie code, just can't figure out how to get the name from the screen and insert it to the cookie code. Appreciate any suggestions. This is on a WP website.
Not natively because php does not execute in browser, it executes on your server, but it can be used to write an HTML input.
The syntax would look something like this:
echo '<input type="text" name="myinput">';
or
?>
<input type="text" name="myinput">
<?php
You would then use a form post, CURL, or AJAX function to send the data back to the server where a second PHP script would process the input.
That said, it would help to post your create cookie code, since you may not even need to send it back to the server, but just handle it all in the browser using Javascript in which case your submit button only needs to pass the input to a Javascript function instead of posting it.
Is this something you are looking for?
Here it just takes the value user input from the browser and set it as a cookie
<?php
if(isset($_POST['name']) && !empty($_POST['name'])){
setcookie('setcookie_name',$_POST['name']); // setting cookie
}
?>
<form action="" method="post">
<input name="name" value="" placeholder="Enter your name" />
<input name="submit" type="submit" value="Submit"/>
</form>

External form action. Post Validate in PHP. Without Javascript

I have this form:
<form name="form2" method="post" action="http://1.1.101.1/reg.php">
<input id="field12" type="text" value="{$username}" name="username" maxlength="32" placeholder="Username" required="required" />
<input id="field22" type="text" value="{$password}" name="password" maxlength="32" placeholder="Password" required="required" />
<input name="checkbox" type="hidden" id="checkbox" value="checkbox" />
<input type="hidden" name="url" value=""/><br/>
<input type="submit" value="Connect to WiFi" name="button1" /><br/>
</form>
the action is a external url.
How can i check in my php when the button submit is posted (name = button1) before it goes to that url.
Right now i have this but its not working becasuse it goes directly to the action url from the form.
if ($_SERVER['REQUEST_METHOD'] == "post") {
var_dump($_POST);
exit;
}
You can't.
The only way to validate it without using client side code is to submit the form to your own server side code.
You then won't be able to reliably redirect the request while maintaining POST.
You have basically two options the way I see it.
If it's not necessary for the user to see the output of the external script, you could do the posting yourself from your backend. I.e. change the action of your form to your own script and do something like the following:
Validation the fields
If validation OK, POST the data to the external URL via CURL (or similar)
If POST to external URL went OK, redirect to wherever the user should end up in the end
If the user must end up at this external URL, you could do it in two steps. First have your form action set to your own server side validation. If it passes, give the user a confirmation page with a form containing the same data which would then post it to the external URL. The fields should probably be hidden/read-only on this page to prevent them from being changed before the final submit.
This last method is definitely possible to mess with since it's easy to first use valid values, and then change the data in the HTML before doing the final submit. So if security is important here, you're stuck with the first option.
Try this
<?php
if(isset($_POST['button1'])){
//action
header('Refresh:3; url=http://1.1.101.1/reg.php');
}
?>

Automatically log in on another website at the click of a button - cURL or Javascript?

I would like to make a button on my website that automatically logs me in on another website. I recon I can use either Javascript/jQuery or PHP/cURL to do this.
Which is the best way to go?
You may use either remote javascript or iFrame. Find more details here: http://kuza55.blogspot.com/2007/06/building-secure-single-sign-on-systems.html
Also checkout google's approach named SAML: http://code.google.com/googleapps/domain/sso/saml_reference_implementation.html
It depends what the website is. JavaScript and jQuery alone cannot be used due to the cross-domain policy. You could perhaps use a combination of cURL and AJAX to achieve something similar.
I think you might need to provide a little more information about the site, and exactly why you'd want to do this...
I'm not sure if this is exactly what you're looking for, but one thing I have done in the past is to mimic the login form on the site you want to log in to.
For example lets say you want to log in to 'example.com'. In the source code for the login page of 'example.com' you will find the html code for the login form.
Example
<form name="blabla" action="action.php" method="post">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="sumbit" value="Login" />
</form>
Create a form on your site similar to the one you find in 'example.com'. If you want you can even hide the fields, include values to make it a one button login. The trick is making sure that the action has the actual url. For example if the form says 'action.php' for 'example.com' you would put 'http://example.com/action.php'
Example
<form name="blabla" action="http://example.com/action.php" method="post">
<input type="hidden" name="username" value="testuser" />
<input type="hidden" name="password" value="testpass" />
<input type="sumbit" value="Login" />
<form>
This will log you in to the site in most cases. If you don't want to leave your own site you can set a 'target' for the form to an iframe or something.

how to Pass Parameters to php page without having to load it

how can i pass parameters from an html page(map.html) to a php(createxml.php) without having to open the php page? Im incorporating googlemaps in html page(map.html) so i want the users to enter data on a form on the html page which will be sent to php(createxml.php) which in turn will connect to mySQL DB and create an xml format of the response the html page uses this xml output to create positions on the map since it contains longitude and latitude.
I have used the following code in the heading of the php page(createxml), but it shows the contents of php file for a brief moment redirecting me to map.html
Thanks for your time, i can post all the code if needed.
<meta http-equiv="refresh" content="0;url=http://localhost/map.html/">
It's quite simple with AJAX, using jQuery you don't have to know much about it :)
So simply import the latest jQuery Library.
Then you have your form:
<form id="my_form">
<input type="text" name="param1" />
<input type="text" name="param2" />
<input type="hidden" name="action" value="do_stuff" />
<input type="submit" value="Submit" />
</form>
and somewhere beneath that, you just paste this tiny javascript-function, which handles the submit of the form:
<script>
$('#my_form').submit(function(){
var post_params = $('#my_form').serialize();
$('#waiting').show();
$.post('the_page_you_are_on.php', post_params, function(data) {
$('#waiting').hide();
return false;
})
});
</script>
(The element (div, p...) with the id "waiting" could e.g. contain one of those fancy ajax loading images, but is not neccessary! :) If you want one to be shown, find one via google, set it as the background image of the #waiting-element and set its display to none (CSS)).
The function itself just calls the page you're on and then you've got the form variables in your post-array, so the top of your page could look something like this:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'do_stuff' :
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//do some DB-stuff etc.
break;
}
}
?>
I hope that helps!
It's a terrible idea, but because you don't want to use AJAX you could put the PHP in a frame and reload just that portion. Again, awful idea, but the closest you're going to get without using AJAX.
On a useful note though, AJAX is literally just one function in javascript. It's not hard at all to learn.
If you are just trying to pass parameters to a PHP page from the web browser, there are other ways to do it beyond 'Ajax'. Take a look at this page and view the source code (be sure to view the source of the included javascript file: http://hazlo.co/showlist.php?s=chrome&i=4e289d078b0f76b750000627&n=TODO
It uses an extremely basic method of changing the src of an image element, but passes information to the web server (PHP page) in the querystring of the image request. In this example I actually care about the results, which are represented as an image, but it sounds like you are just trying to pass data to the server, so you can return a 1 pixel image if you like. BTW, don't be fooled by the URL that is being used, a server rule is telling apache to process a specific PHP file when check it,GIF is requested.
You should play with it and use firebug or chrome's built in debugger to watch the requests that are being sent to the server.
You can't get any results from a PHP-script if you don't request it and process the output. If you dont't want to leave the current page, you have to use AJAX!
"but it shows the contents of php file for a brief moment" The reason is, that your browser first needs to load the entire page, then start the META-redirect. You don't need a redirect to load data from the server, but if you really want to, you should HTTP-headers for redirect.
Ok guys after hours of headache i finally found the solution! Basically i called my xmlproduce.php from inside my map.html, lemme explain so maybe will help others:
maps.html contained a googlmap API Javascript function which called my createxml.php called second.php
GDownloadUrl("second.php", function(data) )
what i did was i tweaked this call to second.php by adding some parameters in the URL like:
GDownloadUrl("second.php?strt="+ysdate+"/"+msdate+"/"+dsdate+"&end="+yedate+" /"+medate+"/"+dedate+"&id="+ide, function(data)
which is sending the parameters needed by second.php, so after that i made a small html form which called the script of googlemap api on the same file(map.html) to pass the parameters from the form to the GDownloadUrl i mentioned above, the code of the form is :
form method="get" action="">
IMEI: <input type="text" id="id" name="id" size="25" /> <br />
Start Date: <input type="text" id="ysdate" name="ysdate" size="4" value="2000" /> <input type="text" id="msdate" name="ysdate" size="1" /> <input type="text" id="dsdate" name="dsdate" size="1" /> <br/>
End Date: <input type="text" id="yedate" name="yedate" size="4" /> <input type="text" id="medate" name="ysdate" size="1" /> <input type="text" id="dedate" name="dedate" size="1" /> <br/>
<input type="button" value="submit" onClick="load()" />
</form>
afterwards i put extra constraints on the form for the values allowed.
Thanks everybody for the help, and you can always ask if somebody needs some clarification.

What is the purpose of $_POST?

I know it is php global variable but I'm not sure, what it do?
I also read from official php site, but did not understand.
You may want to read up on the basics of PHP. Try reading some starter tutorials.
$_POST is a variable used to grab data sent through a web form.
Here's a simple page describing $_POST and how to use it from W3Schools: PHP $_POST Function
Basically:
Use HTML like this on your first page:
<form action="submit.php" method="post">
Email: <input type="text" name="emailaddress" /> <input type="submit" value="Subscribe" />
</form>
Then on submit.php use something like this:
<?
echo "You subscribed with the email address:";
echo $_POST['emailaddress'];
?>
There are generally 2 ways of sending an HTTP request to a server:
GET
POST
Say you have a <form> on a page.
<form method="post">
<input type="text" name="yourName" />
<input type="submit" />
</form>
Notice the "method" attribute of the form is set to "post". So in the PHP script that receives this HTTP request, $_POST[ 'yourName' ] will have the value when this form is submitted.
If you had used the GET method in your form:
<form method="get">
<input type="text" name="yourName" />
<input type="submit" />
</form>
Then $_GET['yourName'] will have the value sent in by the form.
$_REQUEST['yourName'] contains all the variables that were posted, whether they were sent by GET or POST.
It's used to store CGI input via a POST sent to your page.
Example:
Your page contains:
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
One the user submits the values input into the form, you can access those variables through $_POST using the names you provided for the input tags.
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
You can capture post values from forms:
Example:
<form method="POST">
<input type="text" name="txtName" value="Test" />
</form>
To get this you'll use:
$_POST["txtName"];
It contains data sent by HTTP post, this is most often from a HTML FORM.
<form action="page.php" method="post">
<input type="text" name="email" ...>
...
</form>
Will be accessible by
$_POST["email"]
It contains the data submitted via the POST method, and only the POST method, versus data submitted via the GET method. The $_REQUEST superglobal variable contains both $_POST and $_GET data.
When data is posted through a form to the server, you access it through the $_POST array:
<form method="post">
<p><input type="text" name="firstname" /></p>
<p><input type="submit" /></p>
</form>
--
<?php
if ($_POST)
print $_POST["name"];
?>
Not all data is sent through $_POST through. File uploads are done through $_FILES.
As defined by the Hypertext Transfer Protocol specifications, there are several types of requests that a client (web browser) can make to a resource (web server).
The two most common types of web requests are GET and POST. PHP automatically loads any client request data into the global arrays, $_GET and $_POST, based on the type of web request received. The type of request is transparent to the user of the web browser, and is simply based on what is going on in the page. In general however, any regular link you click produces a GET request, and any form you submit produces as POST request.
If you click a link that goes to "http://example.com/index.php?x=123&y=789", then index.php will have it's $_GET array populated with $_GET['x'] = '123' and $_GET['y'] = '789'.
If you submit a form that has the following structure:
<form action="http://example.com/index.php" method="post">
<input type="text" name="x">
</form>
Then the receiving script, index.php, will have it's $_POST array populated with $_POST['x'] = 'whatever you typed into the textbox named x';
There are two ways of sending data from a form to a web app, GET and POST.
GET sends the data as part of the URL string: http://www.example.com/get.html?fred=1&sam=2 is an example of what that would look like. There are some problems with using it for all processing, one of the biggest is that every browser has a different maximum length for the query string, so you may have your data truncated.
POST sends them separately from the URL. You avoid the short length limit, plus you can send binary or encrypted data with POST.
In the first example above, PHP can retrieve the values sent by $_GET['fred'] and $_GET['sam']. You would use $_POST instead if the form was POSTed.
If you're wondering which method you should use, start here
$_POST is used to retrieve values passed to your page via a POST request.
For example, your page uses a form to pass data to another page in your application. Your form would have
<form method="post">
to pass those values via POST.
It is matched by $_GET which perform the same function for GET requests.
If you want to be able to reference either GET/POST values, you can use $_REQUEST
It contains any values posted from a HTML form to this script.

Categories