Sie sind auf Seite 1von 7

Contohpenerapansession,pernyataaninclude

FormLogin
1. Logintanpadatabase

login1.php:formlogin
<?php
//wemustneverforgettostartthesession
session_start();
$errorMessage='';
if(isset($_POST['txtUserId'])&&isset($_POST['txtPassword'])){
//checkiftheusernameandpasswordcombinationiscorrect
if($_POST['txtUserId']==='admin'&&$_POST['txtPassword']==='udinharun'){
//theusernameandpasswordmatch,
//setthesession
$_SESSION['basic_is_logged_in']=true;

//afterloginwemovetothemainpage
header('Location:main1.php');
exit;
}else{
$errorMessage='Sorry,wrongusername/password';
}
}
?>
<html>
<head>
<metahttpequiv="ContentType"content="text/html;charset=iso88591">
<title>LoginForm</title>
<styletype="text/css">
<!
.style5{
color:#0000FF;
fontweight:bold;

}
>
</style>
</head>
<body>
<?php
if($errorMessage!=''){
?>
<p><divalign="center"><spanclass="style5"><?phpecho$errorMessage;?>
</span></p>
<?php
}
?>

</div>
</div>
<formaction=""method="post"name="frmLogin"id="frmLogin">
<tablewidth="400"border="1"align="center">
<tr>
<thwidth="160"scope="col"><divalign="left">UserId</div></th>
<thwidth="224"scope="col"><divalign="left">
<inputname="txtUserId"type="text"id="txtUserId">
</div></th>
</tr>
<tr>
<td><divalign="left">Password</div></td>
<td><divalign="left">
<inputname="txtPassword"type="password"id="txtPassword">
</div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><inputname="btnLogin"type="submit"id="btnLogin"value="Login"></td>
</tr>
</table>
</form>
</body>
</html>

main1.php
<?php
//likeisaid,wemustneverforgettostartthesession
session_start();
//istheoneaccessingthispageloggedinornot?
if(!isset($_SESSION['basic_is_logged_in'])||$_SESSION['basic_is_logged_in']!==
true){
//notloggedin,movetologinpage
header('Location:login1.php');
exit;
}
?>
<html>
<head>
<title>MainUserPage</title>
<metahttpequiv="ContentType"content="text/html;charset=iso88591">
</head>
<body>
<p>Thisisthemainapplicationpage.Youarefreetoplayaroundheresinceyou
areanautenthicateduser:)</p>
<p>&nbsp;</p>
<p><ahref="logout1.php">Logout</a></p>
</body>
</html>

logout1.php
<?php
//iwillkeepyellingthis
//DON'TFORGETTOSTARTTHESESSION!!!
session_start();
//iftheuserisloggedin,unsetthesession
if(isset($_SESSION['basic_is_logged_in'])){
unset($_SESSION['basic_is_logged_in']);
}
//nowthattheuserisloggedout,
//gotologinpage

header('Location:login1.php');
?>

2. Loginmenggunakandatabase
SQLtabeluser:
CREATETABLE`tbl_auth_user`(
`user_id`varchar(10)NOTNULLdefault'',
`user_password`varchar(32)NOTNULLdefault'',
PRIMARYKEY(`user_id`)
)ENGINE=MyISAMDEFAULTCHARSET=latin1;

login2.php
<?php
//wemustneverforgettostartthesession
session_start();
$errorMessage='';
if(isset($_POST['txtUserId'])&&isset($_POST['txtPassword'])){
include'library/config.php';
include'library/opendb.php';

$userId=$_POST['txtUserId'];
$password=$_POST['txtPassword'];

//checkiftheuseridandpasswordcombinationexistindatabase
$sql="SELECTuser_id
FROMtbl_auth_user
WHEREuser_id='$userId'ANDuser_password=
PASSWORD('$password')";

$result=mysql_query($sql)ordie('Queryfailed.'.mysql_error());

if(mysql_num_rows($result)==1){
//theuseridandpasswordmatch,
//setthesession
$_SESSION['db_is_logged_in']=true;

//afterloginwemovetothemainpage
header('Location:main2.php');
exit;
}else{
$errorMessage='Sorry,wronguserid/password';
}

include'library/closedb.php';
}
?>
<html>
<head>
<title>BasicLogin</title>
<metahttpequiv="ContentType"content="text/html;charset=iso88591">
</head>
<body>
<?php
if($errorMessage!=''){
?>
<palign="center"><strong><fontcolor="#990000"><?phpecho$errorMessage;?
></font></strong></p>
<?php
}
?>
<formaction=""method="post"name="frmLogin"id="frmLogin">
<tablewidth="400"border="1"align="center"cellpadding="2"cellspacing="2">
<tr>
<tdwidth="150">UserId</td>
<td><inputname="txtUserId"type="text"id="txtUserId"></td>
</tr>
<tr>
<tdwidth="150">Password</td>
<td><inputname="txtPassword"type="password"id="txtPassword"></td>
</tr>
<tr>
<tdwidth="150">&nbsp;</td>
<td><inputname="btnLogin"type="submit"id="btnLogin"value="Login"></td>
</tr>
</table>
</form>
</body>
</html>

main2.php
<?php
//likeisaid,wemustneverforgettostartthesession
session_start();
//istheoneaccessingthispageloggedinornot?
if(!isset($_SESSION['basic_is_logged_in'])||$_SESSION['basic_is_logged_in']!==
true){
//notloggedin,movetologinpage
header('Location:login2.php');
exit;
}
?>
<html>
<head>
<title>MainUserPage</title>
<metahttpequiv="ContentType"content="text/html;charset=iso88591">
</head>
<body>
<p>Main2Sukseslogindengandatabase<br>
Thisisthemainapplicationpage.Youarefreetoplayaroundheresinceyou
areanautenthicateduser:)</p>
<p>&nbsp;</p>
<p><ahref="logout2.php">Logout</a></p>
</body>
</html>

Filedidalamdirektori'library:'
config.php

<?php
//Thisisanexampleofconfig.php
$dbhost='localhost';
$dbuser='user';
$dbpass='password';
$dbname='database';
?>
opendb.php
<?php
$conn=mysql_connect($dbhost,$dbuser,$dbpass)ordie('Errorconnectingto
mysql');
mysql_select_db($dbname);
?>
closedb.php
<?php
mysql_close($conn);
?>

Das könnte Ihnen auch gefallen