Thread Session-ID wird nicht generiert (68 answers)
Opened by anamollo4music at 2013-03-26 08:23

GwenDragon
 2013-03-28 10:17
#166719 #166719
User since
2005-01-17
14748 Artikel
Admin1
[Homepage]
user image
Und nochmals (jetzt wirklich selbst mit Logindaten auf MySQL 5.1 auf Win 7 mit Apache und Perl 5.10.1 getestet):

login:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!perl.exe

use strict;


use IO::Handle; 

use DBI;
use CGI;
use CGI::Session;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

my $dbhost =    "localhost";
my $database =  "sensor";
my $dbuser =    "root";
my $dbpass =    "";

my $dsn = "DBI:mysql:database=$database; host=$dbhost";

my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die DBI::errstr;

my $cgi = new CGI;
my $select;

my $passwort;
my $user;
my $i=0;
my $session;

my $SESSION_EXPIRESAFTER = '+1h'; # nach einer Stunde, notfalls länger 1 W oder 1M 
my $SIDNAME = CGI::Session->name();
my $sid = $cgi->cookie($SIDNAME) || $cgi->param($SIDNAME) || undef;

$session = CGI::Session->load() or die CGI::Session->errstr();
$session->expires($SESSION_EXPIRESAFTER);
if ( $session->is_empty ) 
{
    $session = CGI::Session->new() or die CGI::Session->errstr();
}
if ( $session->is_expired() ) 
{
    print $session->header(),
        $cgi->start_html(),
        $cgi->p("Your session timed out! Refresh the webpage!"),
        $cgi->end_html();
    exit(0);
}
my $cookie = $cgi->cookie( 
        -name => $SIDNAME, -value => $sid, 
        -httponly => 1,
        -expires => $SESSION_EXPIRESAFTER,
);
        
$sid = $session->id;

my $errormessage;

if($cgi->param('login'))
{
        $select = $dbh->prepare("select benutzer,passwort from benutzer WHERE benutzer=? AND passwort=?;");
        $select->execute($cgi->param('user'), $cgi->param('pwd'));        
        
        while(($user, $passwort) = $select->fetchrow_array)
        {
                if(($user eq $cgi->param('user')) && ($passwort eq $cgi->param('pwd')))
                {                                          
                        $i++;
                        $session->param( 'user',$user );
                        $session->param( 'loggedin', 1 );
                        $session->param( 'lastvisit', "" . localtime() );

                        my $next_url = 'intro.cgi';
                        print $session->header
                        (
                                -location => $next_url,
                                -cookie   => $cookie
                        );
                        print "<font face='Arial' size='3' color=#339900><b>Anmeldung erfolgreich! Sie werden weitergeleitet...</b></font>";
                        exit;
                }       
        }
        if(! $i)                                
        {
                $errormessage = "<font face='Arial' size='3' color=#CC3300><b>Anmeldung fehlgeschlagen!</b></font>";
        }
}   

print $session->header( -type => 'text/html'); 

print "<html><head><meta http-equiv='content-type' content='text/html; charset=utf-8' /><link rel='stylesheet' media='screen,projection' type='text/css' href='/css/main.css' /><link rel='stylesheet' media='screen,projection' type='text/css' href='/css/scheme.css' />";
print "</head>";
print "<body>";
print "<form action='/cgi-bin/login.cgi' method='post'><div id='main'>";
print "<div id='footer'> 
        <h1 id='logo'><a>sensor.<span>ct</span></a></h1>
        <hr class='noscreen' />        
        </div> ";

                
print "<div id='navlogin'><ul class='box'><br><li><p><font face='Arial' size='6'><b>Login</b></font></p><br>
<p>$errormessage</p>
<blockquote>Benutzer-ID: <br><input type='text' size='30' name='user'></li></ul><br>
<ul class='box'><li><blockquote>Kennwort: <br><input type='password' size='30' name='pwd'></li></ul>";         
print "<br><ul class='box'><li><blockquote><br><input class='button' type='submit' name='login'  value='Einloggen'></li><br></ul></div> ";
print "</body></html>";

    


Inro:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!perl.exe

use strict;
use DBI;
use CGI;
use CGI::Session;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

my $cgi = CGI->new();

my $session = CGI::Session->load() or die CGI::Session->errstr;

if (not ($session->param('loggedin') and $session->param('loggedin') == 1 ) )
{
        #nicht angemeldet!!!!
                
        # Weiterleiten zum Login
         my $next_url = 'login.cgi';
        print $session->header
                (
                        -location => $next_url
                );
        exit;           
}

# dann ist wohl eingeloggt, also Datenbank abfragen

my $dbhost =    "localhost";
my $database =  "sensor";
my $dbuser =    "root";
my $dbpass =    "";

my $dsn = "DBI:mysql:database=$database; host=$dbhost";

my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die DBI::errstr;

my $Gesamtsystemquery = $dbh->prepare("select wert from malcos_x;");

my $idMessung;
my $Beschreibung;
my $Wert;


print $cgi->header( -type => 'text/html');

print "<body bgcolor=#bfbfbf>";

print '<p><b><font size=6>Messungen:</font></b></p>';
print "<table border=1>\n";

$Gesamtsystemquery->execute;
while(($idMessung, $Beschreibung, $Wert) = $Gesamtsystemquery->fetchrow_array)
{
        print "  <tr>\n";

        print "    <td>System</td>\n";
        if($idMessung eq "0")
        {
                print "<td>aus</td>\n";
        }
        else
        {
                print "<td>ein</td>\n";
        }
        print "    <td></td>\n";
        print " </tr>\n";
}
$Gesamtsystemquery->finish;



$dbh->disconnect;


print "<br /><br />";


Wenn Login fehlschlägt, dann wird es auch angezeigt!

Editiert von GwenDragon: Subthread
Last edited: 2013-03-28 12:26:30 +0100 (CET)

View full thread Session-ID wird nicht generiert