Ah, I think this is an XY Problem. A session existing in no way means an authenticated or valid user. And the fact that a user can supply a cookie named CGISESSID should not allow them to appear as "signed-in."
I don't WANT want one to be created, I just want to check for the existence of one. If it doesn't exist, sendt the user to the login page.
Not really. What you want to do is check if the session is valid. So, long story short, you can do what you are trying to. It will be verbose however and it will not remove any of the code you'll have to do to check that the session is valid so it's probably pointless unless you've got a high traffic site and don't want to hit the DB with new empty sessions all the time.
If you do want to do it this is the flow-
- Use CGI::cookie() to read the cookie named "CGISESSID" or "YOUR_CUSTOM_NAME."
- If it exists and has a reasonable value, try to load it from CGI::Session->load().
- If it's valid, pass/send them along to /home or whatever (there can be many reasons it wouldn't be valid; wrong/new IP, DB says session is too old to still be valid, some other token of your devising doesn't match the last request for that session).
- If it's not valid, send them to the login page.
- If it doesn't exist, send them to the login page.
The thrust being, you have to create a session to validate one so the only benefit of not doing so is avoiding some overhead and if you have a 100% login access site anyway you won't be saving much. :)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.