Month: October 2010

Perl CGI – Session and Cookie howto

Perl CGI – Session and Cookie howto

Introduction –

Almost  2 year back (today also!)  I struggled a lot for implementing session and cookie in Perl CGI application.  So thought to share my work with you all. I wanted to do it in my way…

Assumption, your web server ie Apache is enabled to run CGI scripts

CGI directory location – /var/application/www/cgi-bin/
Htdocs location – /var/application/www/
Perl Module direcotry – /var/application/module/

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie("APP_SID") || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => '../arun.html');}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => '../arun.html');}
else { print $ref_page->header();}
# don't forget to create dir '/var/tmp'
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>'/var/tmp'});
#################################################
return($session->param('login_user'));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.  In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, '/var/application/module/');
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq "POST" )
{
my %form;
my $session_dir="/var/tmp";
 my ($admin_user,$admin_password) = ("admin","arun123");

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name("APP_SID");
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
 ## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>'+2h',-path=>'/');
## Store data in session variable and save it
$session->param('login_user',$form{username}); # OR
##$session->param(-name=>'login_user',-value=>$form{username});
$session->save_param($page, ["login_user"]);

## Session and Cookie expiration time is SAME.
$session->expire("+2h");
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => 'main.pl',-cookie=>$cookie);}
else { print $page->redirect(-location => '../arun.html'); }
############################
} else { print $page->redirect(-location => '../arun.html'); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!-- Form start -->
<table align='center' border='1'>
<form method="POST" action="cgi-bin/auth_me.pl">
<tr>
<td><label>Login</label></td>
<td><input name="username" type="text"></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name="password" type="password"><br/></td>
</tr>
<tr>
<td><input value="Submit" type="submit"></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, '/var/application/module/');
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>'Arun Main Page');

print "<h3>This is Main Page</h3></br>";
print "<br>Login Name - $login_name";

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun

بيرل المجموعة الاستشارية لاندونيسيا — الدورة وكوميديا كوكي

بيرل المجموعة الاستشارية لاندونيسيا — الدورة وكوميديا كوكي

– مقدمة

تقريبا 1 سنة إلى الوراء ، أنا ناضلت كثيرا من أجل تنفيذ الدورة وكوكي في تطبيق مبادرة كلينتون بيرل. حتى ظننت لتقاسم العمل معكم جميعا.
أردت أن تفعل ذلك في طريقي…

الافتراض ، يتم تمكين الخاص أي ملقم الويب اباتشي لتشغيل البرامج النصية المجموعة الاستشارية لاندونيسياI

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul

Perl CGI – Session และ Cookie howto

Perl CGI – Session และ Cookie howto

บทนำ –

เกือบ 1 ปีหลังผมพยายามมากสำหรับช่วงการดำเนินการและคุกกี้ในโปรแกรม Perl CGI ดังนั้นคิดว่าจะร่วมงานของผมกับคุณทั้งหมด
ฉันอยากจะทำในทางของฉัน …

อัสสัมชั, Apache เว็บเซิร์ฟเวอร์ของคุณตัวอย่างเช่นการเปิดใช้งานเพื่อเรียกใช้สคริปต์ CGI

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul

Perl CGI – Session e Cookie howto

Perl CGI – Session e Cookie howto

Introdução –

Quase um ano atrás, eu me esforcei muito para a execução da sessão e cookie em Perl aplicação CGI. Assim, o pensamento de compartilhar meu trabalho com todos vocês.
Eu queria fazê-lo no meu caminho …

Assunção, ou seja, o seu web apache servidor está habilitado para executar scripts CGI

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul

PERL CGI – 세션과 쿠키 하우투

PERL CGI – 세션과 쿠키 하우투

소개 –

거의 일년 전에, 나는 펄 CGI 응용 프로그램에서 세션 및 쿠키를 구현하기위한 많은 싸워. 그러니까 당신과 내 작품을 공유하는 생각.
난 내 방식으로 그것을 원한다면 …

가정은, 귀하의 웹 서버 예 : 아파치는 CGI 스크립트를 실행할 수있게됩니다

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul

Perl CGI – Session dan Cookie Howto

Perl CGI – Session dan Cookie Howto

Pendahuluan –

Hampir 1 tahun kembali, aku berjuang banyak untuk melaksanakan sesi dan cookie dalam aplikasi CGI Perl. Jadi berpikir untuk berbagi karya saya dengan anda semua.
Aku ingin melakukannya dengan cara saya …

Asumsi, web server apache yaitu diaktifkan untuk menjalankan script CGI

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul

Perl CGI – período de sesiones y cookies howto

Perl CGI – período de sesiones y cookies howto

Introducción –

Casi un año atrás, me esforcé mucho para la aplicación de sesión y cookies en Perl aplicación CGI. Así que cree que compartir mi trabajo con todos ustedes.
Yo quería hacerlo a mi manera …

Asunción, el servidor web Apache es decir, está habilitado para ejecutar scripts CGI

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul

Perl CGI – сессии и Cookie Howto

Perl CGI – сессии и Cookie Howto

Введение –

Почти 1 год назад, я боролся много для реализации сессии и куки в применении Perl CGI. Так думал поделиться моей работы со всеми вами.
Я хотел сделать это на моем пути …

Успенский, веб-сервер Apache т.е. включен для запуска CGI-сценарии

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul

PerlのCGI – セッションとCookieの手引き

PerlのCGI – セッションとCookieの手引き

はじめに –

ほぼ1年前、私はPerlのCGIアプリケーションでセッションとCookieを実装するための多くの苦労した。だから、すべてあなたと私の仕事を共有すると考えられている。
私は自分の方法でそれをやってみたかった…

仮定は、Webサーバー、つまりApacheがCGIスクリプトを実行するために有効になっている

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul

Perl CGI – Sessione e howto Cookie

Perl CGI – Sessione e howto Cookie

Introduzione –

Quasi 1 anno fa, ho lottato molto per l’attuazione di sessione e cookie in applicazione Perl CGI. Così pensò di condividere il mio lavoro con tutti voi.
Ho voluto farlo a modo mio …

Assunta, il server Web Apache cioè è in grado di eseguire script CGI

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie(“APP_SID”) || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => ‘../arun.html’);}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => ‘../arun.html’);}
else { print $ref_page->header();}
# don’t forget to create dir ‘/var/tmp’
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>’/var/tmp’});
#################################################
return($session->param(‘login_user’));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.
In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
my $session_dir=”/var/tmp”;
my ($admin_user,$admin_password) = (“admin”,”arun123″);

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name(“APP_SID”);
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>’+2h’,-path=>’/’);
## Store data in session variable and save it
$session->param(‘login_user’,$form{username}); # OR
##$session->param(-name=>’login_user’,-value=>$form{username});
$session->save_param($page, [“login_user”]);

## Session and Cookie expiration time is SAME.
$session->expire(“+2h”);
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => ‘main.pl’,-cookie=>$cookie);}
else { print $page->redirect(-location => ‘../arun.html’); }
############################
} else { print $page->redirect(-location => ‘../arun.html’); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!– Form start –>
<table align=’center’ border=’1′>
<form method=”POST” action=”cgi-bin/auth_me.pl”>
<tr>
<td><label>Login</label></td>
<td><input name=”username” type=”text”></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name=”password” type=”password”><br/></td>
</tr>
<tr>
<td><input value=”Submit” type=”submit”></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, ‘/var/application/module/’);
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>’Arun Main Page’);

print “<h3>This is Main Page</h3></br>”;
print “<br>Login Name – $login_name”;

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun Bagul