Can PHP call CGI?

Miscellaneous Forums/General Discussion/Can PHP call CGI?

Hi, I've got a PHP page and I need to call a CGI script (counter). Now normally my HTML page was saved as SHTML and this line was added:

<!--#exec cgi="../cgi-bin/counter.cgi"-->


however as the page is PHP, not SHTML, it doesn't seem to work as the server doesn't detect and run the cgi script. Help!

Thanks

ummm.... maybe a box frame? something like the size of what you want that loads the shtml in it...?

You could call it with Shell_exec but personally, I'd just write a php counter.

however as the page is PHP, not SHTML, it doesn't seem to work as the server doesn't detect and run the cgi script. Help!


the issue is that the server hands the .php file over to the PHP pre-processor rather than the shtml processing engine, , and they are two entirely different languages.

Mix & match within the same document doesn't work, so you either need to use the shell_exec like Perturbatio suggested, or use a PHP counter instead. I would recommend getting the PHP counter -- they are very easy to write, and if you don't know how there are hundreds of samples available on the internet that will show you exactly what you would need to do... Google is your friend.

or... you can load an shtml in a new frame...

<iframe height = "#" width = "#" src = "url to .shtml" frameborder = "0" scrolling = "no">< /iframe>


Is iframe universally supported now? Back in the day IE could handle it, but nutscape couldn't...

perhaps it isn't.... regardless... you could use other frame types to the same effect... index with a frameset of 2 frames... 1 that is like 50 pixels at the bottom and load the shtml in there and load the php in the upper one... or something else...

hmm... Iframe should be supported... its a good idea...

Is iframe universally supported now?
No. IFrames suck. Write or steal a php counter.

You could also make a second request to your own web server and forward any headers the script will need.

Alternately, you might be able to solve this with an <img src="http://whatever.com/page_with_counter_include.shtml" width=0 height=0>.

Your server is responsible for compiling and running the .cgi, it does not matter if you call the script from a .php or .html file. Now if you were trying to include the file then that would be different but you are not.

Have you tried this:
<img src='../cgi-bin/counter.cgi' width=0 height=0>


hmm OK thanks all. I'll try out some ideas.

arse. Well nothing here seemed to work (haven't tried the frames option yet, not 100% keen on it). I know the counter code works, in fact I put it in it's own shtml page and if I type that url into IE it works. It just doens't work when called from the PHP file or when used as an image source as per octothorpe or Indiepath's methods. Also I tried

"<?php include('counter.shtml'); ?>
and this didn't work either. Is that syntax correct? I guess that this fails as include simply includes that text and the server doesn't see or treat it like an shtml file with an exec cgi call in it.

Help!

including wouldn't work because it doesn't look at what it is including.. it just tacks it on...

hehe... the frames options are looking more and more keen, aren't they.. haha..

Why don't you just make a php counter? they really are easy as pie... and will make for lots less headaches...

.php is your friend.

.php is your friend.
if you can be arsed to learn it! sigh.


<img src='../cgi-bin/counter.cgi' width=0 height=0>



hehe, I guess it only works on MSIE. at least this is what I would call unproperly implemented. How about <img src="shutdown.exe">

Reminds me a lot of some site redirection wars in the previous millenium...

You should try to use the php "system()" command instead:

system("#exec cgi="../cgi-bin/counter.cgi"");


I couldn't test it with a .cgi script execution here, but with a #include file="path" it worked in a php file.
see also:
http://www.linuxquestions.org/questions/showthread.php?postid=50897
http://www.php.net/manual/en/function.system.php

hmm thanks jfk but it didn't work. My counter.cgi updates a counter and outputs HTML to display the current number. When I called <?php system("#exec cgi=\"../cgi-bin/counter.cgi\""); ?> it just did nothing, no errors or anything.

I didn't understand this
I couldn't test it with a .cgi script execution here, but with a #include file="path" it worked in a php file.p
sorry!

This thing is really peeing me off, there must be some obvious way to do it. Looks like I'll have to give in and find a free php counter. Anyone got any good links/code to post?

Yo, found it!

<?php virtual ( "../cgi-bin/counter.cgi" ); ?>

Now all I've got to figure out is what "TextCounter Fatal Error: This Page Not In Valid URI]" means! it's from the counter.cgi I used. sigh.

Jake

wow 18 hours from the start of this post. Should have gone php :D

well I haven't been working on it 18 hours solid :-)

uri is the uniform resource identifier. It looks like it doesn't like the relative path you are using, try an absolute path. Or maybe this means the cgi executor is testing the extension again and denies to execute anything other than the .shtml extension.
BTW:

I didn't understand this
I couldn't test it with a .cgi script execution here, but with a #include file="path" it worked in a php file.



I meant I was trying the server side include (ssi) "#include file" that works the same as #exec of cgi (used as ssi too): both require the .shtml extension.

So I tried:
<?php
system("#include file=\"test.txt\"");
?>

And it worked, on Apache 1.3.24.

BTW2.
wow 18 hours from the start of this post. Should have gone php :D


Actually I think using "system" or "virtual" IS going PHP - directly.

jfk: thanks for your help, I considered the error meant use an absolute path but haven't had time yet. I'll try it later. Thanks for the lowdown on your other points. Although I am experienced developer, my web programming knowledge is pretty novice.