开发者

best practice for c# calling php which then queries the database

For some reason I have to have a windows client application (written in C#) which communicates with the PHP files that are on my server. Windows application can't be allowed to have SQL queries in the code because of the possible disassembling of the exe file. This is the main reason why this approach is used.

Basically it looks like this: from windows client i call getResult.php which then opens the connection to the database, queries the database, returns the result to the client and closes the database connection. Therefore windows client doesn't have any code for querying the database, it just has calls to the PHP file.

My several questions follow:

1. What is the best way to send request from c# code to the PHP file? (Cause I need to send this php file some parameters like ID, etc... -> I know I can do it with GET like this getResult.php?id=123456, but is this same possible with POST? And also, one question: how to do this in code? http requests or?)

2.Since every time I call the PHP file (there will be more files which I will call, like getResult.php, getStatus.php, etc...) I will somehow need to send login information to that PHP file with which that PHP will query the database. My question here is how to do this securely, and plus: is it maybe somehow possible to call something like doLogin.php and send the login username and password one time, and after that call this (and all other) php files without the need to send the login information as a parameter to the function. I know I can use PHP sessions when the whole application is on the server, but the main difference here is that I am only calling some files, executing them and closing the connection.

My main question is: is this ok from conceptual point of view or are there any co开发者_开发问答mmonly known concepts for this, for which I don't know about - please advise I'm willing to learn. I did some research and do believe this might have to be done with web services approach, but please do reply your thoughts on this.

Thank you for your help!


Your PHP code is effectively serving as a RESTful data-access API. Run your PHP on a webserver over SSL (HTTPS) so that all your comms are encrypted.

You could either use trusted certificates to authenticate the client, or if you require different access levels, submitting a username/password to get an authorisation token for the data-access requests is not a bad idea.


For a simple GET you can do:

var webClient = new WebClient();
webClient.DownloadString("http://someurl.com/somescript.php");

You could then return perhaps an XML or JSON formatted response from the PHP script? You can use WebClient for POST too.

As for the login, you can do that too. I do a similar thing in one of my applications. We send the login details to the script (ASP.NET not PHP) and the ASP page returns an XML response telling the C# app whether or not it was successful - the application can then decide whether it is allowed to continue or not.


What you're looking for is REST. Your PHP files are acting as a web service in this circumstance, and you can use RESTful guidelines to determine the best practices for your scenario.


You have to encrypt data between C# app and PHP. Why? For security. You can easly store encrypted data in MySQL.

Insert into table (myname, mysurename) values (AES_ENCRYPT('Voon',pass),AES_ENCRYPT('Voon',pass))

C# work.

HttpWebRequest myRequest =
 (HttpWebRequest)WebRequest.Create(URL);
             myRequest.Method = "GET";
             WebResponse myResponse = myRequest.GetResponse();
             StreamReader sr = new StreamReader(myResponse.GetResponseStream(),
 System.Text.Encoding.UTF8);
             string result = sr.ReadToEnd();
             //Console.WriteLine(result);
             result = result.Replace('\n', ' ');
             sr.Close();
             myResponse.Close();

Php code:

 <?php
    function connection() { 

        $mysql_server = ""; 

        $mysql_admin = ""; 

        $mysql_pass = "t"; 

        $mysql_db = ""; 

        @mysql_connect($mysql_server, $mysql_admin, $mysql_pass) 
        or die('Brak połączenia z serwerem MySQL.'); 
        // łączymy się z bazą danych 
        @mysql_select_db($mysql_db) 
        or die('Błąd wyboru bazy danych.'); 

    } 

     connection();



     $data = mysql_query("SELECT QUERY") 

     or die(mysql_error()); 
        mysql_query("TRUNCATE TABLE `table`")  or die(mysql_error()); 
     while($info = mysql_fetch_array( $data )) 
     { 

     $stringData = $info['columnname'] . ",";
    $temp = $stringData;

     $stringData =$info['columnname'];
    $temp = "$temp" . "$stringData";

    echo "$temp" . ".";

     } 
     }


     ?> 

This code. Calls php and getresult (column.column, next column.column) in C#. After sending data it recreate table.

Hope it works for you.

EDIT! For calling link with parameters use this:

in PHP

$myname = $_REQUEST['myname'];

eg. http://mylink/setname.php?myname=VoonArt PHP stored in variable VoonArt

2) Use https to send important data. Also encrypt it. Use same encryption in C# and PHP (triple-des)

C#-->Encode Pass-->Hey PHP can you get me some data my password is &283&(^@(08218--> Okay, C# I'll decode your password and send you result!-->PHP decode password --> PHP getdata -->php encode data with (eg. triple-des) --> Hey C# catch, you own me a beer huh?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