PDA

View Full Version : Can someone clue me in so that this Perl Script can be done in PHP.


Sscotties
08-14-2002, 10:35 AM
Calling Applet to server. Make my Applet work.
Somebody help me please! I'm a student and I know that Applets aren't
really meant to behave this way - and even if they do, they may not be
allowed to due to local Firewall restrictions. So, my appeal for help
is as much academic as it is practical. So, please don't tell me about
servlets or the magic of JSP. I want my Applet to talk! The Applet
program is listed here (below) and this accesses a Perl cgi-bin script
on my localhost (127.0.0.1) Apache server. I am using Windows (98 se) and
have created a public_html folder and even tried with a opinion.txt file;-

#! C:\perl\bin\perl.exe
open(OUT, "> /public_html/opinion.txt");
print "content-type: text/plain\n\n";

while (<>) {
print OUT $_;
print $_;
}
close (OUT);
exit 0;
# end of script


I'm a total beginner with Perl, so I don't have the first clue how to
correct the Perl Script, if it is wrong. When I check the server log,
it says it has accessed the script with the following message;-

"POST /cgi-bin/wdwrite.pl HTTP/1.1" 200 64



Here, also is the Applet. It's quite a simple progam;-

/** postMethod.java - see also wdwrite.pl
* Textfied and button. The button calls to a function writePage()
* this then gets the host() and opens a URL connection to send the
* data out, back to the place the Applet came with a dataOutputStream
* object and writeBytes() and linked back to a Perl cgi-bin program.
*/

import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*; //Tidy these up later with .ActionEvent etc;

public class postMethod extends Applet implements ActionListener {
TextArea info;
Button Bsend;
String str;
String host, conext, exc1, exc2;

//*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X
// init the textfield and button. Only the button needs ActionListener

public void init() {
info = new TextArea(5,68);
add(info);
Bsend = new Button("SEND");
Bsend.addActionListener(this);
add(Bsend);
}

//*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X
// show the results or exceptions in a series of strings

public void paint (Graphics g) {
g.drawString("Got this: "+str, 30, 150); // message 2B sent
g.drawString(host, 30, 170); // getHost().getCodeBase()
g.drawString(conext, 30, 190); // getConnection.toSting()
g.drawString(exc1, 30, 210); // didn't like the call to host
g.drawString(exc2, 30, 230); // didn't like the writeBytes()
}

//*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*
// button links to the Post Method and calls the function writePage()

public void actionPerformed(ActionEvent e) {
if (e.getSource() == Bsend) {
str = info.getText();
try {
writePage(); // Has to be caught
}
catch (Exception e1) {
exc1 = "writePage: " + e1.toString();
}
}
repaint(); // exceptions /results etc
}

//*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*
// writePage function returns to repaint. Links the page back to
// whence it came and directs it to the Perl script in cgi-bin
// Note: try-catch are not necessary here. ie: it should still work
// without this. The purpose is to catch an error message should
// an error occur. Sent as a String in repaint() g.drawString

public void writePage() throws Exception {
host = getCodeBase().getHost();
try {
URL url = new URL("http://"+host+"/cgi-bin/wdwrite.pl");
URLConnection con = url.openConnection();
conext = "connect OK: " + con.toString();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-type", "text/plain");
con.setRequestProperty("Content-length", str.length()+" ");

DataOutputStream out = new DataOutputStream(con.getOutputStream());
String content = URLEncoder.encode(str);
out.writeBytes(content);
out.flush();
out.close();
exc2 = "Seems OK: reads to final line.";
}
catch (Exception e2){
exc2 = "dataOutputStream: " + e2.toString();
} // end try-catch
} // end writePage()
}

Spookster
08-14-2002, 02:00 PM
Well what exactly is the perl script suppose to do?

Sscotties
08-15-2002, 08:43 PM
To receive the information inputted by the client from a textfield on the applet, so when the user hits the send button the file is saved onto the server. The Perl Script is meant to take the text and save it to a file called information.txt in the public_html folder

mordred
08-16-2002, 12:08 AM
Because I know virtually nothing of Perl and just a little of Java, I'm not totally sure if my reply can be of any help, but here it is, a code snippet in PHP (since that's what you asked in your thread headline) that will print all POST variables to textfile called information.txt. More precisely, each variable is appended as a new line to the file. Might not be perfect, but should give you a starting point for further refinement to your needs.


if (count($_POST) > 0) {
$file = fopen("information.txt", "a");
flock($file, 2);

$str = "";

foreach ($_POST as $key => $value) {
$str .= $key . ":" . $value . "\n";
}

fwrite($file, $str);

flock($file, 3);
fclose($file);
}

Sscotties
08-17-2002, 08:11 PM
I'm really busy these next few days, so I probably won't get the chance to test ur submission 'til around Tuesday or so. Meanwhile I'm very grateful indeed.
Scotties9

Sscotties
08-20-2002, 01:52 PM
again I'm getting a "200" message from the Apache web log - so it accesses your php file OK and again f-all happens.
It's driving round the bend!!!

True !

I've tried so many code snippets, changes /modifications to the java applet that I hardly know which end is up any more. I've tried Perl and now php and I cannot get this thing activated. What is beyond me is why /how can Sun come up with a technology that seems so hard to implement when what I'm trying to do should be so simple. It should be part of the design fabric of the whole technology - AND MADE EASY TO DO - there is no book or web site that I know of that says to you - put this in your web site and bingo - the magic of applets!

I am still VERY grateful for your support, though going mildly barmy with this thing

Simon