Check username/password through a PHP file



  • Dear readers,

    I am building a game for which you need an account. It wouldnt be smart to put the database information (mysql) in the code it self, cause then people can see it and hack my database.

    Instead I want the C++ code to send the filled in username and password to a PHP file i.e. www.mysite.com/login.php

    The PHP will then return true or false (for example). Based on that, the C++ will say "logged in" or "username/password invalid".

    Unfortunately after 2 days of Google I still havent found out how to do this. I hope someone can tell me how to do this.

    Best regards

    p.s. if possible a sample code would be highly appreciated!



  • No one?... strange lol...



  • I highly suggest you look into 2 things

    1 - Use a Soap methode ( First test your webservice with SoapUI, then use QtSoap to send the request )

    2 - Use post methode using QNetworkAccessManager

    Look first on how to create a "Soap webservice in php"
    and test that in an application called "SoapUI"

    after that, go and create a code in Qt that calls this webserivce and send to it the parameters ( In Soap, you can send multiple parameters and return multiple results, it sends and returns xml files )

    Am not sure about the safety of these methods

    This should give you a hint at least



  • Thank you for your method q8phantom, I surely will look into this.

    Just a side-question:

    Do I have to use soap? isnt there something simple like 4-5 lines of code i.e. send a HTTP Post/Get request, cause that is basicly all you need. If we need (in Qt that is) 20 lines of code or a whole different aproach then that is bad (the way I look at it lol).

    For example when using SFML you can do it this way:

    @sf::Http::Request request;
    request.setMethod(sf::Http::Request::Post);
    request.setURI("/login.php");
    request.setBody("username=xxx&password=yyy");

    sf::Http Http("www.mysite.org");
    sf::Http::Response response = Http.sendRequest(request);

    bool ok = (response.getBody() == "ok");@

    That is simple, small and it works flawless and it is 100% safe.

    Anyways, I will look into that Soap thingy ;) thanks again!



  • Even in Soap, it's as simple as this, specially for you to get started, you'll send two parameters with Soap

    Check out this example how to read and send parameters

    @
    void WeatherFetcher::findTemperature(const QString &city)
    {
    QtSoapMessage message;
    message.setMethod("getTemperature", "http://weather.example.com/temperature");
    message.setMethodArgument("city", "", city);

     // transport is a private member of WeatherFetcher, of type QtSoapHttpTransport
     transport.setHost("www.example.com");
     connect(&transport, SIGNAL(responseReady()), SLOT(readResponse()));
    
     transport.submitRequest(message, "/weatherfetcher/fetch.asp");
    

    }@

    This is an example implementation of the readResponse() slot in the WeatherFetcher class:

    @ void WeatherFetcher::readResponse()
    {
    const QtSoapMessage &response = transport.getResponse();
    if (response.isFault()) {
    cout << response.faultString().toString().toLatin1().constData() << endl;
    return;
    }

     const QtSoapType &returnValue = response.returnValue();
     if (returnValue["temperature"].isValid()) {
     cout << "The current temperature is "
          << returnValue["temperature"].toString().toLatin1().constData()
          << " degrees Celcius." << endl;
    

    }@

    Exmmple is taken from here

    http://doc.qt.nokia.com/solutions/4/qtsoap/qtsoaphttptransport.html

    I hope that helps you, test it out
    This is yet simple as I can see
    [quote author="Devbizz" date="1340532147"]Thank you for your method q8phantom, I surely will look into this.

    Just a side-question:

    Do I have to use soap? isnt there something simple like 4-5 lines of code i.e. send a HTTP Post/Get request, cause that is basicly all you need. If we need (in Qt that is) 20 lines of code or a whole different aproach then that is bad (the way I look at it lol).

    For example when using SFML you can do it this way:

    @sf::Http::Request request;
    request.setMethod(sf::Http::Request::Post);
    request.setURI("/login.php");
    request.setBody("username=xxx&password=yyy");

    sf::Http Http("www.mysite.org");
    sf::Http::Response response = Http.sendRequest(request);

    bool ok = (response.getBody() == "ok");@

    That is simple, small and it works flawless and it is 100% safe.

    Anyways, I will look into that Soap thingy ;) thanks again![/quote]



  • Or as you said before you can use the post method to send the username/password ( Don't send it using get, it's wrong )

    But I prefer the QtSoap, they are more modern and more flexible, just find the code in Google, you'll never regret learning Soap

    and in your .pro write this

    @include (/directory/qtsoap.pri)@

    and you'll have access to Qt soap and use the sample code in my above example

    Greetings,
    Ahmed



  • You can do it in Qt quite similar. It's something like

    @
    QNetworkAccessManager manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply
    )),
    this, SLOT(replyFinished(QNetworkReply*)));

    QNetworkRequest req(QUrl("http://www.mysite.org/login.php"));
    QByteArray data = "username=xxx&password=yyy";

    // maybe save the pointer in a class attribute
    QNetworkReply *reply = manager->post(req, data);

    void MyClass::replyFinished(QNetworkReply *reply)
    {
    if (reply->error() == QNetworkReply::NoError) {
    QByteArray answerBytes = reply->readAll();
    QString answer(answerBytes);
    // remove leading/trailing whitespace (line breaks!)
    bool ok = answer.trimmed() == "ok";
    // handle the server's answer here
    } else {
    // handle the HTTP/network error here
    }
    delete reply;
    }
    @

    Code from brain to terminal, it's not tested, but should give you some hints on how to do it.



  • This is the link

    https://github.com/commontk/QtSOAP

    find qtsoap.pri in the src folder



  • awesome, thank you so much guys, will surely test it all out, eventhough I lack the knowledge, but I will try, test and learn this week :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.