【问题标题】:CGI in C: Page RedirectC 中的 CGI:页面重定向
【发布时间】:2013-06-28 18:33:07
【问题描述】:

我有一段在帐户注册页面上执行的 C CGI 代码。我试图找出在 CGI 执行后如何让页面自动重定向到主页。我在网上搜索,但找不到关于 C 的任何明确信息。

【问题讨论】:

    标签: c http cgi http-redirect


    【解决方案1】:

    以下示例代码会将任何请求重定向到www.youtube.com。 响应应该遵循https://www.rfc-editor.org/rfc/rfc3875#section-6.3.2中指定的cgi协议

    #include <stdio.h>
    #include <fcgiapp.h>
    
    int main() {
      FCGX_Stream *in, *out, *err;
      FCGX_ParamArray envp;
      while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
        FCGX_FPrintF(out, "Location: https://www.youtube.com\r\n\r\n");
      }
      return 0;
    }
    

    【讨论】:

      【解决方案2】:

      HTTP 具有用于重定向的 Location 标头。您可以发送以下缓冲区以将客户端重定向到http://en.wikipedia.org/wiki/HTTP_location

      HTTP/1.1 302 
      Location: http://en.wikipedia.org/wiki/HTTP_location
      

      HTTP/1.1 302 
      Location: /local/redirect/
      

      用于本地重定向。遵循标准的 HTTP 客户端会在相应地接收到上述任何标头后,将 GET 请求发送到 http://en.wikipedia.org/wiki/HTTP_location 或您的 /local/redirect/ 页面。

      【讨论】:

      • 对于 NPH 风格的脚本,脚本可以生成自己的状态行。但是,对于纯 CGI 脚本,Location 标头的存在应该会导致服务器生成 302
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多