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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating a cookie from website to access a secure website 2

Status
Not open for further replies.

uatcisch

Technical User
Feb 9, 2009
5
US
Hello. I'm new here and I had just began using Perl CGI.
I'm trying to have a link from my website to another website which requires a cookie to be created in advance. I've been trying to do this with the minimum steps necessary. So far, I've been unsuccessful using the code below. The cookie is printed instead as text on my website while the redirect returns a status 302 message.

Is there a way of doing this without creating a 2nd perl script that does the printing of the cookie header and the link to the 2nd website?

I would appreciate any help or suggestions. Below is the code.

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

use CGI qw:)standard :netscape :html3 :cgi-lib :shortcuts :br :p );

use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $cgi = new CGI;

use strict;

#the html page
print "Content-type:text/html\n\n";
print "<html><h2>Cookie for Website2</h2><ul>";
print "<li>Test Creating Cookie: </ul>";

my $website2 = ' #for testing only. Not a real website

my $cookie = cookie(
-name => 'cookie1',
-value => "key1=key1:adt=adt1:locn=locn1",
-expires => '+240s',
-domain => '.xyzcompany.com',
-path => '/',
-secure => 1
);

print header(-target=>'_blank', -cookie => $cookie);

print br, center(font({-size=>'+1'}, a({-target=>'_blank',-href=>$website2}, "Link to 2nd Website")));

#print redirect (-uri=>"$website2", -cookie=>"$cookie\n\n");

print "</html>";
----------------------------------
 
remove this line:

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

and retry your code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin, it worked!

Now, if I may ask, what would be the equivalent of disabling the statement 'print "Content-type:text/html\n\n";' when the program structure looks like this:

my $query = new CGI;

print $query->header;
print $query->start_html (<title, etc>);
print $query->start_form();

<some code>
print $query->header(-target=>'_blank', -cookie => $cookie);

print br, center(font({-size=>'+1'}, a({-target=>'_blank',-href=>$website2}, "Link to 2nd Website")));

<some code>

print end_html;
#-------------------------

This is the structure in a more complicated program where I'll be doing the same thing - create a cookie and link it to a website.
 
You can't print a complete header before setting the cookie. And you can't print any output before printing a complete header, so all three of these lines would have to go:

print $query->header;
print $query->start_html (<title, etc>);
print $query->start_form();

Once you print a header you can't print another one like it appears your code does later on.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You're right.

Now, I got a follow up question. I don't know if this should be on another thread about changing the values of a cookie.

The problem is the values for the cookie only become available near the end of the program. So, I can't delete the print header and the 2 other lines of code since they do some other necessary processing.

Instead, I created a cookie with default values when the header is being printed then tried to change the values of the cookie before the statement:

print br, center(font({-size=>'+1'}, a({-target=>'_blank',-href=>$website2}, "Link to 2nd Website")));

But the cookie values were not changed.

Is it still possible to change the values in the cookie after it's printed out to the header and before accessing the other website?
 
Instead of printing everything, add everything to a buffer. Like

Code:
my $preprint;

# instead of
print "something";

# do
$preprint .= "something";

#...

print header(...);
print $preprint;

Once you send a "\n\n", the headers are done and you can't send more headers again. (the CGI header() method prints a "\n\n" after it sends the headers). You'll need to restructure your code's logic so that before anything is printed at all, your code will have the info it needs for the cookie and then print the cookie as it prints the rest of the headers.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Is it still possible to change the values in the cookie after it's printed out to the header and before accessing the other website?

The short answer is no. You need to change how your script works. Kirsle has made a suggestion about building up your output then after the cookie stuff is finished you print whatever needs printing. I agree with that suggestion. Or you could take a look at your script and make sure you really need to set the cookie later instead of sooner.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Building up the output, create a cookie, then doing the print stuff worked like magic on the less complicated program.

However, on the other program, where the cookie have to be done at a later time after some print statements, I think I'll try again the redirect statement. This time without the 'print $cgi->header(-cookie=>$cookie);' statement, which I think was causing the Status: 302 message.
 
Thank you all for your efforts.

I just ran into another problem using the print redirect statement with the larger, more complex program. I'll keep looking in the other threads for a solution.

The redirection is not working and the cookie is not being set. The Status 302 message is written on the screen:

-----------------------------------------------------------
Status: 302 Moved Set-Cookie: cookie1=<cookie values, etc>.
-----------------------------------------------------------

I've tried both statements below but to no avail:

------------------------------------------
print redirect (-uri=>"$website2", -cookie=>"$cookie\n\n");

print $cgi->redirect (-uri=>"$website2", -cookie=>"$cookie\n\n");
----------------------------------------------
 
Keep in mind, a cookie set on one website can't be read from another website if thats what you are trying.

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

Part and Inventory Search

Sponsor

Back
Top