Page 1 of 2
Need help connecting this php script to perl
Posted: Mon Apr 21, 2003 7:53 am
by Nik
Do u have any idea how can i do this? can it be done automtically somehow?
[admin edit: script removed because it is repeated a couple of posts below with syntax highlighting]
thanks!
Posted: Mon Apr 21, 2003 9:42 am
by twigletmac
Please put your PHP code into between [syntax=php]and[/syntax] tags as it makes it a lot easier to read.
Editting your post and adding them in will make the code a lot more pleasant to look at.
Mac
Posted: Mon Apr 21, 2003 10:24 am
by Nik
Iam really sorry, i didnt know this trick... ok let me rephrase:
what about other type of coding like perl? what tags must i use for that?
Here is the code rephrased!
Code: Select all
<?php
ob_start(); error_reporting(E_ALL); $xronos=date("l dS of F h:i:s A"); $host=gethostbyaddr($_SERVER['REMOTE_ADDR']); $i=0;
echo "<body background=images/anemos.jpg>";
echo "<font size=5 color=Yellow>";
if (@mysql_connect('localhost', 'nik', 'macgyver'));
else @mysql_connect('localhost', 'root');
mysql_select_db('db_nik');
$data = file("data.txt");
$diary = file("diary.txt");
?>
<script type="text/javascript">
var textToShow = "<? echo join('"+String.fromCharCode(13)+"', array_map('addslashes', (array_map('trim', $data)))); ?>";
var pos = 0;
var tm;
function init()
{ tm = setInterval("type()", 50) }
function type()
{
if (textToShow.length != pos)
{
d = document.getElementById("divText");
c = textToShow.charAt(pos++);
if (c.charCodeAt(0) != 13)
d.appendChild(document.createTextNode(c));
else
d.appendChild(document.createElement("br"));
}
else
clearInterval(tm);
}
</script>
<center><font size=5 color=LightGreen>- Ç åõ÷Þ ôïõ Éçóïý -<p>
<body background=images/anemos.bmp onload=javascript:init()>
<div id=DivText align=Left style="
background: url(images/blueblack.jpg);
border-color: Yellow;
border-style: Groove;
border-width: 10;
width: 900;
height: 520;
color: LightBlue;
font-face: Com;
font-size: 18">
</div>
</body><p>
<form action="progs.php" method="post">
<input type="submit" name="progs" value="ÄéÜöïñá Ðáé÷íßäéá êáé ÐñïãñÜììáôá">
</form>
<form action="show.php" method="post">
<table border=1 width=70% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=images/blue.jpg>
<tr>
<td align=center> <font size=5 color=Lightgreen>Ðþò óå ëÝíå?</font> </td>
<td align=center> <input type="text" name="onoma"> </td>
</tr>
<tr>
<td align=center> <font size=5 color=Lightgreen>Ðïéü åßíáé ôï ó÷üëéï óïõ åðß ôïõ èÝìáôïò?</font> </td>
<td align=center> <textarea name="sxolio" rows=4 cols=25 wrap="on"></textarea> </td>
</tr>
<tr>
<td align=center> <font size=5 color=Lightgreen>Ðïéü åßíáé ôï e-mail óïõ?</font> </td>
<td align=center> <input type="text" name="email"> </td>
</tr>
<tr>
<td align=center> <input type="submit" name="emfanisi" value="ÅìöÜíéóç"> </td>
<td align=center> <input type="submit" name="kataxorisi" value="ÁðïóôïëÞ"> </td>
</tr>
</table>
</form><br>
<?
echo "<center><font size=4 color=Cyan>";
foreach($diary as $lineno=>$line)
if($line=="\n") unset($diary[$lineno]);
$diary = array_values($diary);
$quote=$diary[rand(0, count ($diary)-1)];
echo "<table border=1 width=90% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=images/blue.jpg>";
echo "<tr>";
echo "<td>";
echo "<center><font size=4 color=Cyan>$quote";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "<center><font size=4 color=Orange> $xronos";
echo "<center><font size=4 color=Pink> $host";
mysql_query("UPDATE counter SET index = index + 1");
$sql = mysql_query("SELECT index FROM counter");
$index = mysql_fetch_array($sql);
echo "<center><font size=5 color=Yellow> $index[0]";
echo "<center><a href=personal><img src=images/cross.gif></a>";
mail ("nik0s@mycosmos.gr", "Åðßóêåøç Ïìéëßáò $host => $index[0]", "", "From: 007@spy.gr");?>
Posted: Mon Apr 21, 2003 10:30 am
by volka
I do not understand.
Connect what where why how?
Posted: Mon Apr 21, 2003 10:35 am
by Nik
what is that you dont understand??
iam trying to convert this php script to a perl one....
what is that confuses you?
Posted: Mon Apr 21, 2003 10:47 am
by twigletmac
Nik wrote:what is that you dont understand??
iam trying to convert this php script to a perl one....
what is that confuses you?
Possibly the fact that the subject of the first post was:
Nik wrote:Need help connecting this php script to perl
Mac
Posted: Mon Apr 21, 2003 10:57 am
by twigletmac
Nik wrote:iam trying to convert this php script to a perl one....
How far have you got?
Nik wrote:what about other type of coding like perl? what tags must i use for that?
BTW, for code other than PHP you can use [ code ]...[ /code ] tags (without the spaces after [ and before ] - that's just to stop the board converting the tags).
Have a read of:
faq.php?mode=bbcode
Mac
Posted: Mon Apr 21, 2003 11:07 am
by Nik
yes what i meant is converting not connecting, my mistake....
well i did connect to the mysql database but what i dont know is that the html and javascript that ia hve will be interpreted correctly by perl??
mine wont work... can u pls rewrite this php to perl where ever u see some changes? i cant dont where to chabge what....
Thank you.
Posted: Mon Apr 21, 2003 11:39 am
by volka
neither html nor javascript is interpreted by perl in your script.
You're printing something that the
client might (if you're lucky

) interpret as html/js.
please post your perl-code.
Posted: Mon Apr 21, 2003 12:18 pm
by Nik
First of alla thanks one more time Volka for your precious help!
Here is the code as far as i can get it!
Code: Select all
#!perl
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBI;
use DBD::mysql;
print header(), start_html( {title => "Äéäá÷Ýò Ðáôñüò Ðáéóßïõ", background => "/images/anemos.jpg"} );
$db = DBI->connect("DBI:mysql:nik_db", {RaiseError => 1}) or
$db = DBI->connect("DBI:mysql:nik_db", "nik", "macgyver", {RaiseError => 1});
$xronos = localtime; $i=0;
$host = gethostbyaddr($ENV{'REMOTE_ADDR'}, 2);
print "<body background=/images/anemos.jpg>";
print "<font size=5 color=Yellow>";
#foreach (@tainies) { print "<option> $_ </option>" };
<script type="text/javascript">
var textToShow = "<? echo join('"+String.fromCharCode(13)+"', array_map('addslashes', (array_map('trim', $data)))); ?>";
var pos = 0;
var tm;
function init()
{ tm = setInterval("type()", 50) }
function type()
{
if (textToShow.length != pos)
{
d = document.getElementById("divText");
c = textToShow.charAt(pos++);
if (c.charCodeAt(0) != 13)
d.appendChild(document.createTextNode(c));
else
d.appendChild(document.createElement("br"));
}
else
clearInterval(tm);
}
</script>
<center><font size=5 color=LightGreen>- Ç åõ÷Þ ôïõ Éçóïý -<p>
<body background=/images/anemos.bmp onload=javascript:init()>
<div id=DivText align=Left style="
background: url(/images/blueblack.jpg);
border-color: Yellow;
border-style: Groove;
border-width: 10;
width: 900;
height: 520;
color: LightBlue;
font-face: Com;
font-size: 18">
</div>
</body><p>
<form action="progs.php" method="post">
<input type="submit" name="progs" value="ÄéÜöïñá Ðáé÷íßäéá êáé ÐñïãñÜììáôá">
</form>
<form action="show.php" method="post">
<table border=1 width=70% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=/images/blue.jpg>
<tr>
<td align=center> <font size=5 color=Lightgreen>Ðþò óå ëÝíå?</font> </td>
<td align=center> <input type="text" name="onoma"> </td>
</tr>
<tr>
<td align=center> <font size=5 color=Lightgreen>Ðïéü åßíáé ôï ó÷üëéï óïõ åðß ôïõ èÝìáôïò?</font> </td>
<td align=center> <textarea name="sxolio" rows=4 cols=25 wrap="on"></textarea> </td>
</tr>
<tr>
<td align=center> <font size=5 color=Lightgreen>Ðïéü åßíáé ôï e-mail óïõ?</font> </td>
<td align=center> <input type="text" name="email"> </td>
</tr>
<tr>
<td align=center> <input type="submit" name="emfanisi" value="ÅìöÜíéóç"> </td>
<td align=center> <input type="submit" name="kataxorisi" value="ÁðïóôïëÞ"> </td>
</tr>
</table>
</form><br>
print "<center><font size=4 color=Cyan>"
@diary = open(IN, "diary.txt");
while (<IN>) { push (@diary, $_); }
$quote=$diaryїint(rand(@diary))];
print "<table border=1 width=80% align=Center bordercolor=Yellow style=border-style: solid; border-width: 5 background=/images/blue.jpg>";
print "<tr>";
print "<td> <center><font size=4 color=Cyan> $quote </td>";
print "</tr>";
print "</table><br>";
print "<center><font color=Orange> $xronos";
print "<center><font color=Pink> $host";
$sql = "UPDATE counter SET visitor=visitor+1";
$db->do( $sql );
$st = $db->prepare( "SELECT visitor FROM counter" );
$st->execute();
@visitor = $st->fetchrow_array();
print "<center><font size=5 color=Yellow> $visitorї0]";
print "<center><a href=personal><img src=images/cross.gif></a>";
#mail ("nik0s\@mycosmos.gr", "Åðßóêåøç ÁôæÝíôáò $host => $visitorї0]", "", "From: 007\@spy.gr");
Posted: Mon Apr 21, 2003 3:51 pm
by volka
ok, back to your php-code

