Php redirect from form

PHP — Redirect and send data via POST

I have an online gateway which requires an HTML form to be submitted with hidden fields. I need to do this via a PHP script without any HTML forms (I have the data for the hidden fields in a DB) To do this sending data via GET:

header('Location: http://www.provider.com/process.jsp?id=12345&name=John'); 

14 Answers 14

You can’t do this using PHP.

As others have said, you could use cURL — but then the PHP code becomes the client rather than the browser.

If you must use POST, then the only way to do it would be to generate the populated form using PHP and use the window.onload hook to call javascript to submit the form.

Well thats weird that cURL isnt the solution as I have used it myself to do exactly this. But I guess your solution works as well.

You can use jQuery $.post method. In this case you can choose between options to stay on this site or redirect when submited.

Yes, its possible to proxy the request and convert it into a POST using PHP and curl — but that implies that the receiving end is susceptible to CSRF and/or is not applying a session based controls over the request (you can fudge some of this, but not all in the parameters passed to curl)

1) there was very limited supprot for 308 redirects in 2015 2) a permanent redirect is almost always the wrong answer to any problem

Читайте также:  Проверить на симметричность матрицы python

here is the workaround sample.

function redirect_post($url, array $data) < ?>   function closethisasap() 
$v) < echo ''; > > ?>

Its a «dirty» solution, but definitely works! I made a similar function but with an explicit echo instead of closing and reopening the tags

A better and neater solution would be to use $_SESSION:

and for the redirect header request use:

header('Location: http://www.provider.com/process.jsp?id=12345&name=John', true, 307;) 

307 is the http_response_code you can use for the redirection request with submitted POST values.

Another solution if you would like to avoid a curl call and have the browser redirect like normal and mimic a POST call:

save the post and do a temporary redirect:

function post_redirect($url)

Then always check for the session variable post_data :

if (isset($_SESSION['post_data']))

There will be some missing components such as the apache_request_headers() will not show a POST Content header, etc..

It would involve the cURL PHP extension.

$ch = curl_init('http://www.provider.com/process.jsp'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "id=12345&name=John"); curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); // RETURN THE CONTENTS OF THE CALL $resp = curl_exec($ch); 
/** * Redirect with POST data. * * @param string $url URL. * @param array $post_data POST data. Example: array('foo' => 'var', 'id' => 123) * @param array $headers Optional. Extra headers to send. */ public function redirect_post($url, array $data, array $headers = null) < $params = array( 'http' =>array( 'method' => 'POST', 'content' => http_build_query($data) ) ); if (!is_null($headers)) < $params['http']['header'] = ''; foreach ($headers as $k =>$v) < $params['http']['header'] .= "$k: $v\n"; >> $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx); if ($fp) < echo @stream_get_contents($fp); die(); >else < // Error throw new Exception("Error loading '$url', $php_errormsg"); >> 

I’m new to PHP. Is there any way to change the URL with it? This function doesn’t change the URL, but redirects. I’m leaving the $headers as null (should I be?).

Note that you could also use an array for the CURLOPT_POSTFIELDS option. From php.net docs:

The full data to post in a HTTP «POST» operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like ‘para1=val1&para2=val2&. ‘ or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

Источник

PHP redirecting from submitted form [closed]

My php file works fine but I cant figure out how to make it redirect to another page once it has been submitted? Code is below, thank you!

Try if(mail($to, $subject, $message)) <. >instead. Plus, you may be outputting before header. Add error reporting to the top of your file(s) right after your opening

If there is text (message), submit form to the email address, php is a new venture for me so I may have this misunderstood but it seems to deliever the message?

I don’t know, does it deliver the message, and are you not using error reporting as I stated earlier?

2 Answers 2

The main issue you will have with your code is that you do «sunny-day» coding, where the world is nice and dandy and everything will work just fine. However, one small problem and your code will break! You need to always program for worst case scenarios as much as you can!

window.location='".$success."';"; > else < //headers have not been sent yet, we can use header(). header("Location: sent.html"); >> else //our mail did not go, lets die() with an error < die("Unable to send email. Please check mail settings."); //instead of the above die() you could redirect to a proper error page. //I am leaving that out for brevity's sake >> else < die("Where art thou, O' elusive message?"); //instead of the above die() you could redirect to a proper error page. //I am leaving that out for brevity's sake. >?> 

