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

Author Topic: Check username/password through a PHP file  (Read 4170 times)

0 Members and 1 Guest are viewing this topic.

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Check username/password through a PHP file
« 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Check username/password through a PHP file
« Reply #1 on: June 23, 2012, 07:49:19 pm »
What is your problem? Where are you blocked?
Laurent Gomila - SFML developer

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Check username/password through a PHP file
« Reply #2 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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Check username/password through a PHP file
« Reply #3 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.
Laurent Gomila - SFML developer

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Check username/password through a PHP file
« Reply #4 on: June 23, 2012, 08:27:43 pm »
OMG so simple haha... thank you so much, your a life saver ;) <3

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Check username/password through a PHP file
« Reply #5 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Check username/password through a PHP file
« Reply #6 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.
Laurent Gomila - SFML developer

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Check username/password through a PHP file
« Reply #7 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 :)
« Last Edit: June 24, 2012, 07:22:26 pm by GroundZero »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Check username/password through a PHP file
« Reply #8 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. ;-)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Check username/password through a PHP file
« Reply #9 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:



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 :)
« Last Edit: June 25, 2012, 02:07:02 am by GroundZero »

 

anything