Let's convert the script step by step.
echo "<table border=1 width=90% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=images/blue.jpg>";
for example can be done outside a php-block simply by
Code: Select all
<table border=1 width=90% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=images/blue.jpg>
The output is exactly the same, so the browser will not notice any difference.
Can you extract all the static parts (that would not need php at all) ?
Start with
Code: Select all
<script type="text/javascript">
var textToShow = "just a test, so php-code here replaced by an example string";
var pos = 0;
var tm;
function init()
{ tm = setInterval("type()", 50) }
try to use indents in way that you can see matching tags (opening/closing)
e.g.
Code: Select all
<html>
<head>
<title>test</title>
<script type="text/javascript">
function doNothing()
{
return true;
}
</script>
</head>
<body>
<p>
<!-- use html comments to describe php-blocks you have to remove here -->
<!-- something like: -->
<form action="test.php" method="POST">
<!-- a while-loop adding an _input text_ element for each record fetched from database -->
<input type="button" value="click me" onClick="javascript:doNothing();" />
</form>
</p>
</body>
</html>
You will see it helps.
Posted: Mon Apr 21, 2003 4:06 pm
by Nik
i'am sorry but i didnt understand anything!
cant i have html and javascript inside my perl file?
cant the browser identify which is which?
pls post my perl prog as u think it may appear to help me...
Posted: Mon Apr 21, 2003 5:01 pm
by volka
as mentioned: step by step.
Take your php-script and extract all static html, just try.
Everything outside the php-blocks is static html (in this case), and everything without loops and variables is static, too. e.g
echo "<tr>";
that's static, there's no need for php here, a flat file could do the same.
For everything you cannot change write a html-comment, that describes what has to be done there.
just try.
Posted: Mon Apr 21, 2003 5:47 pm
by Nik
well here is my script without the static html and also without the javascript code....
Code: Select all
#!perl
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBI;
use DBD::mysql;
print header(), start_html( {title => "Äéäá÷Ýò Ðáôñüò Ðáéóßïõ", background => "images/anemos.jpg"} );
$db = DBI->connect("DBI:mysql:nik_db", {RaiseError => 1}) or
$db = DBI->connect("DBI:mysql:nik_db", "nik", "macgyver", {RaiseError => 1});
$xronos = localtime; $i=0;
$host = gethostbyaddr($ENV{'REMOTE_ADDR'}, 2);
print "<body background=/images/anemos.jpg>";
print "<font size=5 color=Yellow>";
#foreach (@tainies) { print "<option> $_ </option>" };
print "<center><font size=4 color=Cyan>"
@diary = open(IN, "diary.txt");
while (<IN>) { push (@diary, $_); }
$quote=$diaryїint(rand(@diary))];
print "<center><font color=Orange> $xronos";
print "<center><font color=Pink> $host";
$sql = "UPDATE counter SET visitor=visitor+1";
$db->do( $sql );
$st = $db->prepare( "SELECT visitor FROM counter" );
$st->execute();
@visitor = $st->fetchrow_array();
print "<center><font size=5 color=Yellow> $visitorї0]";
print "<center><a href=personal><img src=images/cross.gif></a>";
#mail ("nik0s\@mycosmos.gr", "Åðßóêåøç ÁôæÝíôáò $host => $visitorї0]", "", "From: 007\@spy.gr");
I like this step by step handling of things!
Whats my next step??
Posted: Mon Apr 21, 2003 5:56 pm
by Nik
I also want to remind you Volka that this is inend to be a Perl script not a php script, i am telling you this because from your last posts you metnion php code, you mean perl code right?