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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Can you change an actual COOKIE in Perl?

    I am trying to change the actual value of a cookie based on a set of conditionals. For example, after my user authenticates , I am connecting to a mySQL database to capture the role of the user and then store that value in a variable. However, in order for that "role" to be updated on the other pages, I have to modify the ACTUAL cookie...Below is the code I am trying to use to do this...Any help you could offer would be appreciated...Thanks.

    Code that captures the stored cookie and sets the variables appropriately...
    Code:
    @theCookie = cookie('honorCookie');
    
    if(@theCookie) {
    	$role = substr(@theCookie[0],5);
    	$userID = substr(@theCookie[1],7);
    	$loggedIn = 1;		
    
    }
    else  {
    	print qq{<meta http-equiv="REFRESH" content="0;URL=https://WEB SITE">\n};
    }
    Code to modify the cookie
    Code:
    ...previous code omitted...
    	# PREPARE THE QUERY
    	my $query = "SELECT * FROM $tablename WHERE User = '$userID'";
    	my $query_handle = $connect->prepare($query);
    	
    	# EXECUTE THE QUERY
    	$query_handle->execute();
    	
    	while (@row = $query_handle->fetchrow_array) {
    		$roleIn = "$row[2]";
    		$to_set = cookie(-name => "role",
    						 -value => "$roleIn",
             );
            @theCookie = cookie('honorCookie');
            print ("$to_set");
    		@theCookie[0] = $roleIn;		
    		}

  • #2
    Banned
    Join Date
    Jun 2007
    Location
    Web Designer
    Posts
    321
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Do not you need to use -cookie to set the cookie ?

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I set the cookie in an ASP page...I currently use

    Code:
    $name = cookie("username");
    to fetch the cookie...

    If i try to use

    Code:
    $theCookie = $cgi->cookie('honorCookie');
    to fetch the cookie, I get the following error...

    Can't call method "cookie" on an undefined value at D:\InetPub\PERL LOCATION line 16.
    I am not sure what to do here...Thanks.

  • #4
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    DISREGARD PREVIOUS POST

    Here is where I stand...Everything works properly, except that for some reason, at the top of my page, it displays the following:

    Set-Cookie: role=Super; path=/ Date: Thu, 19 Nov 2009 06:35:10 GMT Content-Type: text/html; charset=ISO-8859-1
    The code that does all of this is below:

    Code:
    use DBI;
    use DBD::mysql;
    use CGI qw( :standard );
    use CGI::Carp qw(fatalsToBrowser);
    print (header);
    
    $userCookie = cookie('user');
    #print "$userCookie";
    
    if($userCookie) {
    	$userID = $userCookie;
    	$loggedIn = 1;
    }
    else  {
    	print qq{<meta http-equiv="REFRESH" content="0;URL=SERVER">\n};
    }
    
    if($loggedIn) {
    	# CONFIG VARIABLES
    	$platform = "";
    	$database = "";
    	$host = "";
    	$port = "";
    	$tablename = "";
    	$user = "i";
    	$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 = '$userID'";
    	my $query_handle = $connect->prepare($query);
    	
    	# EXECUTE THE QUERY
    	$query_handle->execute();
    	
    	while (@row = $query_handle->fetchrow_array) {
    		$roleIn = "$row[2]";
    		$roleCookie = cookie(-name => "role",
    		 -value => "$roleIn",
             -path => "/", 
    			);		
    		}
    
    }
    
    # HTTP HEADER
    print (header -cookie => $roleCookie);
    THE CATCH--> I realize I have "print (header)" in two places. However, if I don't have it at the top, it doesn't receive the value of the "$userCookie" and the rest of the validation doesn't work...I am at a loss as to how I should re-arrange the "print(header) / print(header -cookie => $roleCookie)'" lines...Thanks for the help.
    Last edited by pppebble88; 11-19-2009 at 07:39 AM. Reason: Oops...

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    The CGI::header function does not deal with receiving cookie info, it sends the cookie to the client.

    Your first print header() function sends the HTTP header without the cookie info. All print statements (to STDOUT) after that header is sent will be seen as the HTML content, which is why you're seeing the cookie data in the browser.

    Remove the first print header statement and add these pragmas.

    Code:
    use strict;
    use warnings;
    The strict pragma will force you to declare your vars as lexical vars instead of globals with the my keyword.

    example:
    Code:
    my $userCookie = cookie('user');
    Once you fix those issues, we can move on to troubleshoot the cookie retrieval.

    FYI, It would be better to use server side sessions instead of the cookies.

  • #6
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Fixed Code

    We added use strict to the header, and we put the my declaration in fron t of variables as needed by use strict.

    The new code still prints the cookie Set header on the page instead of chaning the cookie (same problem)

    Could you please advise what we need to change in order to get this cookie set properly.



    Code:
    #!/usr/bin/perl
    
    
    #source: http://forums.speedguide.net/showthread.php?t=190821
    #source: http://articles.techrepublic.com.com...1-1045105.html
    #source: http://www.gossland.com/course/cgi_use/cookies.html
    
    use DBI;
    use DBD::mysql;
    use CGI qw( :standard );
    use CGI::Carp qw(fatalsToBrowser);
    use CGI;
    use strict;
    use warnings;
    
    print (header);
    
    my $userCookie =cookie('user');
    print "$userCookie";
    
    if($userCookie) {
    	my $userID = $userCookie;
    	my $loggedIn = 1;
    }
    else  {
    	print qq{<meta http-equiv="REFRESH" content="0;URL=https://ADDRESS">\n};
    }
    
    if($loggedIn) {
     	#CONFIG VARIABLES
    	my $platform = "";
    	my $database = "i";
    	my $host = "";
    	my $tablename = "";
    	my $user = "";
    	my $pw = "";
    
    	 #DATA SOURCE NAME
    	my $dsn = "dbi:$platform:$database:$host:$port";
    	
    	 #PERL DBI CONNECT
    	my $connect = DBI->connect($dsn, $user, $pw)
    	or die "Connection Error: $DBI::errstr\n";
    	
    	 #PREPARE THE QUERY
    	my $query = "SELECT * FROM $tablename WHERE User = '$userID'";
    	my $query_handle = $connect->prepare($query);
    	
    	 #EXECUTE THE QUERY
    	$query_handle->execute();
    	
    	while (@row = $query_handle->fetchrow_array) {
    		my $roleIn = "$row[2]";
    		my $roleCookie = cookie(-name => "role",
    	 					 -value => "$roleIn",
            					 -path => "/", 
    		);		
    		}
    
    }
    
    # HTTP HEADER
    print (header -cookie => $roleCookie);
    Last edited by pppebble88; 11-20-2009 at 08:30 PM.

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    I added the code tags for you, but in the future please use the code tags when posting your code.

    Remove this line:
    Code:
    print (header);
    And either remove this print statement or put it after sending the header.
    Code:
    print "$userCookie";

  • #8
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    FishMonger,

    Thanks for the help. I just got PRK eye surgey, so I am having a rough time looking @ a computer screen. I removed the line you just mentioned...Below is the code as it stands and what the browser is telling us. THANKS FOR ALL OF THE HELP SO FAR!

    Code:
    #!/usr/bin/perl
    
    
    #source: http://forums.speedguide.net/showthread.php?t=190821
    #source: http://articles.techrepublic.com.com/5100-10878_11-1045105.html
    #source: http://www.gossland.com/course/cgi_use/cookies.html
    
    use DBI;
    use DBD::mysql;
    use CGI qw( :standard );
    use CGI::Carp qw(fatalsToBrowser);
    use CGI;
    use strict;
    use warnings;
    
    my $userCookie =cookie('user');
    print "$userCookie";
    
    if($userCookie) {
    	my $userID = $userCookie;
    	my $loggedIn = 1;
    }
    else  {
    	print qq{<meta http-equiv="REFRESH" content="0;URL=https://address">\n};
    }
    
    if($loggedIn) {
     	#CONFIG VARIABLES
    	my $platform = "";
    	my $database = "";
    	my $host = "";
    	my $tablename = "";
    	my $user = "";
    	my $pw = "";
    
    	 #DATA SOURCE NAME
    	my $dsn = "dbi:$platform:$database:$host:$port";
    	
    	 #PERL DBI CONNECT
    	my $connect = DBI->connect($dsn, $user, $pw)
    	or die "Connection Error: $DBI::errstr\n";
    	
    	 #PREPARE THE QUERY
    	my $query = "SELECT * FROM $tablename WHERE User = '$userID'";
    	my $query_handle = $connect->prepare($query);
    	
    	 #EXECUTE THE QUERY
    	$query_handle->execute();
    	
    	while (@row = $query_handle->fetchrow_array) {
    		my $roleIn = "$row[2]";
    		my $roleCookie = cookie(-name => "role",
    	 					 -value => "$roleIn",
            					 -path => "/", 
    		);		
    		}
    
    }
    
    # HTTP HEADER
    print (header -cookie => $roleCookie);
    Error:
    Software error:

    Global symbol "$loggedIn" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 28.
    Global symbol "$port" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 38.
    Global symbol "$userID" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 45.
    Global symbol "@row" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 51.
    Global symbol "@row" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 52.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 62.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 134.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 148.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 149.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl line 151.
    Execution of D:\InetPub\it350\www.team2.it350.cs.usna.edu\honorBase.pl aborted due to compilation errors.

  • #9
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    The my keyword creates a lexical var that is limited to the scope in which it was declared. Which means that
    Code:
    my $roleCookie = ...
    is local to the while loop and therefore won't be accessible outside of that block.

    You need to declare $roleCookie in the main body of the script to give it file scope and then assign its value in the while loop.

  • #10
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    Try this (it will probably need a little more tweaking).
    Code:
    #!/usr/bin/perl
    
    use DBI;
    use DBD::mysql;
    use CGI qw( :standard );
    use CGI::Carp qw(fatalsToBrowser);
    use CGI;
    use strict;
    use warnings;
    
    my $userCookie = cookie('user');
    my $loggedIn = 1 if $userCookie;
    
    if( not $loggedIn ) {
        print redirect("https://some_url_address");
        exit;
    }
    
    
    #CONFIG VARIABLES
    my $platform = "";
    my $database = "";
    my $host = "";
    my $tablename = "";
    my $user = "";
    my $pw = "";
    
     #DATA SOURCE NAME
    my $dsn = "dbi:$platform:$database:$host:$port";
    
     #PERL DBI CONNECT
    my $connect = DBI->connect($dsn, $user, $pw)
    or die "Connection Error: $DBI::errstr\n";
    
     #PREPARE THE QUERY
    my $query = "SELECT * FROM $tablename WHERE User = '$userID'";
    my $query_handle = $connect->prepare($query);
    
     #EXECUTE THE QUERY
    $query_handle->execute();
    
    while (@row = $query_handle->fetchrow_array) {
    	my $roleIn = "$row[2]";
    	my $roleCookie = cookie(-name => "role",
    					 -value => "$roleIn",
    					 -path => "/", 
    	);
    }
    
    # HTTP HEADER
    print (header -cookie => $roleCookie);

  • #11
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Fish Monger,

    Thanks for all of the help thus far. I put the code you posted in our file and for some reason am still getting the "explicit file" errors. Below is the code as it stands and the errors as we are receiving them...Again, thanks for all of the help with this...I feel like it must be really close...BTW, i Like the logic you used with the "if(not logged in)" then do this, instead of the other way around...


    Also, after the MAIN code section, we havehave a few PERL code lines that I will post BELOW the main section of code...You will see what i mean below...

    Code at the TOP of honorBase.pl

    Code:
    #!/usr/bin/perl
    
    
    #source: http://forums.speedguide.net/showthread.php?t=190821
    #source: http://articles.techrepublic.com.com/5100-10878_11-1045105.html
    #source: http://www.gossland.com/course/cgi_use/cookies.html
    #source: http://codingforums.com/showthread.php?p=889840#post889840
    
    
    use DBI;
    use DBD::mysql;
    use CGI qw( :standard );
    use CGI::Carp qw(fatalsToBrowser);
    use CGI;
    use strict;
    use warnings;
    
    
    
    my $userCookie = cookie('user');
    my $loggedIn = 1 if $userCookie;
    
    if( not $loggedIn ) {
        print redirect("https://some_url_address");
        exit;
    }
    
    #CONFIG VARIABLES
    my $platform = "";
    my $database = "";
    my $host = "";
    my $tablename = "";
    my $user = "";
    my $pw = "";
    
     #DATA SOURCE NAME
    my $dsn = "dbi:$platform:$database:$host:$port";
    
     #PERL DBI CONNECT
    my $connect = DBI->connect($dsn, $user, $pw)
    or die "Connection Error: $DBI::errstr\n";
    
     #PREPARE THE QUERY
    my $query = "SELECT * FROM $tablename WHERE User = '$userID'";
    my $query_handle = $connect->prepare($query);
    
     #EXECUTE THE QUERY
    $query_handle->execute();
    
    while (@row = $query_handle->fetchrow_array) {
    	my $roleIn = "$row[2]";
    	my $roleCookie = cookie(-name => "role",
    					 -value => "$roleIn",
    					 -path => "/", 
    	);
    }
    
    # HTTP HEADER
    print (header -cookie => $roleCookie);
    Code lower in the file

    Code:
      if(substr($roleCookie,7) eq "Super")  {
    	 print ":: <a href='https://ADDRESS'>Insert XYZ Case</a>"; 
     }
    
    ***LOWER IN FILE ***
    
    	  if($roleCookie eq "Super")  {
    	 	print "<font color='red'>Logged in as $userCookie $roleCookie</font>"; 
     		}
    		else{print "<font color='red'>Logged in as $userCookie $roleCookie</font>"; }
    ERRORS:
    Software error:

    Global symbol "$port" requires explicit package name at D:\InetPub\it350\[url]/url] line 37.
    Global symbol "$userID" requires explicit package name at D:\InetPub\it350\ line 44.
    Global symbol "@row" requires explicit package name at D:\InetPub\it350\ line 50.
    Global symbol "@row" requires explicit package name at D:\InetPub\it350\ line 51.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\ line 59.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\ line 131.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\ line 145.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\ line 146.
    Global symbol "$roleCookie" requires explicit package name at D:\InetPub\it350\ line 148.
    Execution of honorBase.pl aborted due to compilation errors.

    For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.
    THANKS!
    Last edited by pppebble88; 11-21-2009 at 05:14 AM.

  • #12
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    Did you read post #9 where I explained why you're getting those errors?

  • #13
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    FishMonger,

    Making progress...I went back and added initializations for the variables that I was getting errors on. The page displays properly, but it still does not create the "role" cookie on the user's computer...The code is below...I tried to print the "query" and the "roleIn" variables, and neither of them print...Thanks for all of the help.

    Code:
    #!/usr/bin/perl
    
    
    #source: http://forums.speedguide.net/showthread.php?t=190821
    #source: http://articles.techrepublic.com.com/5100-10878_11-1045105.html
    #source: http://www.gossland.com/course/cgi_use/cookies.html
    
    
    use DBI;
    use DBD::mysql;
    use CGI qw( :standard );
    use CGI::Carp qw(fatalsToBrowser);
    use CGI;
    use strict;
    use warnings;
    
    
    my $userCookie = cookie('user');
    my $loggedIn = 1 if $userCookie;
    my @row = "";
    my $roleCookie = "";
    
    if( not $loggedIn ) {
        print redirect("ADDRESS");
        exit;
    }
    
    #CONFIG VARIABLES
    my $platform = "";
    my $database = "";
    my $port = "";
    my $host = "";
    my $tablename = "";
    my $user = "";
    my $pw = "";
    
     #DATA SOURCE NAME
    my $dsn = "dbi:$platform:$database:$host:$port";
    
     #PERL DBI CONNECT
    my $connect = DBI->connect($dsn, $user, $pw)
    or die "Connection Error: $DBI::errstr\n";
    
     #PREPARE THE QUERY
    my $query = "SELECT * FROM $tablename WHERE User = '$userCookie'";
    my $query_handle = $connect->prepare($query);
    print ("$query");
    
     #EXECUTE THE QUERY
    $query_handle->execute();
    
    while (@row = $query_handle->fetchrow_array) {
    	my $roleIn = "$row[2]";
    	print ("$roleIn");
    	my $roleCookie = cookie(-name => "role",
    					 -value => "$roleIn",
    					 -path => "/", 
    	);
    }
    
    # HTTP HEADER
    print (header -cookie => $roleCookie);

  • #14
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    Thatr code should have given you an internal server error. If it didn't, then the script you tested is not the same as the one you posted.

    The first output sent needs to be the HTTP header, but you have that as the last output.

  • #15
    New Coder
    Join Date
    Oct 2009
    Posts
    41
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Fish Monger,

    I just tried to move the "print header" to the very top line (after declaring the packages used) and it gives me an error...Is that the line you are talking about? I am not sure why I am not getting an internal server error...The exact code I posted is the one I am using...Why should I get an internal server error? Can you be more explicit on which print line I should change? Thanks.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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