Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Can i call a handmade perl module within a php file?

by mwah (Hermit)
on May 26, 2008 at 13:13 UTC ( #688520=note: print w/replies, xml ) Need Help??


in reply to Can i call a handmade perl module within a php file?

Simple solution:

1) write a wrapper script (mywrapper.pl):

use MyCounter; # declare the usage of MyCounter my $file = shift; my $host = shift; my $db = <enter dbi opening here>; # where's the DB coming from? ... $counter = counter( $0, $db, $host ); #i call my module print $counter;

2) call your wrapper script in each PHP file via shell_exec(...):

<?php $file = $_SERVER['SCRIPT_FILENAME']; $host = $_SERVER['SERVER_ADDR']; $output = shell_exec("perl /local/www/nik/mywrapper.pl $file $host"); echo "<pre>$output</pre>"; ?>

3) DON'T DO THAT. In PHP, write a module as you did in Perl, Include() it and invoke it the same way like you did in Perl. Use mysql_pconnect(...) for persistent database connections (if necessary).

Regards

mwa

Replies are listed 'Best First'.
Re^2: Can i call a handmade perl module within a php file?
by mwah (Hermit) on May 26, 2008 at 18:35 UTC

    After coming back to this problem an reading tachyon-II's reply, I wondered if this couldn't simply be generalized away for every web file or script (from whatever source), with the goal of inserting a small image with the number of hits for every single page link on a site. The single Perl-Script (eg. in /cgi-bin/) behind this 'image-link' would simply find the link in question (to be counted up) in the $ENV{HTTP_REFERER} variable, handle the database stuff in the background and respond by creating an image (containing a number) on the fly.

    This would imply:

    1) generate an appropriate link scheme, like this:

    [any html or script here] ... <img src="/cgi-bin/counter.pl/image.png" /> </body> </html>

    where the appendix /image.png is a $ENV{PATH_INFO} to help Internet Explorer to "do the right thing" ;-)

    2) we need an appropriate database scheme, for MySQL this would (assumed the database is named 'counting') read:

    ... $dbh->do( q{ CREATE TABLE counters ( pagename VARCHAR(640) NOT NULL , pagecounter INT(11) DEFAULT 0 , PRIMARY KEY (pagename) ) Engine=InnoDB charset=latin1 COMMENT='pagecounter' } ); ...

    ... so I chose the link (pagename) to be the PK of the table. This later enables MySQL to do an INSERT IF NOT EXISTS equivalent thing (its actually: 'INSERT IGNORE INTO counters (pagename) VALUES (?)', which is simply ignored if the page entry (PK!) exists.

    3) The script, which is straightforward. First, the database is updated (my DBI is rusty, please correct my glitches), then the hit-number image is created (via Imager) and sent to the browser. Database access stuff and a font file path for number-output has to be modified. '/cgi-bin/counter.pl':

    I did set this up on my (Linux) Box and it works nice. So I decided to post it, maybe its of use for someone else.

    Regards

    mwa

Re^2: Can i call a handmade perl module within a php file?
by Nik on May 26, 2008 at 21:09 UTC
    I decided to follow step No3 although i dont know any php :)
    The closest solution i came up with is the following, please correct my mistakes
    This is the function that i want to make a php module out of it so every php script can easily invoke the php module: This is just the function i dont know how to make it a php module...
    <?php function counter($pagename,$db,$host) { preg_match("{([^/\\]+?)(?:\.[^/\\.]+)?$}", $pagename, $matches); l +ist($pagename) = $matches; //======= Insert or Update pagecounter appropriately, then display + it ======== @$db->do('INSERT INTO counters (pagename, pagecounter) VALUES (?, +0)', null, $pagename); if ($host != 'webmaster') $db->do('UPDATE counters SET pagecounter + = pagecounter + 1 WHERE pagename = ?', null, $pagename); list($counter) = $db->selectrow_array('SELECT pagecounter FROM cou +nters WHERE pagename = ?', null, $pagename); return $counter; } ?>
    ...and after the creation of the module i will have to invoke it as follows(iam sure i have errors)
    $file = $_SERVER['SCRIPT_FILENAME']; $host = $_SERVER['SERVER_ADDR']; $db = mysql_pconnect("mysql1.000webhost.com", "a3310457_counter", "a33 +10457_akis", "*****"); #i changed host and details here $counter = counter( $file, $db, $host );
    This became php coding now but it started as a perl question and ended like this (sorry for this)
    If someone knows a bit of php then please helpe me correct soem errore i have so i get this thing finally to work.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://688520]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2021-04-11 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?