Welcome to WebmasterWorld Guest from 54.162.151.77

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Is this a job for Perl?

     
9:44 am on Mar 6, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 7, 2003
posts:1408
votes: 0


I receive a silent POST to a perl script. Can I use the received data and pass it as parms to a php script?

It would be like: GET file.php?Parm1&Parm2

I've already coded the php to process the parms and update my database... not really wanting to do it all over in Perl.

1:17 pm on Mar 6, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 19, 2000
posts:366
votes: 0


Hi grandpa,

It would take a simple perl script to either show the content/result of file.php?Parm1&Parm2 or 301 redirect to it.

The lines below should do a 301 redirect passing the parameters, it's the full script (not tested).

------------------------
#!/usr/bin/perl

print "Status: 301 Moved Permanently\r\n" .
"Location: file.php?$ENV{'QUERY_STRING'}\r\n" .
"\r\n";

------------------------
If you want to avoid having to do anything in perl you could also redirect on the server level, a call to "script.cgi?Parm1&Parm2" can silently be converted into a call for "file.php?Parm1&Parm2"

This can be achieved using mod_rewrite [httpd.apache.org] or mod_alias [httpd.apache.org] if you're on Apache.

6:41 am on Mar 7, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 7, 2003
posts:1408
votes: 0


Thanks Damien

I've doodled with this thing off and on all day... I guess I'm gonna have to learn enough perl to make it work that way. I guess if that's my only 'downer' for the week I'm doing OK...

1:37 am on Mar 8, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 7, 2003
posts:1408
votes: 0


OK, it *is* a job for perl.

I spent my Saturday night with a mug of coffee, a pack of smokes and I learned about DBI, POST, GET and a few other things. I even wrote a perl script from scratch, no cut and paste this time.

But it seems to have a small flaw that I still don't understand, maybe someone can point me towards enlightenment.

From the top: My shopping cart will send me a silent post for any item that's purchased. Right now, that post is to a script in my cgi-bin directory, call it script.pl.

I receive the parameters with STDIN and split off the parameters. I can massage the information I receieve and perform an update to MySQL. When I'm in a test mode I need to see some results, so I use this line in my script:
print header(); # HTTP headers

So far so good.

But, since this is a silent post I don't need to see the output in a production mode. In fact, my guess, that would actually be a bad thing. The shopping cart isn't requesting a page, they're sending me data.

So I'm left with 2 questions in this process.
If I remove the HTTP headers I get a boatload of errors.
What would I use instead? Some sort of headers are obviously required....

Second, in theory I should send a response code back to let the cart know the data was received and processed. I assume this is echo'ed and not printed. But I'm having trouble with this, and I'm guessing it goes back to the HTTP header situation.

Insight is appreciated.

6:26 pm on Mar 8, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 22, 2004
posts:59
votes: 0


Hi grandpa,

It sounds to me as if you are printing the HTTP header so you can debug output from your script. When you remove the print header() line do you also remove the rest of your debugging lines? If you contrinue to send the debugging to STDOUT without the header, you will get lots of errors.

As far as responding to the other script. I guess that you are trying to communicate with it using an HTTP protocol. You send it a request, it sends you bacj=k some data, you process the data, and respond that the request was received OK. You need to send the requests and responses to the other script using HTTP. I hope you are doing all this with the LWP module. It will make your life much simpler.

I have only skimmed over the top of how I think this is working. Let me know if I am going in the right direction and let's get into some details of how this should work.

7:50 pm on Mar 8, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 19, 2000
posts:366
votes: 0


Not sure I fully understand the issue with the headers, but I think you could solve it by adding the following line instead of
print header();
try
print "Content-type: text/html\n\n";

As far as I understand it you wish to have a form post to a perl script, update the database and print a "thank you" or confirmation page for the user to see.

Here's a script example that could do that, maybe it helps. It's not a full script, just some syntax examples. It shows how to print a confirmation page or redirect to one.
You do not need any modules for it.

[perl]
#!/usr/bin/perl

# a simple way to extract parameters from a post
# it assumes a name=value&name2=value2 format
my %vars = map {my($k,$v) = split(/=/)} split(/&/, $ENV{"QUERY_STRING"});

## if there was a field in the form named "username", you can now use
## $vars{'username'}
## to acces the value of 'username'
## Note this script does not have any security checks to check the values passed to this script

## save stuff in mysql here
# .........
# username = $vars{'username'}
# address = $vars{'address'}
# .........

## the thank you message and headers
# place a comment in front of the next lines
# if you wish to redirect after saving the data

# headers
print "Content-type: text/html\n\n";

# message in regular html format
print qq~
<html><body>
some html here
</body></html>
~;

## OR a redirect to the thankyou page
## Uncommment lines below to enable
## print "Status: 302 Moved Temporarily\r\n" .
## "Location: thankyou.html\r\n" .
## "\r\n";

[/perl]

2:08 am on Mar 9, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 7, 2003
posts:1408
votes: 0


A new day, a new set of problems :)

The headers issue is resolved, at least for now. What I *think I understand* is that any request to a server requires a header. When I was ready to comment out my print lines (after testing) I also commented out my headers, thinking "Who needs 'em.. I'm not printing anything"

After reading more, I think that now I want to do the impossible. Figures....

Here's the process:
1) customer fills in a form with widget and clicks submit
2) form is POST'ed to shopping cart
3) shopping cart POST's back to my script

This actually works now. I had a bear of a time with step 3 because the data coming back was not in the same order that the cart documentation indicates. I finally got smart and dumped their POST and figured out where everything was located.

That was last night. The BIG problem I have now is that the data sent to my script (Step 3) is no good... at least for the purposes I want. So I need to do the impossible, POST the form results to a script where I can put all the pieces back together again (this has been done) and THEN, sent it as a POST to the cart. Everything I read says it can't be done that way, ie: POST -> process -> POST,
or if can be done it relies on taking advantage of broken standards.

I think at this point maybe I need to have another look at my database design and try to figure out how to get the data I *really* want into my form.

11:35 am on Mar 10, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 7, 2003
posts:1408
votes: 0


I know it's not necessarily topical to perl, but would anyone care to comment on this from the previous message:

POST -> process -> POST ->

accurately expanded to this:

POST -> process /update db -> POST -> original page

The process is to POST from a form on my page to my script.
The script does its work and then sends a POST to another script. That script would have to return to the web page with the form.

From what I've read I understand this isn't possible, no matter how much I want it.

4:53 pm on Mar 10, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


anything is possible but sometimes very difficult or just ridiculous. :)

- POST -> process /update db

no problem plain and simple

- POST -> original page

now this is open to interpretation. Why do you need to post again? You've written it to the db so you don't need to post all of the information. A possibility is just passing the id and then grabbing the full data from the db.

You could even redirect to the original page with a query string so it can reload the info.

If you really want to repost you can use curl but it seems a little over the top.

10:17 pm on Mar 10, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 7, 2003
posts:1408
votes: 0


anything is possible but sometimes very difficult or just ridiculous. :)

Being a relative babe-in-the-woods I'd tend to lean to the ridiculous...

Why do you need to post again?

The first POST is an intermediate step I'd like to implement. Problem is, everything after that step needs to proceed as if the step were not there. So, the second POST represents the actual action that is taken right now.

It sounded kinda silly to me, too. But if it's doable I could proceed while I try to develop a more permanent solution.

10:48 pm on Mar 10, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


do you have the option of doing the db update on the same page that you need the second post for?
11:19 pm on Mar 10, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 7, 2003
posts:1408
votes: 0


I sure don't. That page is beyond my reach.