<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technical Diary &#187; cookies</title>
	<atom:link href="http://andriigrytsenko.net/tag/cookies/feed/" rel="self" type="application/rss+xml" />
	<link>http://andriigrytsenko.net</link>
	<description>With Andrii Grytsenko</description>
	<lastBuildDate>Sat, 28 Jan 2012 12:41:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Perl and cookies auth</title>
		<link>http://andriigrytsenko.net/2010/06/perl-and-cookies-auth/</link>
		<comments>http://andriigrytsenko.net/2010/06/perl-and-cookies-auth/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 12:05:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=794</guid>
		<description><![CDATA[I made little overview how to make http authorization through the PERL. I&#8217;m not gonna describe how to make HTML form and how to parse input data from html. I leave these as you your homework ;). As storage for HTTP sessions I chose database(specifically MYSQL). Here is my table with sessions : desc mc_sessions; [...]]]></description>
			<content:encoded><![CDATA[<p>I made little overview how to make http authorization through the PERL.</p>
<p><span id="more-794"></span></p>
<p>I&#8217;m not gonna describe <a href="http://www.google.com.ua/search?hl=en&amp;source=hp&amp;q=html+form&amp;aq=f&amp;aqi=g10&amp;aql=&amp;oq=&amp;gs_rfai=">how to make HTML form</a> and <a href="http://www.google.com.ua/search?hl=en&amp;q=how+to+parse+input+data+from+html+in+perl&amp;aq=f&amp;aqi=&amp;aql=&amp;oq=&amp;gs_rfai="> how to parse input data from html</a>. I leave these as you your homework ;). As storage for HTTP sessions I chose database(specifically MYSQL).</p>
<p>Here is my table with sessions :</p>
<pre>desc mc_sessions;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| user_name       | varchar(50)  | YES  |     | NULL    |       |
| session_id      | varchar(255) | NO   | PRI | NULL    |       |
| expiration_time | int(11)      | YES  |     | NULL    |       |
+-----------------+--------------+------+-----+---------+-------+</pre>
<pre class="brush: perl; title: ; notranslate">
use CGI qw/:standard/;
use CGI::Cookie;

sub set_cookies {.
    my ($db,$user_name,$user_passwd) = @_;

    my $exp_time = time() + 259200; #(3 days)
    my $session_id = md5_hex($user_id.''.$user_name.''.$user_passwd.''.$exp_time);

    my $sth = $dbh-&amp;gt;prepare(&quot;INSERT INTO $table (user_name,session_id,expiration_time) VALUES (?,?,?)&quot;);
    $sth-&amp;gt;execute($user_name,$session_id,$exp_time);

    my $cookie1 = new CGI::Cookie(-name=&amp;gt;'SessionID',-value=&amp;gt;&quot;$session_id&quot;);
    my $cookie2 = new CGI::Cookie(-name=&amp;gt;'ExpirationTime',-value=&amp;gt;&quot;$exp_time&quot;);

    print header(-cookie=&amp;gt;[$cookie1,$cookie2]);

    return 1;
}

sub check_auth {
    my ($dbh) = @_;

    my %cookies = fetch CGI::Cookie;
    my %cook_hash;

    foreach my $key (sort keys %cookies) {
        $cookies{$key} =~ /=(.*);/;
        $cook_hash{$key} = $1;
    }

    return -1 if (!defined($cook_hash{SessionID}));

    my $user_name = is_session_valid($dbh,$cook_hash{SessionID});

    return -1 if ($user_name == -1);

    return $user_name;
}

sub is_session_valid {
    my ($dbh,$session_id) = @_;

    my ($exp_time);

    my $sth = $dbh-&amp;gt;prepare(&quot;SELECT * FROM sessions WHERE session_id=? and expiration_time &amp;gt; ?&quot;);
    $sth-&amp;gt;execute($session_id,time());

    while ( my @array = $sth-&amp;gt;fetchrow_array ) {
            ($user_name,$session_id,$exp_time) = @array;
    }

    return $user_name if (defined($user_name) and $exp_time &amp;gt; time());

    return -1;
}
</pre>
<p>As result we have 3 functions.</p>
<ol>
<li><span style="color: #000000;"><strong>set_cookies</strong></span><span style="color: #000000;"> &#8211; this function generate, put in database and send cookies to user&#8217;s browser.</span></li>
<li><strong>check_auth</strong> &#8211; check if user has already have some cookies from server if yes call <em>is_session_valid</em> for future checking.</li>
<li><strong>is_session_valid</strong> &#8211; look about user&#8217;s  session id  and check whether this session was expired or not.</li>
</ol>
<p><span style="color: #ff0000;"><strong><span style="text-decoration: underline;">Pay attention:</span></strong></span></p>
<p><span style="color: #ff0000;"><strong></strong></span><span style="color: #ff6600;"><span style="color: #ff0000;">Each function receives variable </span><strong><span style="color: #ff0000;">$dbh</span></strong><span style="color: #ff0000;"> it&#8217;s </span><a href="http://search.cpan.org/~timb/DBI-1.611/DBI.pm#Outline_Usage"><span style="color: #ff0000;">database connection identificator</span></a><span style="color: #ff0000;"> and should be created before function run.</span></span></p>
<h2 style="text-align: center;">How does it use?</h2>
<p>Edit you CGI code. And take care that all pages checks sessions id.<br />
Put this function call after authorization by password to send cookies to user:</p>
<pre class="brush: perl; title: ; notranslate">
set_cookies($dbh,$username,$password);
</pre>
<p>And these in all remains site&#8217;s pages to prevent site from unauthorized actions:</p>
<pre class="brush: perl; title: ; notranslate">
my $user_name = check_auth($dbh);

if ( $user_id == -1 ) {
        some action[s]
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2010/06/perl-and-cookies-auth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

