【问题标题】:C# PHP-CGI with cookie support支持 cookie 的 C# PHP-CGI
【发布时间】:2016-12-17 01:25:54
【问题描述】:

我正在尝试创建一个 WebServer,它使用 HTML、图像... 但是我想实现 PHP,并且我有一个 PHP-CGI 的工作实现,但它不适用于像“file_get_contents”和 cookie 这样的东西。 我想知道是否有办法解决这个问题?

private string ProcessPHP(string phpPath, CookieCollection cookies)
{
    Process CGI = new Process();
    CGI.StartInfo.FileName = "php\\php.exe";
    CGI.StartInfo.CreateNoWindow = true;
    CGI.StartInfo.Arguments = "-q \"" + phpPath + "\"";
    CGI.StartInfo.RedirectStandardOutput = true;
    CGI.StartInfo.UseShellExecute = false;
    CGI.Start();

    string OutputText = CGI.StandardOutput.ReadToEnd();

    CGI.WaitForExit();
    CGI.Close();

    return OutputText;
}

我知道我没有对 CookieCollection 做任何事情,但我想知道如何使用它。

谢谢!

【问题讨论】:

    标签: c# php cookies server cgi


    【解决方案1】:

    这是一个老问题,但目前 Windows 版本的 php 附带一个单独的 php-cgi.exe。这是您必须运行的可执行文件。

    使用 CGI,脚本信息和请求标头通过环境变量发送,即:

    CGI.StartInfo.info.EnvironmentVariables["SCRIPT_FILENAME"] = @"c:/wwwroot/index.php";
    

    有关更详细的示例,请参阅https://en.wikipedia.org/wiki/Common_Gateway_Interface

    HTTP 标头(例如 cookie)也通过环境变量发送。它们以'HTTP_' 为前缀,破折号被转换为下划线:

    CGI.StartInfo.info.EnvironmentVariables["HTTP_COOKIE"] = ...
    

    如果您的请求正文包含数据,则必须设置CONTENT_LENGTH环境变量,并在启动过程后将数据写入CGI.StandardInput

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 2010-11-06
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多