2

How do I send a data contained in a $_POST request to another server? Here's my php file

<?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $contact = $_POST['contact'];
    $address = $_POST['address'];
    $ip = $_SERVER['REMOTE_ADDR'];
    if($name !=''&& $email !=''&& $contact !=''&& $address !='')
    {
        header("Location:http://example.com/loginsuccess/");
    }
    else{
        ?><span><?php echo "You must fill out all the fields";?></span> <?php
    }
}
?>

Like if I wanted $ip to be logged then how do I add that to another server? Like for example if my domain is example.com then I want the $ip log to go on myexample.com. I do not know how to do this and I've spent so long trying and experimenting on how to do this but I can't figure it out, so that's why I'm here on stackoverflow

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Larry Workman
  • 33
  • 1
  • 1
  • 4
  • @FastSnail that confuses me =/ – Larry Workman Sep 13 '16 at 13:39
  • The problem isn't sending the request to another server, the problem is the other server knowing what to do with that request. Are you the one who controls the other server? – apokryfos Sep 13 '16 at 13:40
  • @apokryfos yes, I'm thinking I could add a php file on the other server to handle the request? But what would I put on that php file? – Larry Workman Sep 13 '16 at 13:43
  • You have really phrased the question wrongly. However this question is quite broad. There's a lot of different ways you can handle this logging in the remote server. In fact you don't even need to use POST when sending the data. Most typical thing to do is send a request like `myexample.com/handler.php?ip=$ip` to the remote server and when the server receives such a request, store the contents of `$_GET["ip"]` in a database table or a file. – apokryfos Sep 13 '16 at 13:48
  • @apokryfos Sorry that I phrased is wrong, but like I would send the $_POST to the other server and it stores it?? How would I do that because I already have a redirect in the php file as you can see – Larry Workman Sep 13 '16 at 13:54
  • why don't you send a GET parameter in the header url ? Does it have to be POST ? Maybe like header("Location:http://myexample.com/loginsuccess/?ip=$ip"); – user3526204 Sep 13 '16 at 13:54
  • @LarryWorkman the redirect affects the user, which is not what you need. You need to do something on a server-to-server basis before you send the response over to the user. You would most likely need `cURL` or similar URL opening functions. I think @Prasad's answer should work for you as long as you figure out what to do at `myexample` as well. – apokryfos Sep 13 '16 at 13:58
  • @user3526204 but wouldnt I have to redirect it to that php file for it to get `$ip`? – Larry Workman Sep 13 '16 at 13:58
  • See my edited comment @Larry Workman. Use myexample.com instead of example.com in the redirect. if I have understood your need correclty. – user3526204 Sep 13 '16 at 14:00
  • @apokryfos How would I do that? Oh and sorry I'm fairly new to php – Larry Workman Sep 13 '16 at 14:00
  • @user3526204 so I have that php file on example.com right? Then what do I put on myexample.com? – Larry Workman Sep 13 '16 at 14:02
  • Put another php file there (on myexample.com) and use a GET request and grab the data. And use it as you want. – user3526204 Sep 13 '16 at 14:03
  • @user3526204 Ok, thank you! – Larry Workman Sep 13 '16 at 14:09
  • @Larry Workman If that works, let me know. I will put it as an answer... so you can accept and upvote :) Btw, make the url look like this header("Location:myexample.com/loginsuccess/recievingfile.php?ip=$ip"); – user3526204 Sep 13 '16 at 14:10
  • @user3526204 Ok, give me some time I'll test this out, I'll tell you if this works or not – Larry Workman Sep 13 '16 at 14:19
  • @Larry Workman Hope you know how to capture $ip at recievingfile.php ? if(isset($_GET['ip'])){$getip=$_GET['ip'];} – user3526204 Sep 13 '16 at 14:22
  • @user3526204 I'm betting it's gonna work :) that's because I get all of this now (took me long enough xD) – Larry Workman Sep 13 '16 at 14:49
  • @Larry Workman.. all the Best :) – user3526204 Sep 13 '16 at 14:57
  • @Larry Workman I have tested it. Check here http://www.chalobazaar.in/test.php (please type with.php at the end) It will take you to http://www.landshoppe.com/testip.php and display the result. I guess this what you may want – user3526204 Sep 13 '16 at 16:26
  • @user3526204 sorry for the late reply =/ I took a snooze but I uploaded the file to the second server and it didn't work :/ I got a "HTTP ERROR 500" – Larry Workman Sep 13 '16 at 18:23
  • Could I get a download to those files if you don't mind? @user3526204 – Larry Workman Sep 13 '16 at 18:25
  • I probably got a http error cause I did something wrong =/ – Larry Workman Sep 13 '16 at 18:26
  • @LarryWorkman in the file test.php in example.com $ip = $_SERVER['REMOTE_ADDR']; header("Location:http://landshoppe.com/testip.php?ip=$ip"); And in the file testip.php in myexample.com $ip = $getip=$_GET['ip']; echo "

    IP Details :

    $getip

    ";
    – user3526204 Sep 15 '16 at 09:20
  • Download from here http://www.chalobazaar.in/downloadfile and http://www.landshoppe.com/downloadfile – user3526204 Sep 15 '16 at 09:44

1 Answers1

2
 <?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $contact = $_POST['contact'];
    $address = $_POST['address'];
    $ip = $_SERVER['REMOTE_ADDR'];
    if($name !=''&& $email !=''&& $contact !=''&& $address !='')
    {
        $curl_handle=curl_init();
        curl_setopt($curl_handle,CURLOPT_URL,'http://myexample.com');
        curl_setopt($curl_handle, CURLOPT_POST, 1);
        curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "name=$_POST['name']&email=$_POST['email']&contact=$_POST['contact']&address=$_POST['address']");
        $res = curl_exec($curl_handle);
        curl_close($curl_handle);
        if ($res) {
            echo "success message";
        }
    }
    else{
        ?><span><?php echo "You must fill out all the fields";?></span> <?php
    }
}`enter code here`
?>
Mahadeva Prasad
  • 709
  • 8
  • 19