SFML community forums

General => SFML wiki => Topic started by: ActionBoy on February 26, 2011, 05:35:27 pm

Title: Get ip adress from php page.
Post by: ActionBoy on February 26, 2011, 05:35:27 pm
This small code might help someone with a little php/sfml coding. Would it fit in the wiki?

main.cpp
Code: [Select]

#include <SFML/Network.hpp>
#include <iostream>

using namespace std;

sf::IPAddress mMyIP;

string mStringDomain    = "http://www.yourdomain.com";
string mStringURI       = "/ip.php";

int getMyIP()
{
    sf::Http Http;

    Http.SetHost(mStringDomain);

    // Prepare a request to retrieve the index page
    sf::Http::Request Request;
    Request.SetMethod(sf::Http::Request::Get);
    Request.SetURI(mStringURI);

    sf::Http::Response Page = Http.SendRequest(Request);

    if(Page.GetStatus()==200)
    {
        mMyIP = Page.GetBody();
        if(mMyIP.IsValid() == true)
        {
            cout<<"My IP: "<<mMyIP.ToString()<<endl;
            return 1;
        }
        else
        {
            cout<<"The ipadress retrieved was not valid."<<endl;
            return -1;
        }
    }
    else
    {
        cout<<"Could not connect."<<endl;
        return -2;
    }
}

int main()
{
    getMyIP();

    // Wait until the user presses 'enter' key
    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');

    return EXIT_SUCCESS;
}


ip.php
Code: [Select]

<?php 
   
echo $_SERVER['REMOTE_ADDR'&#93;; 
?>

Title: Get ip adress from php page.
Post by: lakunar on February 26, 2011, 05:52:24 pm
There are already functions which get the ip:
Code: [Select]
sf::IpAddress::GetLocalAddress();  // my address on the local network
sf::IpAddress::GetPublicAddress(); // my address on the internet
Title: Get ip adress from php page.
Post by: ActionBoy on February 26, 2011, 05:59:05 pm
Yes, but this is also a simple introduction on how to get a value from your own server.