Hello Perlmonks I have been incorporating a login system into my site, the scripts for the site are perhaps stretching back 15 years and include old code and new so some of it is cobbled together but it works. I have a login script that generates and sets a cookie and includes username, password and login value it also validates the user with the site database so if the user enters the correct data they will get the cookie, if not no cigar. This works correctly and places the cookie on the users machine and my server. The main script controls the rest of the site. My problem is this. I am using the following code at the start of the script

%cookies = CGI::Cookie->fetch; $sessionid = $cookies{'CGISESSID'}->value; $session = CGI::Session->load(undef, $sessionid, {Directory=>''}) + or die CGI:Session->errstr(); $login = $session->param('login'); $logswitch = ($login);

$logswitch is the variable to switch the username and password entry screens on and off (i.e. 1 and 0) With the above code when the user is logged in all works correctly, when the user is not logged in (ie no cookie) I get a 'Can't call method "value" on an undefined value' error. To negate this I have tried this code

if (exists $cookies{name}) { %cookies = CGI::Cookie->fetch; $sessionid = $cookies{'CGISESSID'}->value; $session = CGI::Session->load(undef, $sessionid, {Directory=>''} +) or die CGI::Session->errstr(); $login = $session->param('login'); $logswitch = ($login); }

This works well for a user with no cookie but if the user logs in, the value of $logswitch remains undef and so fails to trigger the click through screens. Any help would be appreciated as I have been on this for days now, I think I've read everything on CPAN and I use strict, warnings, diagnostics and anything else I can lay my hands on in all of my scripts. I may just be brain frazzled at the moment so apologies if it's obvious, just can't see it myself. Thank you in advance Dazz


In reply to Fetching cookies without errors? by Dazz45X

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.