Skip to content

Instantly share code, notes, and snippets.

@bg1bgst333
Created April 12, 2025 23:10
Show Gist options
  • Save bg1bgst333/834a91f4a0ce4556fa9c7ee3e1f6880f to your computer and use it in GitHub Desktop.
Save bg1bgst333/834a91f4a0ce4556fa9c7ee3e1f6880f to your computer and use it in GitHub Desktop.
CGI::Session:delete
#!/usr/bin/perl
use strict; # 厳密な文法チェック.
use CGI; # CGI
use CGI::Session; # Session
# CGIオブジェクトの作成.
my $q = CGI->new; # CGI::newでCGIオブジェクトを生成し, $qに格納.
# パラメータにユーザー名がある.
my $username = $q->param('username');
if ($username){
# パスワード取得.
my $password = $q->param('password');
if ($username eq 'taro' && $password eq 'testpass'){
# セッション作成..
my $session = CGI::Session->new(undef, $q); # new(dsn, query)で復元.
# パラメータ設定.
$session->param('username', $username);
# ヘッダーにクッキーをセット.
my $cookie = $q->cookie(CGISESSID => $session->id);
print $q->header(-location => 'main.cgi', -cookie => $cookie);
exit;
}
else{
# 失敗
print $q->header();
print "login failed!";
exit;
}
}
# フォーム表示
print $q->header();
print <<'HTML';
<form method="post">
username: <input type="text" name="username"><br>
password: <input type="password" name="password"><br>
<input type="submit" value="submit">
</form>
HTML
#!/usr/bin/perl
use strict; # 厳密な文法チェック.
use CGI; # CGI
use CGI::Session; # Session
# CGIオブジェクトの作成.
my $q = CGI->new; # CGI::newでCGIオブジェクトを生成し, $qに格納.
# セッションの取得..
my $session = CGI::Session->new(undef, $q); # new(dsn, query)で復元.
if ($session){
# セッションの削除.
$session->delete();
# main.cgiに戻る.
print $q->redirect('main.cgi');
}
else{
# ヘッダ.
print $q->header();
print "no session.".$q->br;
}
#!/usr/bin/perl
use strict; # 厳密な文法チェック.
use CGI; # CGI
use CGI::Session; # Session
# CGIオブジェクトの作成.
my $q = CGI->new; # CGI::newでCGIオブジェクトを生成し, $qに格納.
# セッション復元.
my $sid = $q->cookie('CGISESSID');
my $session = CGI::Session->load(undef, $sid); # loadで復元.
if ($session){
my $msg;
# セッションが空.
if ($session->is_empty){
$msg = " and session empty.";
}
# セッション期限切れ.
if ($session->is_expired){
print $q->redirect('login.cgi');
}
# ユーザー名の取得.
my $username = $session->param('username');
# ヘッダ.
print $q->header();
# セッションIDの確認.
if ($session->id){
print "sid: ".$sid.$q->br;
print "session id: ".$session->id.$q->br;
}
# ユーザーがいる.
if ($username){
print $username.$q->br;
print $q->a({-href => 'logout.cgi'}, 'logout');
}
else{
print "no login".$msg.$q->br;
}
}
else{
# ヘッダ.
print $q->header();
print "no session.".$q->br;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment