2013-05-17 48 views
0

嗨,我在这里问了一个问题:how-can-i-integrate-php-with-my-http-server 我编程的个人网络服务器,我试图使用PHP-CGI与PHP集成,我运行系统命令(“ PHP-CGI.exe path/to/php/script.php getvar = getValue“),然后我将输出发送到浏览器,除了输出在浏览器中显示为纯文本/文本页面之外,其他所有工作都很酷:如何发送文本/ html数据到浏览器

X-Powered-by:PHP/5.4.14 Set-Cookie: PHPSESSID = 9rurvleetdkucms44i4cac9a14;路径= /过期时间:星期四,1981年11月19日 08:52:00 GMT Cache-Control:no-store,no-cache,must-revalidate, post-check = 0,pre-check = 0 Pragma:no-cache内容类型:这里的text/html

ID值= AAAA </H1>

是PHP脚本:

<?php 
session_start(); 
echo "< h1>ID value = < /h1>". $_GET['id']; 
?> 

,我使用的命令:PHP-CGI html_doc /索引。 php id = AAAA

我的问题是:为什么它以简单/文本发送数据而不是text/html我想要?! 为什么它会在浏览器中显示如上所示的标题信息? 谢谢大家! (注意:ⅰ分隔< H1>所以可以出现!)

回答

1

这是一个BASIC html_to_browser用C插座

#include <stdio.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netdb.h> 
#include <string.h> 
#include <strings.h> 


int main(int argc, char *argv[]){ 

    char *reply = 
    "HTTP/1.1 200 OK\n" 
    "Date: Thu, 19 Feb 2009 12:27:04 GMT\n" 
    "Server: Apache/2.2.3\n" 
    "Last-Modified: Wed, 18 Jun 2003 16:05:58 GMT\n" 
    "ETag: \"56d-9989200-1132c580\"\n" 
    "Content-Type: text/html\n" 
    "Content-Length:156\n" 
    "Accept-Ranges: bytes\n" 
    "Connection: close\n" 
    "\n" 
    "o EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE GRANDE ___2 \n \r\n\r\n" 
    "THIS IS A HTTP RESPONSE TO BROWSER \n USING C SOCKETS _ \\_t 5 \n \r\n\r\n" 
    "\r\n\r\n \r\n\r\n \r\n\r\nNOT FINISHED AA _3\r\n\r\n"; 

    int sd = socket(PF_INET, SOCK_STREAM, 0); 

    struct sockaddr_in addr; 
    bzero(&addr, sizeof(addr)); 
    addr.sin_family = AF_INET; 
    addr.sin_port = htons(80); 
    addr.sin_addr.s_addr = INADDR_ANY; 

    if(bind(sd,(struct sockaddr*)&addr,sizeof(addr))!=0){ 
     printf("bind error\n"); 
    } 

    if (listen(sd, 16)!=0){ 
     printf("listen error\n"); 
    } 

    while(1){ 
     socklen_t size = sizeof(addr); 
     int client = accept(sd,(struct sockaddr*)&addr, &size); 

     if (client > 0) 
     { 
      printf("client connected\n"); 
      /*send(client, reply, sizeof(reply), 0);*/ 
     send(client, reply, strlen(reply)+1, 0); 
     } 
    } 
    return 0; 
} 

编译+与须藤控制台上执行(以允许端口80)或改变addr.sin_port到的东西比 '1024' 大

... CTRL + Z完成

+1

我使用的是WINSOCK,但是你写头的方式对我很有用,非常感谢你,这真的很有帮助! – EvilThinker

1

在PHP中w ^应该做header("Content-Type: text/html");将php.INI中的default_mimetype指令设置为默认的MIME类型。

+0

header(“Content-Type:text/html”);不工作,实际上我在浏览器中看到了纯文本/文本的Content-Type:text/html! – EvilThinker

+0

如果你看到标头为body,可能发生的事情是你的http服务器发送头文件和边界,然后传递给php并再次发送头文件。对于PHP文件,我认为你将不得不压制在你的HTTP服务器发送标题,并让PHP-CGI做它 – Orangepill

+0

我确实抑制了从我的服务器发送标题,只允许php-CGI标题,但他们不工作,当我在我的服务器中使用了@mf_的头文件,并抑制了PHP-cgi头文件,它运行的很酷,但当我在脚本中使用session_start()函数时遇到了新的麻烦:-Warning:session_start():无法发送会话cookie - 头文件已在第6行的....... index.php中发送 - 警告:session_start():无法发送会话缓存限制器 - 已在第6行的........ index.php中发送的标头 – EvilThinker

相关问题