Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Read Cookies stored with Perl

    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.

    Code:
    #!/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() );

  2. #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,215
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  3. #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,687
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    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:
    Code:
    print "var myjsvar = '",$myperlvar,"';\n";
    but not necessary since you can do all this in perl on the server side.

    Edit: if the perl variable is a string, remember to use quote,

    best regards

  4. #4
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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!

  5. #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,687
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by pppebble88 View Post
    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!
    Code:
    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

  6. #6
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,215
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  7. #7
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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...

    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...



    Code:
    $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)?

    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.

  8. #8
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    BTW...I pulled up the cookie that my perl script is creating..

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

    Code:
    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.

  9. #9
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,215
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Quote Originally Posted by pppebble88 View Post

    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •