Click here to Skip to main content
15,031,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everybody,

I have a cpp code reading some information from a device via bluetooth and writes it to a txt file. it works properly.

Also I have a php web page (in localhost) and I want to run this cpp executable from my php page.

I used exec function but when i click the relevant button it does not work. may be it is a security issue.

My php code:
	$cmd = "C:\Users\wecare\Desktop\help\comport\Debug\comport.exe";
<button type="button"  onclick= "<?php exec ($cmd);  ?>" 
<p align="center" class="style12">OK</p>

Any help?
Updated 22-Dec-11 1:16am

This is not allowed as it breaches the security rules for web browsers.
mstftrn 22-Dec-11 7:17am
then, is there any other solution for this?
Richard MacCutchan 22-Dec-11 7:40am
I'm not sure, but checking the PHP documentation may help.
You're mixing up server-side and client-side code. The exec inside the onclick will run when the web page is built on the server, leaving the web page with an empty onclick="".

PHP only runs when the web page is put together by the server, so you will either have to run the command when the page is opened and then display the result as part of the page, or use AJAX to call back to the server to run the command from the page and get the results back if you must use a button.

According to the manual page[^], the exec function does not output anything so you would need to echo the result of the call or assign it to a variable to see what you get back.
mstftrn 22-Dec-11 10:45am
thank you...
will javascript work?
Graham Breach 22-Dec-11 15:48pm
Well... you can't run your EXE directly using Javascript, but that's where AJAX comes in. If you're not familiar with AJAX, I would skip it and just make your PHP page call the EXE and output the result.

Remove your button and replace it with <?php system($cmd); ?>
mstftrn 6-Jan-12 7:33am
Hi, after a while i have a question again. works and prints the output, but thn i cannot use that output. For example i cannot send it to mysql database. even cannot "echo" the outbut again!
Any idea?
Graham Breach 6-Jan-12 8:22am
Either change from using "system" to "exec" and store the result in a variable, or use output buffering (ob_start, ob_get_flush, etc.)
mstftrn 27-Dec-11 4:37am
Hi, i did what you said and it worked but the program, now, does not write into the txt file but just show the output of the printf function. Is it again a security issue? any way to solve it?
Graham Breach 27-Dec-11 4:52am
I expect it's a permissions problem, but I've never run PHP on Windows so I don't know what you would have to change to make it work.

I would try adding some logging into the EXE to see if it is failing to write the file or failing to read from the bluetooth device. There may already be some useful messages in the web server logs to help you find the problem.
mstftrn 27-Dec-11 4:57am
it can read from the bluetooth device because it prints right output when using "system($cmd)". But cannot write to file. As you said it may be about permissions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900