As you can see, one of the fundamental ideas behind programming is to think about most of the things that can go wrong, when writing a code, and write accommodating conditions, so that they get handled gracefully.

Источник

HTML Form + PHP, Page Redirect

and a small PHP script that sends me an email. I am wondering how to redirect the user to a Thank You page. Any suggestions? Sorry, here is the PHP script:

I want to redirect back to a local page, my index page that is located one folder above my php script. The problem with the header is that it can’t go above the directory that it is currently in; maybe I should add a thank you html page in that folder and redirect? Or is there a better way?

3 Answers 3

After your php sends the email, add this php snippet:

header("HTTP/1.1 303 See Other"); header("Location: /thank-you.php"); exit(); 

In the php script, you can set an HTTP redirect header like so:

There are a couple caveats, however:

  1. You can print no output before the header statement. Since this is an HTTP header, printing content will end the header and begin the body.
  2. This won’t work in an AJAX script, since you will redirect a page that is never rendered in the browser. If you do use AJAX, you can set window.location.href to the page you want to redirect to.

The only reason I am confused by this is why is every single redirect that I generally see done to another PHP script? I simply want to redirect to another HTML page, which I’m not sure I can do.

The redirect will be handled by Apache, so you can redirect to any URL. It can be a PHP script, HTML file, image, video, etc.

With php, redirects are easy. After the logic of your small script is done, use the header() function.

header('Location: http://www.yourlocation.com/there/myfile.html'); 

This sends a redirect header that will cause the targeted page to load. You can target any address. However, the script will continue to execute, so don’t assume this ends it. Any code after this will run. Also, headers have to be sent before any output, so no HTML and be sure that you don’t echo anything or have any spaces before the opening

The manual has more about header() as well.

Источник

PHP Redirect to another page after form submit

I have read all your posts about inserting headers into a php form file in order to redirect the user to another URL AFTER the form is submitted — but I can’t figure out how to do it. Below is my code. Can you show me where to put the header/redirect so that the information gets e-mailed and then the user goes to another html page?

 
"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); > // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['company']) || !isset($_POST['street']) || !isset($_POST['city']) || !isset($_POST['state']) || !isset($_POST['zip'])) < died('We are sorry, but there appears to be a problem with the form you submitted.'); >$first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // required $company = $_POST['company']; // required $street = $_POST['street']; // required $city = $_POST['city']; // required $state = $_POST['state']; // required $zip = $_POST['zip']; // required $error_message = ""; $string_exp = "/^[A-Za-z0-9 .'-]+$/"; if(!preg_match($string_exp,$first_name)) < $error_message .= 'The First Name you entered does not appear to be valid.
'; > if(!preg_match($string_exp,$last_name)) < $error_message .= 'The Last Name you entered does not appear to be valid.
'; > $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]$/'; if(!preg_match($email_exp,$email_from)) < $error_message .= 'The Email Address you entered does not appear to be valid.
'; > if(!preg_match($string_exp,$company)) < $error_message .= 'The Company you entered does not appear to be valid.
'; > if(!preg_match($string_exp,$street)) < $error_message .= 'The Street you entered does not appear to be valid.
'; > if(!preg_match($string_exp,$city)) < $error_message .= 'The City you entered does not appear to be valid.
'; > if(!preg_match($string_exp,$state)) < $error_message .= 'The State you entered does not appear to be valid.
'; > if(!preg_match($string_exp,$zip)) < $error_message .= 'The Zip Code you entered does not appear to be valid.
'; > if(strlen($error_message) > 0) < died($error_message); >$email_message = "Response from Mailing List Page. Please enter in database.\n\n"; function clean_string($string) < $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); >$email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Company: ".clean_string($company)."\n"; $email_message .= "Street: ".clean_string($street)."\n"; $email_message .= "City: ".clean_string($city)."\n"; $email_message .= "State: ".clean_string($state)."\n"; $email_message .= "Zip: ".clean_string($zip)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> Thanks for subscribing to our mailing list ?>

Источник

Оцените статью