Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Get ip adress from php page.  (Read 5795 times)

0 Members and 1 Guest are viewing this topic.

ActionBoy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Get ip adress from php page.
« 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;; 
?>


lakunar

  • Newbie
  • *
  • Posts: 20
    • View Profile
Get ip adress from php page.
« Reply #1 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

ActionBoy

  • Newbie
  • *
  • Posts: 36
    • View Profile
Get ip adress from php page.
« Reply #2 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.