0

I have PHP website which stores values in session.

Can I get these values from an independent C# program (on the same machine and time)?

Other option (if the above is not possible) is to get the current textbox value into C# variable (again, if possible).

rtruszk
  • 3,902
  • 13
  • 36
  • 53
ofir parnass
  • 3
  • 1
  • 3

2 Answers2

0

You can build an API.

session_start();
if(isset($_GET['action']) && $_GET['action'] == "getSession") {
   echo json_encode($_SESSION);
}

Then you can call the api via your C# program.

// Returns JSON string
string GET(string url) 
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    try {
        WebResponse response = request.GetResponse();
        using (Stream responseStream = response.GetResponseStream()) {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            return reader.ReadToEnd();
        }
    }
    catch (WebException ex) {
        WebResponse errorResponse = ex.Response;
        using (Stream responseStream = errorResponse.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            String errorText = reader.ReadToEnd();
            // log errorText
        }
        throw;
    }
}

The call with the url is like: GET("http://localhost/api.php?action=getSession")

For security reason i would prefer to add an security code to the api.

Tobias
  • 653
  • 4
  • 12
  • Yes, sure i can. http://stackoverflow.com/questions/8270464/best-way-to-call-a-json-webservice-from-a-net-console the first answer and the first code block. I will add this to my answer. – Tobias Jul 21 '15 at 12:31
-1

Yes,Possible. When you run your web application, you must save session in database. Session have many state and mode. Store session in database is one of the mode.Then you can access data using simple c# program.

Please visit this link.

https://msdn.microsoft.com/en-us/library/ms178581(v=vs.140).aspx http://www.codeproject.com/Articles/150689/Details-explanation-on-Compression-Enabled-Session

Suman Banerjee
  • 1,923
  • 4
  • 24
  • 40