PDA

View Full Version : Read Cookies stored with Perl



pppebble88
Nov 16th, 2009, 12:51 AM
Hello,

I am trying to retrieve a cookie I stored with Perl through javascript. Based on the contents of the cookie, I want the javascript to print/not print an extra link on my page. Below is the code, in Perl, I am using to store the cookie. In the Perl forum I was told to post in here for the javascript help.

The basic idea is that if the cookie has a "Role" of "Super," the ADMIN link displays on the page. If the role doesn't have "Super," the link is hidden.

Any help you could offer would be greatly appreciated. Thanks a lot.


#!/usr/local/bin/perl


# This page was created and worked on by 2/C Collard, 2/C Waymouth, 2/C Troisi, and 2/C Cunha
#source: http://forums.speedguide.net/showthread.php?t=190821

# PERL MODULES WE WILL BE USING
use DBI;
use DBD::mysql;
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);

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

$userPassed = param("userID");

# CONFIG VARIABLES
$platform = "";
$database = "";
$host = "";
$port = "";
$tablename = "";
$user = "";
$pw = "";

# DATA SOURCE NAME
$dsn = "dbi:$platform:$database:$host:$port";

# PERL DBI CONNECT
$connect = DBI->connect($dsn, $user, $pw)
or die "Connection Error: $DBI::errstr\n";

# PREPARE THE QUERY
my $query = "SELECT * FROM $tablename WHERE User = '$userPassed'";
my $query_handle = $connect->prepare($query);

#print ($query);

# EXECUTE THE QUERY
$query_handle->execute();

while (@row = $query_handle->fetchrow_array) {
$roleIn = "$row[2]";
#print ("$roleIN");
}

$query = new CGI; # create a new CGI object
$cookie = $query->cookie ( -name => 'Role',
-value => '$roleIn',
-path => '/',
-expires => '+60m');

$location = '';

print $query->header(-cookie=>$cookie);
print qq{<meta http-equiv="REFRESH" content="0;URL=http://www.togoto.com">\n};

# HTTP HEADER
#print( header() );
#print ( start_html() );

# Print XHTML footer
#print ( end_html() );

bazz
Nov 16th, 2009, 01:20 AM
well, if you need javascript to do this then it was correct you were sent here but.... why use JS for this when it can all be done with perl/html?

I normally do it with perl and html so it works even when js is disabled.

just query the specific cols you nedd from your db (do not query using * ).
ensure one of the cols queried is the admin_rights and store this in your session as well as the login script. then when you need it get the admin value from the session and with a conditional output, show the link, or not.



bazz

oesxyl
Nov 16th, 2009, 05:43 AM
my %cookies = CGI::Cookie->fetch;

# some code, ...

if(exists($cookies{'role'})){ # add checking value if you need here
# code when cookie exists
}


passing a variable from perl to javascript is very simple:


print "var myjsvar = '",$myperlvar,"';\n";


but not necessary since you can do all this in perl on the server side.

if the perl variable is a string, remember to use quote, :)
best regards

pppebble88
Nov 16th, 2009, 06:29 AM
I am trying to figure out why Javascript is not needed...The pages we want the link to display in are all written in HTML...Since you can't directly insert Perl into an HTML page, how would Javascript not be needed in order to determine what to display?

thanks for all of the help!

oesxyl
Nov 16th, 2009, 06:37 AM
I am trying to figure out why Javascript is not needed...The pages we want the link to display in are all written in HTML...Since you can't directly insert Perl into an HTML page, how would Javascript not be needed in order to determine what to display?

thanks for all of the help!


if(exists($cookies{'role'})){
print qq(<a href="link to logout script">log out</a>);
}else{
print qq(<a href="link to login script">log in</a>);
}

perl and javascript do same thing from this point of view, only print the page( markup or whatever you want). The difference is when they do and where. Perl generate the page on the server and javascript modify the generated page in client browser.

best regards

bazz
Nov 16th, 2009, 07:12 AM
If you generated your session with perl are your pages not built dynamically by perl? if so, then perl can do the job as it creates the dynamic page.
bazz

pppebble88
Nov 16th, 2009, 01:43 PM
No...My pages are not created dynamically by perl...Wouldn't that mean that every page would end with a ".pl" instead of a ".html?"

Also, oesxyl, for your code...


my %cookies = CGI::Cookie->fetch;

# some code, ...

if(exists($cookies{'role'})){ # add checking value if you need here
# code when cookie exists
}

what is the % for? Below is what I have for the cookies...




$query = new CGI; # create a new CGI object
$cookie = $query->cookie ( -name => 'Role',
-value => '$roleIn',
-path => '/',
-expires => '+60m');

$location = '';

print $query->header(-cookie=>$cookie);
print "var myjsvar = '",$cookie,"';\n";
print qq{<meta http-equiv="REFRESH" content="0;URL=http://www.team2.it350.cs.usna.edu/honorBase.html">\n};

If I want to pull out the "Role" of the cookie, can I just reference that part of it (refer to my 2nd block of code)?


<!--
if ( ! get_cookie ( "Role" ) )
{

}
else
{
var role = get_cookie ( "Role" );
document.write("Test");
if( role == "Super")
{
document.write ( "Hi " + username + ", welcome to my website!" );
}
}
// -->

Thanks for all of the help.

pppebble88
Nov 16th, 2009, 03:35 PM
BTW...I pulled up the cookie that my perl script is creating..

The contents of this (from firefox) are below...



Name: Role
Content: %24roleIn
Host: "our website"
Path: /
Send for: Any type of connection
Expires: Monday, November 16, 2009 10:32:14 AM


Why is the VALUE of "Role" not stored in "Content"?

Thanks.

bazz
Nov 16th, 2009, 03:44 PM
Why is the VALUE of "Role" not stored in "Content"?

Thanks.

I don't know the answer to that one but, it may become clear if we were able to see how the cookie values are being stored.

I think you may find it much easier if this page was created in perl or even server-side shtml. If you need to have the html extension for the page you could use mod rewrite.

hth

bazz