How can form values be sent to a script on a different domain without switching to that domain or using JavaScript?
When dealing with cross-domain form submissions without using JavaScript, one common approach is to utilize a server-side proxy script. This script acts as an intermediary between the form submission and the target domain, allowing the form values to be sent securely without directly interacting with the target domain. By sending the form data to the proxy script on the same domain and then forwarding it to the target domain, the cross-domain issue can be effectively bypassed.
<?php
// Proxy script to send form values to a different domain
$target_url = 'https://www.example.com/submit-form.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$form_data = http_build_query($_POST);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Handle response from target domain
echo $response;
}
?>
Keywords
Related Questions
- How can PHP learners ensure that they are correctly assigning values from a database query to variables in PHP?
- How can PHP scripts be modified to accurately test and display file paths, file existence, and file readability to diagnose file access issues?
- What are some best practices for implementing a block system in PHP for website organization?