Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 5th, 2003, 10:55 PM
vypergal vypergal is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 vypergal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help with PHP calling C

Hi. I am trying to call a C program using PHP. I have the program running on a Linux Redhat 7.2. I am using PHP4.

My C program prints out a few welcome messages before it checks if the development board that I am using is connected to it. If I run the program using the console, the board is detected but when I tried to run it from my browser, the development board is NOT detected. I am using exec() to run my program. Could someone please advice? I am really new to this.

Also, I need to pass and get some variables to/from the C program from my PHP file. Is it possible?

Any help would be appericiated. Thanks in advanced.

Reply With Quote
  #2  
Old February 8th, 2003, 12:06 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
Quote:
If I run the program using the console, the board is detected but when I tried to run it from my browser, the development board is NOT detected.

Could be one of 1000 possible reasons.
- wrong path
- exec() not allowed in your webserver config
- chroot´d environment
...

Quote:
Also, I need to pass and get some variables to/from the C program from my PHP file. Is it possible?

for small variables, i would recommend the command line. call the C program like this:
exec("mycprogram $name $name2 $name3");
and read the variables via your "**argv" parameter in the C program.
For more complex situations, you need to get into IPC or sockets as both (PHP and C) do support them. the easiest of these mechanisms would imho be piping, i.e. popen() the C program instead of exec(), fwrite() to the pipe and make the C program read the variables from stdin.
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old February 10th, 2003, 10:36 PM
vypergal vypergal is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 vypergal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi. Thanks for the advice. But I still can't solve the problem. I think I may have been a bit brief in my previous posting. This is the situation. The C Program was previously developed by a fellow programmer. I am actually calling his program using PHP. I need to pass some variables to the program to control the C program to allow me to execute some commands.

As I mentioned previously, I managed to get the I have managed to trace the C program to the part where the error message is generated. In one of the functions, the program calls the function open('/path/to/file', O_RDWR). At this part of the program, the open() function should return me a value of 3 but instead when I call it from PHP, it returns a value -1 to me. why??

I have changed the permission of the the executable file "test" to allow users to read, write and execute, groups to read and execute and others to read and execute.

In my Apache httpd.conf file, I have added the following
<Directory "/usr/local/src/Application">
AllowOverride None
Options None
Order allow, deny
Allow from all
</Directory>

In my php file, I call the C program using exec("/usr/local/src/Application/test", $rval. $result)

I am really lost as to what is the problem Could someone please advice???

Reply With Quote
  #4  
Old February 11th, 2003, 12:08 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
Quote:
I have changed the permission of the the executable file "test" to allow users to read, write and execute, groups to read and execute and others to read and execute.


Change the folder´s permissions instead. The folder that your C program tries to write to. For a simple check if this is actually your problem, chmod it to 777 (rwxrwxrwx).

Reply With Quote
  #5  
Old February 13th, 2003, 12:50 AM
vypergal vypergal is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 vypergal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello. Thank you!!! Managed to solve it. Just had to give the other users the write privilege..

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Help with PHP calling C


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway