0

I need to access data from a C program, and without refreshing browser have that data displayed.

To start I tried a simple C program displaying text using printf("Hello"); and in the php section of the index.php file tried to run the C program and print its output. However, the real goal would be to maybe have a counter in the C program that counts up to 100, increment by 1 every second and in the browser without refreshing the page the value of that counter would be displayed as it changes.

testcode.c

#include<stdio.h>
int main()
{
    pringf("Display message");
    return 0;
}

index.php


<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body><h1>html</h1>

<?php echo '<p>PHP</p>'; 
   exec("gcc -o /home/birt/c-code.c",$output1,$returnvalue1);
   exec("./c-code",$output2,$returnvalue2);
   echo "<b> output :</b>"+$output2;
  ?>

 </body>
</html>

When I point my browser to the server the HTML and PHP are displayed, however the output from the C program does not

Brum
  • 1
  • 1

1 Answers1

0

Welcome to Stack Overflow!

First, I would strongly recommend not having the PHP compile the code; get the C program working how you want it to, then have the PHP simply call the C program.

Second, give careful consideration to whether or not you really need an external C program -- you can do a lot within PHP, and you avoid several pitfalls that can arise out of calling out to an external program.

Third, your fundamental question is really about dynamic web pages. That's a big topic, and a good answer is out-of-scope for an SO answer. Read up on HTML5 and the client-server communications that are available with it, which allow for the richer "no-refresh" webpage updates you are asking about. Here's a great place to start:

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5

The World Wide Web is full of articles and references for how to use HTML5. Start simple and extend your application as you learn more. Definitely come back here to SO when you run into problems, so that you can ask specific questions about particular problems you are facing. Be sure to include your code, what you've tried, and what is and isn't working; for help asking good questions, see:

https://stackoverflow.com/help/minimal-reproducible-example

HTH, and good luck!

landru27
  • 1,654
  • 12
  • 20