Forum Moderators: coopster & phranque

Message Too Old, No Replies

Pulling a CGI script into an HTML page?

         

victoryrun

4:29 pm on Feb 9, 2003 (gmt 0)

10+ Year Member



I have a webpage that is made up of tables. I have a CGI program (it's a picture posting program) which calls all files from a directory on my server and shows them in a page. Problem is, you have to click a link called www.mydomain.com/cgi-bin/cgiprogram.pl to view these results (the photos). I want visitors to my website to be able to click the "photoalbum.html" link I have in my regular HTML and have the current results of that "cgiprogram.pl" to append into this "photoalbum.html". I haven't the FOGGIEST on script writing, about all I can do is modify ones I find for free, and I get lucky to do most of those. So I thought maybe an ILAYER on my HTML page would allow a piece of code to draw that cgi file. Is this accomplishable? How do I go about doing it?

Thanks tons,
Ramsey

Birdman

4:41 pm on Feb 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have php available, you can use the include function [php.net] but you will have to name the page photoalbum.php.

Wherever you want the script to be inserted do this:

<?php
include("cgifile.pl")
?>

Welcome to WebmasterWorld!

andreasfriedrich

5:21 pm on Feb 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are running Apache [httpd.apache.org] you could use mod_include [httpd.apache.org] to include the output of your script into the page.

If you are running PHP [php.net] on Apache [httpd.apache.org] you could use virtual() [php.net] to include the output with a subrequest.

include() [php.net] will not work when you include a local script using its local filename since PHP [php.net] switches back to HTML mode and just loads the file spits out the parts that are HTML and executes the PHP [php.net] parts. When you use include() [php.net] with a URI then PHP [php.net] will request that URI and include the output of that request. So you would need to use include("http://your.server.tld/path/to/cgi.script");

Andreas

victoryrun

2:59 am on Feb 10, 2003 (gmt 0)

10+ Year Member



Hi everybody so far,

Thanks for the helpful tips. I don't know much about my server (I *do* believe it's an Apache, but I may be wrong and I don't know any versions or anything.) The hosting service provider is Netfirms, if anybody has had any dealings with them and might be able to help.

I thought I had PHP access, but I know don't have any database backups (like MySQL or others) and I have never used any PHP programing. But when I tried to do the PHP "Include" I got the following error:

---
Fatal error: Call to undefined function: phpinclude() in photocontest_test.php on line 221
---
So I don't know if that means that I don't have PHP or if the code I used was wrong or what. I used the full [myserver.com...] inside the include code. Should I have used the path instead? Is there something else I should be (or shouldn't, for that matter) be doing?

Thanks for all your help so far!

Ramsey

jatar_k

3:34 am on Feb 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this

phpinclude()

should be

include("http://www.myserver.com/cgi-bin/filename.pl");

victoryrun

2:04 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



Okay, changed & uploaded, new problem. Received following messages:

---

Warning: php_hostconnect: connect failed in photocontest_test.php on line 237

Warning: Failed opening 'http:*//www.mysite.net/cgi-bin/photocontest/conformation.pl' for inclusion (include_path='.:/usr/local/nf/lib/php') in photocontest_test.php on line 237

---

Any idea what they mean? (And you can also go to <snip> to see the file in question. I'm trying to have the contents of the cgi file displayed in the right hand column of the table (the white background area, where the error msg is displayed).

Thanks for the help so much so far. I think I'm on the right track, and you have all been so helpful! Thanks again.

Ramsey

[edited by: jatar_k at 2:27 pm (utc) on Feb. 10, 2003]
[edit reason] no urls please [/edit]

andreasfriedrich

2:39 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



allow-url-open [php.net] needs to be set to true.

Check that the URI you try to open returns something valid by requesting it using your browser.

Andreas

victoryrun

3:10 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



Andreas,

I'm assuming this is something that I would need to contact and verify (or change) through my server admin?

Ramsey

jatar_k

3:13 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yes,

you can see the php config by using

phpinfo() [php.net]

victoryrun

3:27 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



Okay,

The values are set to 1.

<snip>

I'm so confused! :-O

[edited by: jatar_k at 3:00 am (utc) on Feb. 11, 2003]
[edit reason] no urls please [/edit]

victoryrun

3:37 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



Also, thought I'd add that the file I'm trying to pull into my page is at <snip> and it is currently working. (I have some redundant design/layout issues, mainly because I didn't know I could pull the cgi contents into part of my webpage, and if I get it working, I'll fix all that, but let's just work on getting it working.) :-)

Ramsey

[edited by: jatar_k at 3:01 am (utc) on Feb. 11, 2003]
[edit reason] no urls please [/edit]

andreasfriedrich

3:51 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There seems to be an issue with PHP [php.net] 4.0.6 and Apache [httpd.apache.org] 1.3 which you are using as well. Your problem could be related to that.

[bugs.php.net...]

Andreas

victoryrun

4:26 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



So, short of uploading a newer version of either/or (which is beyond my control at that point), is there anything code-wise that I can do or change to get around this problem?

andreasfriedrich

4:38 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure.


<?php
$fp = fsockopen ("127.0.0.1", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs ($fp, "GET /horses-cgi-script HTTP/1.0\r\n".
"Host: www.somehorses.com\r\n\r\n");
while (!feof($fp)) {
echo fgets ($fp,128);
}
fclose ($fp);
}
?>

example taken from the PHP [php.net] manual - fsockopen() [php.net]

This will open a connection to your local server using fsockopen() [php.net]. If the connection is established it will send a request for the horses-cgi-script using fputs() [php.net]. If you are using name based virtual hosting then you need to set the Host: field to contain your domain name. It will then loop and read chunks of data using fgets() [php.net] and echo() [php.net] them. If there is nothing left to read feof() [php.net] will be true and it will close the connection using fclose() [php.net].

Andreas

victoryrun

5:29 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



Okay I've been racking my brain for the past hour or so.

I thought I'd try some simple things to see if I could get them to work before I work on my cgi-file. <snip> is a very simple page where I'm trying to include an HTML page found at <snip> and I've been UNsuccessful w/ the following code:

<?php
$fp = fsockopen ("udp://00.00.00.00", 21, $errno, $errstr);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs ($fp, "GET / HTTP/1.0\r\nHost: /simplephptest2.html\r\n\r\n");
while (!feof($fp)) {
echo fgets ($fp,128);
}
fclose ($fp);
}
?>

209.171.43.28 is my IP, and 21 is my initial port #. I keep getting the following error: Invalid argument (22) ... and it's DRIVING ME NUTS! I've even deleted all code through line 22 and still see this error. Check the above pages to see for yourself. I'm clueless! (And so far apparently helpless too!)

[edited by: jatar_k at 3:03 am (utc) on Feb. 11, 2003]
[edit reason] Read TOS [webmasterworld.com] [/edit]

victoryrun

5:35 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



also changed the code to

<?php
$fp = fsockopen ("209.171.43.28", 21, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs ($fp, "GET /simplephptest2.html HTTP/1.0\r\n".
"Host: www.domain.net\r\n\r\n");
while (!feof($fp)) {
echo fgets ($fp,128);
}
fclose ($fp);
}
?>

and get the same 22 error. Then, changed code to :

<?php
$fp = fsockopen ("udp://209.171.43.28", 21, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs ($fp, "GET /simplephptest2.html HTTP/1.0\r\n".
"Host: www.domain.net\r\n\r\n");
while (!feof($fp)) {
echo fgets ($fp,128);
}
fclose ($fp);
}
?>

(simply adding the udp://) and get a 500 Internal Server Error... I'm completely socked!

[edited by: jatar_k at 3:03 am (utc) on Feb. 11, 2003]

andreasfriedrich

5:38 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HTTP uses TCP/IP not UDP. Are you sure that your webserver is listening on port 21? Thatīs usually an FTP port. Unless you are doing some funny port forwarding your webserver is on port 80 (checked using [site.com:80...] and not 21.

Andreas

[edited by: jatar_k at 3:04 am (utc) on Feb. 11, 2003]

victoryrun

5:56 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



You're right: I took that port from my FTP client. (like I said, I'm helpless!). But I switched the port from 21 to 80 in the code, saved, uploaded and still have that same Invalid Argument (22) error. Any insight?

amznVibe

5:56 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Am I totally missing something here? Why is this so complicated?
Many servers have SSI so why not just put this into the first simpletest1.html

<!--#include file="simpletest2.html" -->

If it doesn't work at first, just add these lines to your .HTACCESS file to process all .htm & .html as shtml

AddType text/html .shtml .htm .html
AddHandler server-parsed .shtml .htm .html

I mean unless you just insist on using PHP to solve this?

you can also trigger cgi's the same way with SSI

<!--#exec cgi="/cgi-bin/cginame.cgi" -->
or
<!--#include virtual="/cgi-bin/cginame.cgi" -->

[edited by: amznVibe at 6:01 pm (utc) on Feb. 10, 2003]

andreasfriedrich

6:00 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



amznVibe, I suggested mod_include [httpd.apache.org] in msg #3 [webmasterworld.com] but it seems Ramsey liked the PHP [php.net] approach better. Perhaps heīs regretting that by now ;)

Birdman

6:11 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>I mean unless you just insist on using PHP to solve this?

I suppose that was my fault for bringing it up in msg #2 [webmasterworld.com]

[edited by: Birdman at 6:14 pm (utc) on Feb. 10, 2003]

andreasfriedrich

6:14 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strange! This works.

<?php
$fp = fsockopen ("www.aaroncarter.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs ($fp, "GET /index.php\r\n HTTP/1.0\r\n\r\n".
"Host: www.aaroncarter.com\r\n\r\n");
while (!feof($fp)) {
echo fgets ($fp,128);
}
fclose ($fp);
}
?>

This doesnīt.

<?php
$fp = fsockopen ("www.yoursite.net", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs ($fp, "GET /\r\n HTTP/1.0\r\n\r\n".
"Host: www.yoursite.net\r\n\r\n");
while (!feof($fp)) {
echo fgets ($fp,128);
}
fclose ($fp);
}
?>

And this does work again:

telnet www.yoursite.net 80
Trying 209.171.43.28...
Connected to ip28-43-171-209.toro1.na.psigh.com.
Escape character is '^]'.
GET / HTTP/1.0
Host: www.yoursite.net

HTTP/1.1 200 OK
Date: Mon, 10 Feb 2003 18:11:04 GMT
Server: Apache/1.3.26 (Unix) mod_perl/1.26 PHP/4.0.4pl1 mod_ssl/2.8.10 OpenSSL/0.9.6a
Last-Modified: Mon, 13 Jan 2003 18:31:13 GMT
ETag: "1729902-658-3e2305f1"
Accept-Ranges: bytes
Connection: close
Content-Type: text/html

<HTML>

Andreas

[edited by: jatar_k at 7:28 pm (utc) on April 16, 2003]

andreasfriedrich

6:15 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, itīs all your fault! Blame it on the Birdman ;)

victoryrun

6:16 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



Boy, I sure feel like an idiot! :-)

<snip>

This looks like the way to go. I'm going to work on that cgi include now. I looked on my server, don't have an .htacces file, so should I create one and if so, how? Is there a way to make all .HTML files read as .SHTML files? Thanks for putting up with my stupidity!

[edited by: jatar_k at 3:06 am (utc) on Feb. 11, 2003]

victoryrun

6:27 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



<snip>

Does this mean my permissions are set incorrectly? *sooo confused - went to school for graphic design, not computer programing*

I just like the PHP stuff better because it kind of seemed neater at the time.

Andreas - I tried the two different php code snippets you just listed and BOTH of them gave me the "22" error. I also really haven't the foggiest of ideas what info you got from the telnet bit.

*sigh* maybe I'll just never get this....

[edited by: jatar_k at 3:06 am (utc) on Feb. 11, 2003]

amznVibe

7:22 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



here's a simple .htaccess file, copy the following into a text editor,
save as .htaccess and put it into the folder that has the highest/first index.html

# make sure certain files are never shown in an open directory
IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

# turn on important server options
Options FollowSymLinks Includes ExecCGI

# support various CGI extensions:
AddType application/x-httpd-cgi .cgi .pl

# support SSI parsing on most HTML types:
AddType text/html .shtml .htm .html
AddHandler server-parsed .shtml .htm .html

# encourage browsers/proxies to cache image files for a month, to keep bandwidth down
ExpiresActive on
ExpiresByType image/gif "access plus 30 days"
ExpiresByType image/jpeg "access plus 30 days"

# return correct content-type for certain images:
AddType image/x-xbitmap .xbm .XBM
AddType image/bmp .bmp .BMP
AddType image/x-icon .ico

# always a good idea to have a custom 404 error message
ErrorDocument 404 /missing.htm

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>

amznVibe

7:32 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



an error occurred while processing this directive

if you are trying to do a cgi include, you'll have to try the other technique since you are getting the above message...

if you are using <!--#exec cgi="/cgi-bin/cginame.cgi" -->

then switch it to <!--#include virtual="/cgi-bin/cginame.cgi" -->

and see if that clears up the problem

also, when you install the above .htaccess file, you will be able to use the extension of .htm or .html instead of .shtml

victoryrun

8:42 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



if you are using <!--#exec cgi="/cgi-bin/cginame.cgi" -->

then switch it to <!--#include virtual="/cgi-bin/cginame.cgi" -->

and see if that clears up the problem

also, when you install the above .htaccess file, you will be able to use the extension of .htm or .html instead of .shtml

------- Total Astonishment -------- :-)
Using the include virtual worked like a charm. I copied the above text, named it .htaccess and uploaded it to my www/ dir on my server, but I still need to type filename.shtml to get the SSI's to work properly. Did I miss something?

Thanks so much for everyone putting up with me. I've tried other forums for help and never found a community or group of people who are so helpful and know their stuff so well. Thanks again!

andreasfriedrich

11:02 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it sorted. Thanks for continueing this amznVibe ;)

amznVibe

11:29 pm on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



happy to help, everyone starts confused and I know real code snippets help alot...

Andreas we still didn't help with the problem of .htm and .html not being treated as .shtml

Can you spot any problems with my sample htaccess file above?
These lines should have fixed that issue, no?

AddType text/html .shtml .htm .html
AddHandler server-parsed .shtml .htm .html

ah wait, I think I spotted something, try adding this line to .htaccess, or replace the above AddType with it

AddType text/x-server-parsed-html .html .htm
This 35 message thread spans 2 pages: 35