0

Hi i asked a question before here : how-can-i-integrate-php-with-my-http-server i programmed a personal web server and i tried to integrate it with PHP using the PHP-CGI , i run a system command ("PHP-CGI.exe path/to/php/script.php getvar=getValue") and i send the outputs to the browser , everything works cool except that the output shows in the browser as plain/text page like :

X-Powered-By: PHP/5.4.14 Set-Cookie: PHPSESSID=9rurvleetdkucms44i4cac9a14; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-type: text/html

ID value = AAAA< /h1>

here's the PHP script :

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

and the command i used : php-cgi html_doc/index.php id=AAAA

my question is : why does it send data in plain/text instead of text/html i want?! and why does it show the header information in the browser like above ?! Thank you all in advance! (Notice : i seperated < h1>so it can be appeared!)

Community
  • 1
  • 1
EvilThinker
  • 740
  • 1
  • 9
  • 13

2 Answers2

1

this is a BASIC html_to_browser using C sockets

#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;
}

compile + execute on a console with sudo(to allow port 80) OR change the addr.sin_port to something bigger than '1024'

...ctrl+z to finish

mf_
  • 605
  • 8
  • 17
  • 1
    i'm using WINSOCK but the way you wrote the headers is useful for me, thank you very very much, it's really helpful! – EvilThinker May 17 '13 at 12:19
1

In php you would do header("Content-Type: text/html"); to set a default change the default_mimetype directive in php.INI to the correct mime type.

Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • header("Content-Type: text/html"); is not working , in fact i see Content-Type: text/html in plain/text in the browser! – EvilThinker May 17 '13 at 12:41
  • If you are seeing headers as body what is probably happening is that your http server is sending headers and the boundry then its passing to php and sending headers again. For php files I think you're going to have to suppress sending headers in you http server and let php-cgi do it – Orangepill May 17 '13 at 12:52
  • i did suppressed sending headers from my server and allowed only php-cgi headers but they're not working and when i used @mf_ 's headers in my server and supressed PHP-cgi headers , it worked cool but i got a new trouble when i used session_start() function in the script : -Warning: session_start(): Cannot send session cookie - headers already sent in .......index.php on line 6 -Warning: session_start(): Cannot send session cache limiter - headers already sent in ........index.php on line 6 – EvilThinker May 17 '13 at 13:40