Home » Php » How to submit the form to a different server and from that server return to the same page php

How to submit the form to a different server and from that server return to the same page php

Posted by: admin February 25, 2020 Leave a comment


I’m trying to verify user apiKey. so the I have a application which user installs on their server and they need to add their apiKey to activate it. so I’ve created a form where they can add their apiKey and it’ll submit the key to my server and then my server verify the key. but I want to know how to get from which URL we recieved the request so we can send back results to that url?

<form action="my-server-.com/verifyfile.php" method="post">                                 
    <div class="form-group row">
        <label for="email" class="col-lg-2 col-form-label">Enter API Key</label>
        <div class="col-lg-6">
            <input type="text" placeholder="paste your api key here" name="apiKey" class="form-control" required="">
        <div class="form-group col-lg-2">
            <input type="submit" value="Register" name="verify-api" class="btn btn-outline-success btn-sm">                
How to&Answers:

If i have understood your question,

  1. someone sends an apiKey to your site
  2. you capture the apiKey
  3. you verify the apiKey
  4. after verifying the apiKey you respond to the url that submitted the apiKey

Below php code can sit in your server on a php page to do the task

//If the API Key is sent lets validate it
if (isset($_GET['apiKey'])) {

   $apikey = $_GET['apiKey'];  //The apikey

   if(isset($_SERVER['HTTP_REFERER'])) {   
   $from_url = $_SERVER['HTTP_REFERER']; 
   } else 
   die(); //end 

   //Do your validation for the apiKey
   if ($apikey == 'is_okay') { //do your own validation to test if its okay

    //what you want to append to the url received
    $query_parameter = 'result=yes'; 

   //append your parameter strings to the url you want to respond to
   if(strpos($from_url,'?') !== false) {
    $from_url .=  '&'.$query_parameter;
   } else {
    $from_url .=  '?'.$query_parameter;

   //your site then automatically navigates to the url with an appended query string
   header('Location: '.$from_url);

   } else { 
   die(); //do something here if apiKey not verified 




You can use Curl.

    if (isset($_POST['apiKey'])) {
                $api_key = $_POST['apiKey'];
            } else {
                $api_key = '';

    //You can use Curl...
    $ch = curl_init();

                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
                curl_setopt($ch, CURLOPT_USERAGENT, 
      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_URL,"https://urltoAnotherserver.com"); 
                curl_setopt($ch, CURLOPT_POST, 1); 
                curl_setopt($ch, CURLOPT_POSTFIELDS, $api_key);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

                $json = curl_exec($ch);
                $response = json_decode($json, TRUE);



And when you receive response you can verify the key in php


hi you can make redirect back to url from where they came,but for some interaction you cant without proper code on client/user server side

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.