Forum Moderators: coopster & phranque

Message Too Old, No Replies

Placing the title in the URL.

         

Jesse_Smith

3:39 am on Dec 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How would I edit this script so the title can be in the URL as

file.cgi?title=Title_Goes_Here

and then in the content area using $title or $FORM{'title'}

so the title shows up at the title and header with out the _? (Right now I can't even get the title to show up in the title and header.)


#!/usr/bin/perl

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ s/"//g;
$value =~ s/_/ /g;
}
sub get_url_input {
}
if (%FORM) {
if ($FORM{title}) { $FORM{title} = $FORM{title}; }
}

{
print "Content-type: text/html\n\n";
print <<EOM;
<TITLE>$title or $FORM{'title'}</TITLE>
<H1>$title or $FORM{'title'}</H1>

CONTENT AREA

EOM
}

lZakl

2:21 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



I am going just by what I know... Is there a reason you're not using the POST method? I would try this maybe?

[code]
1) THE HTML FILE

<html>
<head>
<title>
Title sender
</title>
</head>
<body>
<br><br>
Enter your title
<FORM METHOD="POST" ACTION="title.cgi">
<INPUT TYPE="text" NAME="title" SIZE="15" MAXLENGTH="15">
<INPUT TYPE="SUBMIT" VALUE="set title">
</FORM>
</body>
</html>

2) THE CGI FILE

#!/usr/bin/perl

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

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );

@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$title = $FORM{title};

print "<html>\n";
print "<head>\n";
print "<title>$title</title>\n";
print "</head>\n";
print "<body>\n";
print "The title of my page is $title \n";
print "</body>\n";
print "</html>\n";
[\code]

P.S. I didn't test the CGI, so watch for typos.. ;0)

lZakl

2:26 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Also in the single quote inside the $FORM{'title'} can hinder sometimes I have found... I would try removing them.

Jesse_Smith

2:03 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not using a form. Trying to just use the URL to place the title in the page.

file.cgi?title=test_goes_here

Then in the header/title 'test goes here' would show up.

That code is edited from another script that did use forms.

volatilegx

3:25 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's pretty simple. You get the extra URL data by grabbing the $ENV{'QUERY_STRING'} environment variable.

$temp = $ENV{'QUERY_STRING'};
($varname,$content) = split(/=/,$temp,1);
$var{'title'} =~ s/_/ /;

print "Content-type: text/html\n\n";
print <<EOM;
<TITLE>$var{'title'}<TITLE>
<H1>$var{'title'}</H1>

CONTENT AREA

EOM

rocknbil

3:29 pm on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What both of these posts are getting at is that the method POST reads STDIN, GET (which is what you're doing there) is read on the query string. The single quotes will have no detrimental effect on your hash and allows you to use spaces in keys, as in $FORM{'this is my key'}. It is unnecessary for single word keys.

As a side note - is there a particular reason you're using an underscore? If you use a + or the encoded space %20, any of a million read/parse routines will turn the title into plain text with spaces. Note the extra step is removed to sub out the underscore, and that it extracts the encoded data regardless of GET or POST.

file.cgi?title=Title+Goes+Here
or
file.cgi?title=Title%20Goes%20Here

(either will work)

#!/usr/bin/perl

%FORM = &query;
$title = $FORM{'title'}; #This is uneccessary

print "Content-type: text/html\n\n";
print <<EOM;
<TITLE>$title or $FORM{'title'}</TITLE>
<H1>$title or $FORM{'title'}</H1>

CONTENT AREA

EOM
}

sub query{
my $method = $ENV{'REQUEST_METHOD'};
my ($query_string,$pair,%query_hash);

$query_string = $ENV{'QUERY_STRING'} if $method eq 'GET';
$query_string = <STDIN> if $method eq 'POST';
return undef unless $query_string;

foreach $pair (split(/&/, $query_string)) {
($_qsname, $_qsvalue) = split(/=/, $pair);
$_qsname =~ s/[\+]/ /g;
$_qsname =~ s/%([\da-f]{2})/pack("C",hex($1))/ieg;

$_qsvalue =~ s/[\+]/ /g;
$_qsvalue =~ s/%([\da-f]{2})/pack("C",hex($1))/ieg;
$query_hash{$_qsname} = $_qsvalue;
}
return %query_hash;
}

Before anyone flames me, this was a copy/paste from an existing read parse, and I'm aware it crashes if the $ENV{REQUEST_METHOD} is undefined and there's no query string. :-)