EvilThinker
EvilThinker

Reputation: 750

How to send text/html data to a browser

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!)

Upvotes: 0

Views: 2988

Answers (2)

Orangepill
Orangepill

Reputation: 24645

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.

Upvotes: 1

mf_
mf_

Reputation: 625

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

Upvotes: 1

Related Questions