From SHTML to PHP - converting the CGI include?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Kreem
Forum Newbie
Posts: 5
Joined: Thu Jan 09, 2003 9:58 am
Contact:

From SHTML to PHP - converting the CGI include?

Post by Kreem »

Hello.

I just converted the main page on my site from SHTML to PHP so that news posts are dynamically added.

My only problem is that the cgi counter that I had (which didn't work in html, hence SHTML) which uses this line:

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

...no longer works. Any help on this would be greatly appreciated. I'd also consider a new counter, if that was an effective solution.

Thanks.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

the include() http://www.php.net/include function should be able to help you out. however i can't remember if it will execute you cgi script. read the documentation to be sure.
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

It would seem to me that the file should be executed, here is what the manual has to say:
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end.
Kreem
Forum Newbie
Posts: 5
Joined: Thu Jan 09, 2003 9:58 am
Contact:

Post by Kreem »

That seems to have worked, in that the script apparently runs, however I now get this large message as opposed to the count:



#!/usr/bin/perl ###################################### # the perl line might need changing # # In order to find out where your # # server has perl, type: # # which perl # # at a telnet session to your server # # # # # ###################################### #------------------------------------------------------------ # # Version 2.0 # # This script is copyright 1999 Brian Taber # # Distribution of this script is unlimited # please visit http://www.tgd-computers.com/bst # to obtain this script. # # Please do not modify this script in any way. # If you have any suggestions or questions # for me on the script, please e-mail btaber@hotmail.com # # # # Remember to upload the script in ASCII format! # The permissions for the script must be set before use!! # Here is the command lines in UNIX # # chmod 755 counter.pl # chmod 666 counter.txt # chmod 666 counterip.txt # # # #------------------------------------------------------------ # Modify the 5 varibles below to suit your needs. # CAUTION! Do not remove the semi-colins ; # Or Quotes '' # 1 - This is the directory where the digits are kept, this must be the full path to the images: $url = 'URL' ; # 2 - This is the extension on the digits: $ext = 'gif' ; # 3 - This is the text file that contains the count: $file = 'counter.txt' ; # 4 - This is the text file that contains the IP addresses of the visitors: $ip = 'counterip.txt' ; # 5 - This tells the counter to use images or text numbers. The deafult is use text. # read the ReadMe text file on how to use each of them. the selections are: # 1 - Use SSI and display text # 2 - Use SSI and display images # 3 - Use regular IMG tag to display image $type = 1 ; # # # DO NOT MODIFY BELOW THIS LINE!!! #------------------------------------------------------------------------------- # $addr = "$ENV{'REMOTE_ADDR'}"; $query = $ENV{'QUERY_STRING'} -1; $t = 0; open(COUNT, $file); $count = ; close(COUNT); @nums = split(//, $count); if ($type eq "1"){ print "Content-type: text/html\n\n"; foreach $num (@nums){$display = "$num"; print $display;} } if ($type eq "2"){ print "Content-type: text/html\n\n"; foreach $num (@nums){$display = ""; print $display;} } if ($type eq "3"){ print "Content-type: image/gif\n\n"; open (IMAGE, "$url/$nums[$query].$ext"); print ; close(IMAGE); } open(INF,$ip); foreach $i (){ chop($i); if ("$i" eq "$addr"){$t = 1}} if ($t eq "0"){ open(NIP, ">>$ip"); print NIP $addr."\n"; close(NIP); ++$count; open(NCOUNT, ">$file"); print NCOUNT $count; } close(INF);




Help. 8O
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

do you have a working example of this? it appears to me that all it is doing is outputing the contents of your perl script and not actually excuting it. since you're converting to php you might lookinto converting the perl script to php. it doesn't look to hard and im sure the people here would be willing to help in the conversion.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the perl script hasn't been executed but included as plain text.
take a look at the exec functions of php.
Since the perl-script is using CGI you have to provide the necessary variables. If you have php running as CGI, too, you should not have to worry about this.
If php is running as mod_php you have to export at least $_SERVER['REMOTE_ADDR'] and $_SERVER['QUERY_STRING']
Note: Introduced in 4.1.0. In earlier versions, use $HTTP_SERVER_VARS.
so that perl can find it as $ENV{'REMOTE_ADDR'} and $ENV{'QUERY_STRING'}.
putenv is one option. Another (maybe) is to invoke the script preceded by the variables (never tried this from a php-script ;) )

Code: Select all

$path2script = '/www/cgi-bin/counter.cgi';
$exec = 'QUERY_STRING="'.$_SERVER&#1111;'QUERY_STRING'].'" REMOTE_ADDR="'.$_SERVER&#1111;'REMOTE_ADDR'].'" '.$path2script;
passthru($exec);
$exec should look like QUERY_STRING="" REMOTE_ADDR="127.0.0.1" /www/cgi-bin/counter.cgi
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

maybe try shell_exec

Code: Select all

$count = shell_exec('path/to/file');
$count = str_replace( "Content-type: text/html", "", $count );
echo $count;
User avatar
DaZZleD
Forum Commoner
Posts: 38
Joined: Tue Jan 07, 2003 5:39 am

