Normal textarea CodePress
File: /php.php
Status: Saved on 2012-01-25 19:50:58 using mode FTP_ASCII
<HTML>
<HEAD>
</HEAD>
<?
$host = "31.170.160.86";
$port = 5;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Couldn't Create Socket");
$result = socket_bind($socket, $host, $port) or die("Couldn't bind to socket");
$result = socket_listen($socket, 3) or die("Couldn't listen to connection");
$spawn = socket_accept($socket) or die("Couldn't accept connection");
$output = "Thank God, your first PHP Socket Server";
socket_write($spawn, $output, strlen($output)) or die("Couldn't Write message");
socket_close($spawn);
socket_close($socket);
?>
</body>
</HTML>
#include<iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
if(argv[1]=="DoSomething") //Parameter passed. Process it and return it using std::cout
{
//Do some processing on the arguments passed here...
string ReturnProcessedOutput = "Thank God, I am not an atheist !";
cout << ReturnProcessedOutput; //Return it to PHP
}
else
{
cout << "Some raw data for PHP script..."; //Return to PHP
}
return 0;
}
<?php
$Parameter1 = "DoSomething"; //Our first parameter
$output=`PHPTesting.exe $Parameter1`; //Execute PHPTesting.exe (name of my cpp exe) passing our first parameter.
echo "<strong>$output</strong>"; //Output what you recieved from your cpp program (output variable).
?>
typedef struct _http_header
{
char *header;
char *value;
} HTTP_HEADER;
static int strpos(const char *haystack, const char *needle)
{
const char *ind = strstr(haystack, needle);
return (ind != NULL ? (int)(ind - haystack) : -1);
}
static char *substr(const char *subject, int start, int len)
{
char *substring = NULL;
if (start >= 0 && start < (int)strlen(subject))
{
len = ((start + len) > (int)strlen(subject) ? (int)(strlen(subject) - start) : len);
substring = (char*)calloc(len + 1, sizeof(char));
for(int i = 0; i < len; i++)
substring[i] = subject[start + i];
}
return substring;
}
static char* CreateHttpGetString(char *url, HTTP_HEADER *headers, int nheaders)
{
const char *crlf = "\r\n";
const size_t szlf = 2;
char *request = NULL;
if (strncmp(url, "http://", strlen("http://")) == 0)
url += strlen("http://");
int fslash = strpos(url, "/");
if (fslash == -1)
fslash = (int)(strlen(url));
char *domain = substr(url, 0, fslash);
char *relpath = substr(url, fslash, (int)strlen(url));
if (domain != NULL)
{
if (relpath == NULL)
relpath = "/"; //no relative path, get the base path.
//gets the size of the request before building it.
size_t szRequest = strlen("GET HTTP/1.1") + strlen(relpath) + (3 * szlf) + strlen("Host: ") + strlen(domain);
for(int i = 0; i < nheaders; i++) //loop through the headers to get their lengths (CRLFheader: value)
szRequest += szlf + strlen(headers[i].header) + 2 + strlen(headers[i].value);
request = (char*)calloc(szRequest + 1, sizeof(char)); //allocate some memory for the request string.
sprintf(request, "GET %s HTTP/1.1%sHost: %s", relpath, crlf, domain); //copy over the basics of the request.
for(int i = 0; i < nheaders; i++) //loop through the headers and append each one.
{
strcat(request, crlf);
strcat(request, headers[i].header);
strcat(request, ": ");
strcat(request, headers[i].value);
}
strcat(strcat(request, crlf), crlf);
}
if (strcmp(relpath, "/") != 0) //if there was no original relpath, we set the path to "/" which is constant and cannot be freed.
free(relpath);
free(domain);
return request;
}
HTTP_HEADER headers[5];
headers[0].header = "User-Agent"; headers[0].value = "Mozilla/5.0 Firefox/3.6.12";
headers[1].header = "Accept"; headers[1].value = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
headers[2].header = "Accept-Language"; headers[2].value = "en-us,en;q=0.5";
headers[3].header = "Accept-Encoding"; headers[3].value = "deflate";
headers[4].header = "Accept-Charset"; headers[4].value = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
printf("%s", CreateHttpGetString("http://www.mpgh.net/forum/31-c-c-programming/419473-c-php.html", headers, 5));
GET /forum/31-c-c-programming/419473-c-php.html HTTP/1.1 Host: www.mpgh.net User-Agent: Mozilla/5.0 Firefox/3.6.12 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
void SendGet( char* Host, char* ParamName, char* Param ){
char* Request = new char[ BufferSize ];
// the rfc only requires you have the Host header and (obiously) the request itself I believe.
sprintf( Request, "GET /php.php?%s=%s HTTP/1.1\r\n\HOST: %s\r\n\r\n", ParamName, Param, Host);
//now request holds the GET request with all headers and stuff...
}