1

I have the following C++ program for testing purposes:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
int len;
char* lenstr = getenv("CONTENT_LENGTH");

if (lenstr != NULL && (len = atoi(lenstr)) != 0)
{
    char* post_data = new char[len];
    fgets(post_data, len + 1, stdin);
    cout << post_data << endl;
    delete post_data;
}

}

This simply ought to get an environment variable and display it on the screen. The PHP file from which I want to invoke this function is as follows (I call this via AJAX):

<?php
$sent = $_POST['source'];
if ($sent != "")
{
    echo exec("test.exe");
}
else
    echo "Please enter text";
?>

Now, the question is, how do I get $sent to be fed into the C++ program as an argument to be processed in there? I've done some research on the Internet and also here on SO, but so far I keep failing.

AlexM
  • 325
  • 4
  • 11
  • Depends on whats inside your Post variable? Is it simple ansi string without whitspaces, put it directly. If it has whitespaces use quots around. If they are more complecated data use format that you can transport as command line argument, for example base64? – Rene M. Mar 21 '17 at 15:43
  • In the worst case, the Post variable could contain any UTF-8 sign (though very unlikely), so the input is genrally speaking UTF-8 characters with whitespaces between them (i. e. words in different alphabets). – AlexM Mar 21 '17 at 15:56
  • Then I would use base64. This way you are sure that only ASCII chars are transported. – Rene M. Mar 21 '17 at 16:03
  • In this case I first need to acquanit myself with base64 due to no experience with it. – AlexM Mar 21 '17 at 16:08
  • You should know base64 when you are doing web developing – Rene M. Mar 21 '17 at 16:17
  • Probably, but I've only recently begun with it, only a week ago or two and so far, I haven't yet dealed with it. – AlexM Mar 21 '17 at 16:32
  • 1
    https://en.m.wikipedia.org/wiki/Base64 – Rene M. Mar 21 '17 at 17:00

1 Answers1

1

to pass parameter from php when you are using exec function , you may do that like following:

exec("test.exe '" . $_POST['xParameter'] . "'");

then you will be allowed to receive it from your c++ side using the traditional way to receive terminal arguments as follows :

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

Update

this is a working version of how to pass arguments from php using C Syntax not C++

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("hello %s\n", argv[1]);
}

then , in php :

echo shell_exec('./hello Hassan');

// Output: hello Hassan

if you used exec :

exec('./hello Hassan', $out);
print_r($out);
// Output: Array ([0] => hello Hassan)
hassan
  • 7,812
  • 2
  • 25
  • 36
  • Does that mean that essentially I can get rid of my above C++ code if I just change my int main() to int main(int argc, char \*argv[])? – AlexM Mar 21 '17 at 15:58
  • i think so, i'm not familiar with c++, but after trying it with `C` pure code it works like a charm – hassan Mar 21 '17 at 16:03
  • if you want the full `C` code i may update the answer and you can transform it into `C++` – hassan Mar 21 '17 at 16:03
  • however, i'm sure that `C` and `C++` share the same way to pass argument vie terminal which is `int main(int argc, char *argv[])` maybe in `C++` be as follows: `int main(int argc, char** argv)` – hassan Mar 21 '17 at 16:04
  • I would welcome it if you added your version to your answer as I appreciate any help. You know, I myself, too, am not a too experienced programmer. – AlexM Mar 21 '17 at 16:10
  • I've updated my answer using the `C` syntax , for more details about `C++` syntax check out : http://stackoverflow.com/a/31114759/2359679 – hassan Mar 21 '17 at 16:18