Post by DaZZleD »

you cold just make a counter of your own in php with mysql support... just a thought
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

DaZZleD wrote:you cold just make a counter of your own in php with mysql support... just a thought
and if you want you can see what others have already done at places like hotscripts.com:
http://www.hotscripts.com/PHP/Scripts_a ... /Counters/

Mac
Kreem
Forum Newbie
Posts: 5
Joined: Thu Jan 09, 2003 9:58 am
Contact:

Post by Kreem »

First of all, I apologize for flogging what should be a dead horse.

Unfortunately, none of the above code worked, and I've now tried 9 different php based counters, and none of them worked well either (though I can appreciate that it was human error). I would also really like to just keep the current CGI counter I have, as it does everything I need.

Now then, someone suggested above posting the PERL code here.

Apparently, it supports "supports SSI and IMG tag".

Code: Select all

# 1 - This is the directory where the digits are kept, this must be the full path to the images:

	$url  = 'URL'  			;

# 2 - This is the extension on the digits:

	$ext  = 'gif'  				;

# 3 - This is the text file that contains the count:

	$file = 'counter.txt'  		;

# 4 - This is the text file that contains the IP addresses of the visitors:

	$ip   = 'counterip.txt'  		;

# 5 - This tells the counter to use images or text numbers.  The deafult is use text.
#     read the ReadMe text file on how to use each of them.  the selections are:
#     1 - Use SSI and display text
#     2 - Use SSI and display images
#     3 - Use regular IMG tag to display image

	$type = 1  				;

#
#
# DO NOT MODIFY BELOW THIS LINE!!!
#-------------------------------------------------------------------------------
#


$addr = "$ENV&#123;'REMOTE_ADDR'&#125;";
$query = $ENV&#123;'QUERY_STRING'&#125; -1;
$t = 0;

  open(COUNT, $file); $count = <COUNT>; close(COUNT);
  @nums = split(//, $count); 
if ($type eq "1")&#123;
	print "Content-type: text/html\n\n"; 
	foreach $num (@nums)&#123;$display = "$num"; print $display;&#125;
&#125;
if ($type eq "2")&#123;
	print "Content-type: text/html\n\n"; 
	foreach $num (@nums)&#123;$display = "<img align=abscenter src=$url/$num.$ext>"; print $display;&#125;
&#125;
if ($type eq "3")&#123;
	print "Content-type: image/gif\n\n";
	open (IMAGE, "$url/$nums&#1111;$query].$ext");
	print <IMAGE>;
	close(IMAGE);
&#125;

open(INF,$ip);
foreach $i (<INF>)&#123;
chop($i);
if ("$i" eq "$addr")&#123;$t = 1&#125;&#125;

if ($t eq "0")&#123;
open(NIP, ">>$ip");
print NIP $addr."\n";
close(NIP);
++$count;
open(NCOUNT, ">$file"); 
print NCOUNT $count; 
&#125;
close(INF);

Thanks for all of the help so far. :?
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Try the passthru command. It should run your perl script and then show you the out put. I have not used it yet but i will be soon. The programers for the company I work for say that is what to use. We use shtml at the moment and are converting to php and that is what we will have to use for our banner ads.
Kreem
Forum Newbie
Posts: 5
Joined: Thu Jan 09, 2003 9:58 am
Contact:

Post by Kreem »

I'm afraid that, just like many of the other suggestions, the counter simply doesn't appear.

When using a command like exec or passthru, the line doesn't appear in the html code (which I assume is normal), in case that helps.

I never thought I would have this much trouble with a counter...

:x
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe you should alter the script a bit for testing purposes

Code: Select all

$addr = "$ENV&#123;'REMOTE_ADDR'&#125;";
die (print "Content-type: text/plain\n\ninvalid REMOTE_ADDR") if (length($addr) < 1);
$query = $ENV&#123;'QUERY_STRING'&#125;-1;
and try the passthru again.
Kreem
Forum Newbie
Posts: 5
Joined: Thu Jan 09, 2003 9:58 am
Contact:

Post by Kreem »

Nothing happens. I assume therefore that the script is still not being run, but rather 'output' as it was before, but it just doesn't show up on the page.

Look, I feel bad wasting people's time on a stupid counter.

Can anyone recommend a good, simple PHP counter? I've had no luck at all...

Thank you for all of your help.

I'll figure it out someday... :roll:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

as mentioned, http://www.hotscripts.com has some.
But one last try:
If you replace the whole contents of the perl-file by

Code: Select all

#!/usr/bin/perl
print "hello world\n";
and still nothing happens, at least one of the following is probably true
  • the path to the script is invalid. It must be the filesystem path as seen from where the script is running, e.g. if your script is located in /home/userA/www/counting.php passthru('cgi-bin/counter.cgi') will try to execute the file /home/userA/www/cgi-bin/counter.cgi
  • the file has not the appropriate eXecute-flag set. Try perl /absolute/path/to/script instead
  • Your script cannot acces the directory (also permission flags)
  • you cannot access passthru or it is broken. Does passthru('ls'); generate output?
Post Reply