Gossamer Forum
Home : General : Perl Programming :

Redirect

Quote Reply
Redirect
Does anyone know how a redirect code would look, if I wanted a cgi script that sends the mail then goes to another page?

I already made the CGI script
here is some of the script itself(I put part of it so no one can use the script) Smile
(I just want to add the redirection part to the bottom at the elsif[/] part it is in quote

Code:
print MAIL "Subject: $subject\n\n";

print MAIL " -------------------------------------------------------\n";
print MAIL " The following form was sent by: $FORM{'name'}\n";
print MAIL " on $date\n\n";
print MAIL " -------------------------------------------------------\n\n";

foreach $key (keys(%FORM)) {
print MAIL "$key : $FORM{$key}\n\n";
}

close(MAIL);
###########################################
Quote:
if ($Choice eq "1") {
print "$thanx";
}
elsif ($Choice eq "2") {
print "HTTP/1.0 302 Temporary Redirection\r\n" if $ENV{PERLXS} eq "PerlIS";
print "(Location: \"http://nytesoft.hypermart.net\"\n\n)";
}
elsif ($Choice eq "3") {
print "$thanx2";
}
exit;
##########################################
#DONE! You don't need to edit anything below.
##########################################


sub dienice {
($errmsg) = @_;

print "<center><font color=\"000080\"><b><h2>Houston, we have a problem.</h2></b></center></font>\n";
print "$errmsg<p>\n";
print "</body></html>\n";
exit;
}
Smile

------------------
Patrick Chukwura
http://nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
Hiya,

With this bit here you have a little bit too much...
print "HTTP/1.0 302 Temporary Redirection\r\n" if $ENV{PERLXS} eq "PerlIS";
print "(Location: \"http://nytesoft.hypermart.net\"\n\n)";

You don't need to pass any header info with redirection...you could change it to this:

print "Location: http://www.where-do-you-want-to-go.com\n\n";

You'll also notice you don't need to use any extra quotes. Also after the .com/youcan/add/extra/info.htm without any problems.

Hope this helps Smile

Rod
Quote Reply
Re: Redirect In reply to
It still doesnt work, all it does is when I try is it actually prints Location: http://www.where-do-you-want-to-go.com\n\n

------------------
Patrick Chukwura
nytesoft.hypermart.net

[This message has been edited by niteridarz (edited April 09, 1999).]
Quote Reply
Re: Redirect In reply to
Hi,

I would think that you've printed the Content -Type=text/html header somewhere before you got to printing the Location one. Because of this it's just printing the Location header as text, rather than reading it as one.

Sorry if this is gibberish, I'm knackered. I'll come back tomorrow and try again! Smile

adam
Quote Reply
Re: Redirect In reply to
Yes I did, is that the problem? If so I will fix it now.

------------------
Patrick Chukwura
http://nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
It worked! I had to move the print 'content' crap below the part I wanted to change.

------------------
Patrick Chukwura
http://nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
One problem, When I use that it works but I have to put it above the print Content type line. Is there a way I can write the print redirect thing
Code:
print location: www.mysite.com\n
under the print Content area? I relly need to know.

------------------
Patrick Chukwura
http://nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
Patrick,

What's happening is that you're telling the browser to do two things, all in the one routine. By printing the Content-Type header first you're telling it that what's it's about to receive is text or html and it prints it. And if you print the Location header first, you're telling it to go somewhere else, so it won't print the HTML. You can't have both.

You have to divide up these two routines and decide what you want to do. Do you want to show the user a page or send them off somewhere else?

If you still don't understand, post a link to it here and I'll have a look at it. Just make sure that you rename it to .txt to I can read it.

adam
Quote Reply
Re: Redirect In reply to
dahamsta: I tried to switch the places of the two things I want it to do. I want users able to pick 3 choices, 1 will give them a thank you page, 2 will redirect them to a diffent page(thats where the trouble is) 3 will show them a thank you message then redirect them, instead on choice 3 I use the HTML redirect. It redirects correctly, it's just that when I put the elsif thing for the choices before the string that tells the script not to send the form if the user misses a field, it redirects anyway, not telling the user that the mail was not sent, or they missed a field.
You can view the file here( i took out the link, for security resons)
------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net

[This message has been edited by niteridarz (edited April 10, 1999).]

[This message has been edited by niteridarz (edited April 10, 1999).]
Quote Reply
Re: Redirect In reply to
Patrick,

Well, it's all a bit confused really, you seem to have spent more time with the lovely formatting that with the actual script! Smile For a start, your process is all mixed up, the flow is all over the shop. I find that it's easier to follow what you're doing if you put every separate process into it's own subroutine, and then call those subroutines as you need them with &subroutine;. It makes it easier to follow the process of the script as it does the business.

I've stripped out all of your comments and rejigged it the way I'd do it. Note that I haven't checked the operation of any of your subroutines, so I can't guarantee it will work, I'm just showing you the way the program should flow.

http://dotcgi.com/post.txt

I don't like doing it this way, but trying to explain why the whole thing is mixed up could take all day. Read my comments and follow the flow of the program. And don't just put your comments back in and release it! Smile

adam
Quote Reply
Re: Redirect In reply to
THanx, no I wont copy that and use it as my own, I dont do that type of stuff. You should get rid of that link so no one else can. Smile

------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
I will redo the whole script and just call it Nyteform 2.0 Smile

------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
dahamsta: I have recently made a guestbook, it writes to a .html file. Now I want to know how would I write a code if I wanted the user or an administrator to be able to delete certain things in the .html file that the script writes to? Also what if I wanted the script to write between a certain area in the .html file, like if I wanted it to go between an image, and a copyright sentence?

------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net

[This message has been edited by niteridarz (edited April 12, 1999).]
Quote Reply
Re: Redirect In reply to
Also how do you make a CGI script create a file?

------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
Here is an easy way to write a file:
Code:
open(FILE,">/path/to/file/to/be/created.ext")
print FILE "Whatever u want in the file";
close(FILE);
Be sure to include the > before the path to the file, this tells perl to write the file. If you want to print muiltipable lines in the file, include print FILE before the data. Hope this helps.

Matt

[This message has been edited by matt (edited April 12, 1999).]
Quote Reply
Re: Redirect In reply to
Matt:no, I know how to write to a file already. I'm asking how do you delete certain stuff from a file a CGI script writes to and how do you make a CGI script create a file, like Link and lots of other scripts do. Just look at the post that I posted before this one. Smile

------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net

[This message has been edited by niteridarz (edited April 12, 1999).]
Quote Reply
Re: Redirect In reply to
This code will not only write a file but it will create a file too. Just set the path/to/file.ext to the path, and the name of the file, and its file type that you want to be created.
Code:
open(FILE,">/path/to/file.ext");
print FILE "Whatever u want in the file";
close(FILE);

Matt
Quote Reply
Re: Redirect In reply to
oh, I thought it was just to open en existing file

------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
Patrick,

It will create the file if it does not exist and overwrite the file if it does exist. If that is what you want to do, no problem. But if you do not want to overwrite the file, a better way would be:

Code:
(-e /path/to/file.ext) ?
open(FILE,">/path/to/file.ext") :
open(FILE,">>path/to/file.ext");
print FILE "Whatever u want in the file";
close(FILE);

Now, if the file does not exist, it will be created. If it does exist, it will be appended to.

[This message has been edited by Bobsie (edited April 13, 1999).]
Quote Reply
Re: Redirect In reply to
Patrick,

Sorry about the delay, I didn't notice your posting. I've been cruising a particularly nasty flame thread which so far has spanned three forums. Highly entertaining, but I'm glad I'm not involved. Smile

Anyway, there's a slight difference in creating a file and just writing to it...

This will create a new file OR overwrite the current contents when you print to it:

Code:
open(FILE,">$file");

And this opens a file and appends what you print to it at the END of the file. Notice there's two arrows instead of one:

Code:
open(CART,">>$cart");


Ok? Now your guestbook. Let me just say first that I'm no expert on Perl, I'm a learner just like yourself. Take my advice and browse a few of the tutorials on cgi-resources.com . I wish I'd done it earlier myself. Probably the best one for a beginner is cgi101.com which is nice and simple and even has a downloadable .pdf for referring to offline. I still open it about twice a day! Smile

Right, well the usual way to set up a file you're writing to for administration is to add something that the Perl script can recognise while it's parsing the file. Something that wouldn't normally be in a HTML file, like <%%marker%%> or something. One misconception I used to have is that Perl scripts can write to or delete something from the middle of a file. But that's not how it's done. What you have to do is read the data from the file line by line, and then read it back into another variable which you'll write to the file when you're done.

So if you want to write to it in a specific place, you open the file, read it line by line into a new variable, and you wait until you hit the marker you're looking for. Then you add your new data into the new variable, finish off the rest of the parsing and print the final data back to the file. If you wanted this to be an ongoing process, you'd print another marker in the data you write.

(Gawd I'm not very good at this am I?)

And if you wanted to delete from it, you just don't write the data you want to skip to the new variable, so when you print it to the file it's not there.

However, if you want to get specific about placement, say you want to insert something between the third and fourth entries in your guestbook, you have to use some kind of count to keep an eye on where you are in the file. The administration on guestbooks I've seen works something like this (but they're data-driven).

It sounds pretty complicated, and it's more difficult working with a HTML file anyway because you have to parse through all the HTML as well. If I was you I would practise with some plain text files first. Here's a commented routine that you would use to parse a file for <%%marker> and delete it, just to give you a basic idea of what I'm talking about.

Code:
# Setup -- What's your marker?
$marker = "<%%marker%%>";

# Open the file. If you can't, send off to an error routine.
open (FILE,"$file") | | &error("Can't access $file for reading.");
# Put the data into the @lines array.
@lines = <FILE>;
# Close the file. We're only reading it now.
close(FILE);

# For each line in the array...
foreach $line (@lines) {
# Remove the line breaks...
chomp($line);
# And unless the $line variable is the same as $marker...
unless ($line eq $marker) {
# We add it to a new variable - $newdata.
$newdata .= "$line\n";
}
}

# Now, open the cart for writing.
open (CART, ">$cart") | | &error("Can't access cart for writing -- $cart");
# Print the new data.
print CART "$newdata";
# Close the file.
close (CART);

Done!

I hope this helped a little anyway. Now, if you'll excuse me, all this was just an excuse to get away from my own work, so I'm off! Smile

adam

(Bugger, I *always* make *some* mistake!)

[This message has been edited by dahamsta (edited April 13, 1999).]
Quote Reply
Re: Redirect In reply to
Yeah thanx, I have went to cgi-resources and cgi101 but that class is not finished there, It only goes to chapter 6. I might plan to buy the book! What three forums are you talking about?

------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net
Quote Reply
Re: Redirect In reply to
can anyone answer my question at http://www.gossamer-threads.com/...um3/HTML/001358.html ? If so answer it at that forum.

------------------
Patrick Chukwura
Nytesoft WebSolutions
nytesoft.hypermart.net