SFML community forums

Help => General => Topic started by: GroundZero on June 23, 2012, 07:44:55 pm

Title: Check username/password through a PHP file
Post by: GroundZero on June 23, 2012, 07:44:55 pm
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!
Title: Re: Check username/password through a PHP file
Post by: Laurent on June 23, 2012, 07:49:19 pm
What is your problem? Where are you blocked?
Title: Re: Check username/password through a PHP file
Post by: GroundZero on June 23, 2012, 08:02:50 pm
Well, I have no clue how to do it, dont even have a clue in what direction I should check lol.
I asked on Qt forums but no answer, I tried several other C++ forums but no one is helping me out of giving me any points lol.

I would love to get some help from someone i.e. telling me how to do it (what functions I need) or if someone could write me a super simple sample script. Then I can just "tear it apart" and see what each command means and does :)
Title: Re: Check username/password through a PHP file
Post by: Laurent on June 23, 2012, 08:15:07 pm
To send data to a remote PHP script, you must send a sf::Http request, either in POST or GET mode. GET encodes the variables in the URL, while POST hides them in the request body. On the PHP side, the difference is just a matter of reading the $_POST or $_GET global variable.

Then your PHP script can answer anything, for example "ok" or "error".

sf::Http::Request request;
request.setMethod(sf::Http::Request::Post);
request.setURI("/login.php");
request.setBody("username=xxx&password=yyy"); // I let you figure out how to build this string dynamically

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

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

The PHP script:
$username = $_POST['username'];
$password = $_POST['password'];
// sql stuff with $username and $password...
if (ok)
    echo "ok";
else
    echo "error";
It might not work out of the box, I just wrote this quickly to show you the right direction.
Title: Re: Check username/password through a PHP file
Post by: GroundZero on June 23, 2012, 08:27:43 pm
OMG so simple haha... thank you so much, your a life saver ;) <3
Title: Re: Check username/password through a PHP file
Post by: GroundZero on June 24, 2012, 04:22:08 pm
I tried your code, but I am getting strange errors which dont make a clue (to me that is lol)...

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>

int main()
{
    // message
    std::cout << "Hello world!" << std::endl << std::endl;

    // http request to verify username and password
    sf::Http::Request request;
    request.setMethod(sf::Http::Request::Post);
    request.setUri("/login.php");
    request.setBody("username=GroundZero&password=test");

    sf::Http Http("www.site.com");
    sf::Http::Response Responce = Http.sendRequest(request);

    std::string result = Responce.getBody();

    // let the user know if the login was accepted
    std::cout << result << std::endl;

    // end program
    return 0;
}

QT       += core gui

TARGET = project
TEMPLATE = app


SOURCES += main.cpp

HEADERS  +=

LIBS += -L"C:\SFML\lib" -lsfml-window -lsfml-graphics -lsfml-network -lsfml-system

INCLUDEPATH = "C:\SFML\include"

Error messages:
The program has unexpectedly finished.

Compile output:
Quote
WARNING: c:\Users\Angelo\Desktop\Projecten\Validate Username and Password\project\project.pro:17: Unescaped backslashes are deprecated.
WARNING: c:\Users\Angelo\Desktop\Projecten\Validate Username and Password\project\project.pro:17: Unescaped backslashes are deprecated.
WARNING: c:\Users\Angelo\Desktop\Projecten\Validate Username and Password\project\project.pro:17: Unescaped backslashes are deprecated.
C:/QtSDK/mingw/bin/mingw32-make.exe -f Makefile.Debug
mingw32-make.exe[1]: Entering directory `C:/Users/Angelo/Desktop/Projecten/Validate Username and Password/project-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug'
g++ -mthreads -Wl,-subsystem,windows -o debug\project.exe debug/main.o  -L"c:\QtSDK\Desktop\Qt\4.7.4\mingw\lib" -lmingw32 -lqtmaind -LC:\SFML\lib -lsfml-window -lsfml-graphics -lsfml-network -lsfml-system -lQtGuid4 -lQtCored4 -LC:\OpenSSL-Win32_full\lib
mingw32-make.exe[1]: Leaving directory `C:/Users/Angelo/Desktop/Projecten/Validate Username and Password/project-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug'
16:20:35: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited normally.
Title: Re: Check username/password through a PHP file
Post by: Laurent on June 24, 2012, 04:48:44 pm
Quote
The program has unexpectedly finished.
People wrote debuggers for this kind of situations :P

Quote
project.pro:17: Unescaped backslashes are deprecated.
Use escaped backslashes (\\), or simply slashes (/) for your paths.
Title: Re: Check username/password through a PHP file
Post by: GroundZero on June 24, 2012, 07:17:57 pm
Yeah but the debugged tells me nothing 9 out of 10 times, maybe cause lack of knowledge but... (ill edit my post later).

When I start program with the debugger it says:

During startup program exited with code 0xc0000135

So i cannot find any errors from the debugger :)
Title: Re: Check username/password through a PHP file
Post by: eXpl0it3r on June 24, 2012, 09:36:39 pm
Then you haven't understood the sense of the debugger. The errors that get thrown at you are genrated by the compiler or linker but not the debugger. With the debugger you can step into an application and run it instruction by instruction, or can set breakpoints at some specific instructions...
I guess you should learn more about C++ and its tools or use another language for your task. ;-)
Title: Re: Check username/password through a PHP file
Post by: GroundZero on June 24, 2012, 09:58:29 pm
well, I am here for pointers (to learn) so I know what to search / look for and to learn ;)
thanks for your answers people, will look into it and do some research :D

What I do know is this:

The error starts at the following code:

sf::Http::Request request;

If I remove the following part:

Code: [Select]
sf::Http::Request request;
   
    request.setMethod(sf::Http::Request::Post);
    request.setUri("/login.php");
    request.setBody("username=GroundZero&password=test");

    sf::Http Http("www.site.com");
    sf::Http::Response Responce = Http.sendRequest(request);

Everything works fine.

When I use the sample code from 2.0 documentation

Code: [Select]
// Create a new HTTP client
 sf::Http http;

 // We'll work on http://www.sfml-dev.org
 http.setHost("http://www.sfml-dev.org");

 // Prepare a request to get the 'features.php' page
 sf::Http::Request request("features.php");

 // Send the request
 sf::Http::Response response = http.sendRequest(request);

 // Check the status code and display the result
 sf::Http::Response::Status status = response.getStatus();
 if (status == sf::Http::Response::Ok)
 {
     std::cout << response.getBody() << std::endl;
 }
 else
 {
     std::cout << "Error " << status << std::endl;
 }

I get the same error.

Breakpoints not showing the progress or what so ever when using debug mode... all I see is below here:

(http://img708.imageshack.us/img708/7369/70658446.png)

P.S. I know you can do the #if-not-defined cout stuff, but that sucks donkey nuts and wouldnt even work in this case :)