PHP redirection with post values - php

Is it possible to redirect user to external server without using:
header('Location:address.com?var=1');
or
HTML form with JS auto submit
Seems really simple but can't figure it out. I do not want to use GET, I would rather send it via headers/post if possible.
Making ajax request is slowing it down and I believe there should be some kind of solution in pure PHP.
CURL is not an option as I want to go to that link with posted data.
Cheers

You can do this using this method session_set_cookie_params(). It is used to transfer data from 1 url to another url using session. To get the result you have to write this function before every session_start().
session_set_cookie_param();
#session_start();

Related

send GET request without loading page in php

Hi i have a form after the submit i want to run url in background with out goin to the page it self
if(isset($_POST['submit'])){
http://url/default/en_US/send.html?u=admin&p=hodamai&l=1&n=01211200671&m='
}
I think you want an AJAX-Request to your server. That means your browser makes a background-request to your server and execute the script while you're still on your homepage. Then you'll get the response as JSON. If you want, you could use fetch as well, but I prefer AJAX because even IE supports that. I think both are easy to use ;)
https://www.w3schools.com/xml/ajax_intro.asp

POST in PHP AND redirect the user to that page?

I'm a bit confused. How do I POST data to a URL and then redirect the user's browser to that location - all in one operation?
I see
header('Location: page.php?' . http_build_query($_POST));
but that is GET, not POST and ppl think thats really bad practice - PHP open another webpage with POST data (why?)
My kludgy workflow involves setting up a form and then submitting it via javascript - anything has to be better than that...
I think I can do a set of header() stmts but this action happens for the user way after the page has been geenrated, so i dont think that would work
You cannot redirect POST requests. As simple as that. Any redirect will always turn into a GET request.
If you want to receive POST data, then send that data to another page, you have two choices:
if both pages are on the same server, use sessions to save the data server-side, don't make the client carry it over
if the destination is on another server and you need to send the client there together with the data, set up another intermediate form like you are
Use AJAX to save the data before you leave the page. Use the answer you get back to fire a redirection to the new url right within the current page. Don't be affraid of Javascript and ajax. Try this light AJAX library: http://www.openjs.com/scripts/jx/

Monitoring for postbacks with PHP?

I have a PHP page with content that my users can view. When this page receives a POST request from a certain external URL, I'd like to redirect the user to another page.
The problems I'm' having are:
How can I monitor the page for requests being sent in an efficient way?
How can I actually redirect them since header() doesn't work.
Thanks.
For the second question, header() will work if you use it before you output any information to the page. However, if you echo, print, or have HTML before it, it will give an error.
Edit: In response to Toad's comment, then you'd have to do as Aaron Harun suggested. Have the page save the $_POST data to the database or a file (make sure you sanitize it!), just like you would with any $_POST data. You would then need to use AJAX to get a response from a second PHP page that simply checks for the existence of updated data from wherever you saved it. If the response comes back true, then you redirect using a JavaScript redirect.
The only other way to do it without using AJAX would be to refresh the page using an HTML meta refresh element at timed intervals to check if $_POST data has been received. HTML would perform the refresh, PHP would do the checking, and you could use either for the redirect.
You can use an AJAX script to "ping" the server intermittently to see if there have been any changes. If there are, redirect the user with JavaScript.
It's pretty much the only way.
we can put this snippet to check the post request
if($_POST['flag']==1) {
header("location:newpage.php");
exit();
}
But If you want to check the request regularly without user interaction than you will have to use AJAX

PHP: Simulate a <form> tag with POST data and also redirecting the user

I have been scouring the internet for a way to do this with no luck :(
Basically, it's easy to make a form in HTML with a submit button and some data, and send it to a URL. With this you send the POST data and also the user is taken to the page.
I know you can send POST data using cURL and get a response back in PHP but how do I take the user there, I need to simulate exactly what a tag does in php.
Some sample code or links would be great!
If you want the user to see the resulting page, you have two options:
Proxy the result to the user. This isn't as simple as it sounds, due to link URLs and whatnot, and might not even be what you want.
Don't use PHP at all, create the form in HTML (lots of hidden inputs) and submit it via JavaScript.
In cURL, use curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);. This will display the result to screen rather than returning it to a string. If the other site typically redirects after the form is submitted, then the redirect will occur as well and will take you to the new page.
If you really need to redirect the user to another page after the submission (instead of just showing the result on the same page), you can use a header ("Location: http://www.yourdomain.com/resultpage"); after the PHP handles the cURL call with the POST data. I'm curious though why you do not want to output the results of the cURL call immediately.
It sounds like you're consuming a web service. Are you?
Maybe a more detailed description of what you are trying to achieve would be helpful.

Can you post data from PHP

I need to cause a user's browser to post data using PHP to another site.
Example: You go to start.com/auto-login-hack (via GET)... then PHP sets the right headers etc. and causes the browser to, via POST, go to 3rdparty.com/login.php with login credentials.
I have done this is the past by having an HTML form and an onload script that submits the form to the destination.
I don't know enough about headers and etc. Is this possible? Can anyone link an example? My search skills just turned up how to use $_POST.
Thanks.
Yes, you can submit POST requests from PHP.
One of your choices is to use curl as shown in this SO question.
However, you cannot do redirects.
You cannot redirect to a POST; this is a limitation of HTTP. You'd have to use JavaScript to cause the browser to post a form.

Categories