Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to take source code of page and save as html file?

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
0
0
i am generating html pages based on name=value pairs...i would like to take the source code of the page i create and save it as an html page, the name of the file being the name=value pairs i used to generate it...

for example:

let's say i create a page using this - index.pl?a=dry-rabbit-food

and the new page that is generated is -

<html>
<title>Dry Rabbit Food</title>
</html>
<body>dry rabbit food is great!</body>
</html>

now, i want to take the source code and then save as a new file, called dry-rabbit-food.html

i want to do this automatically.

similar to this, i use this to create a new html file:

Code:
#!/usr/bin/perl
use CGI qw(:standard);

$cgi = new CGI;

$cfile0 = "new-html-file.html";
open (COUNTER, "$cfile0");
$count0 = <COUNTER>;
close (COUNTER);

$count0 = "
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
";

open (COUNTER, ">$cfile0");
print COUNTER ("$count0");
close (COUNTER);

print $cgi->header();
print << "EOF";
<html>
<head>
<title></title>
</head>
<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>
new page has been created and saved!
</body>
</html>
EOF

this works great if i already know what i'm going to have on the new html file i create, but on a larger scale, the new page i'm going to create is dynamic, so the above code doesn't help me.

how can i accomplish this?

- g
 
Way to skim the post too quickly, ishnid. ;-)

Going by your example of "index.pl?a=dry-rabbit-food", you would get the param "a" from the query...

Code:
use CGI;
my $q = new CGI;

my $name = $q->param ("a");

Then...

Code:
open (WRITE, ">$name.html");

That would save the file as "dry-rabbit-food.html"

Note that you'll want to make sure $name is an acceptable value. i.e. I could go to, say,

Code:
index.pl?a=../../hidden_folder/.htaccess\0

(where \0 is a null byte), and instead of modifying a file you think it would modify, it would modify a file named ".htaccess" which is in a "hidden_folder" two levels higher up in your web directories (the null byte \0 would make your code not append ".html" so they would edit ".htaccess" and not ".htaccess.html")

As for what you plan on generating as the page contents, you can use param() to get other query string arguments.

Code:
# /index.pl?schoolbus=yellow&barn=red&sky=blue
my $schoolbus = $q->param ("schoolbus"); # yellow
my $barn = $q->param ("barn"); # red
my $sky = $q->param ("sky"); # blue

-------------
Cuvou.com | The NEW Kirsle.net
 
thanks.

i know how to take the param contents and make html pages...the page that is generated from the query "a=" is dynamic and database driven...

that is to say:

index.pl?a=dry-rabbit-food

would create this page:

Code:
<html>
<head>
<title>Dry Rabbit Food</title>
</head>
<body>
We have several dry rabbit food products available for you:
<br><br>
<br>
Food Option 1 - $9.99
<br>
Food Option 2 - $11.99
<br>
Food Option 3 - $15.99
<br>
</body>
</html>

when the parameters are passed to the .pl page, this one being "dry-rabbit-food", a database is called and information about dry rabbit food is passed and put on the page.

that's why it's dynamic.

so, once the .pl page is generated, i need to take the source of the page and save as a file called "dry-rabbit-food.html"...

is that a little clearer?

- g
 
thank you ishnid...your answer was perfect.

here's what i got, and it works:

Code:
#!/usr/bin/perl

use CGI qw(:standard); 
use LWP::Simple;

$goPage = param("goPage");

$content = get("[URL unfurl="true"]http://www.myrabbitsite.com/index.pl?a=$goPage");[/URL]

$goPageNew=$goPage.'.html';
open (COUNTER, ">$goPageNew");
print COUNTER ("$content");
close (COUNTER);

print "Location: [URL unfurl="true"]http://www.myrabbitsite.com/$goPageNew\n\n";[/URL]

that works great!

i named the above file genPage.pl and pass the info to it using the value=pair system...

for instance, i type in genPage.pl?goPage=dry-rabbit-food and it takes care of the rest.

thank you again, ishnid. that's why i love perl...it does everthing i need!

- g
 
or use the getstore() function of LWP::Simple

Code:
#!/usr/bin/perl

use CGI qw(:standard);
use LWP::Simple;
print header(),start_html();
$goPage = param("goPage") or die "No name was given";
getstore("[URL unfurl="true"]http://www.myrabbitsite.com/index.pl?a=$goPage","$goPage.html");[/URL]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top