projekt01/ |--config.php |--login.php |--logout.php |--db.php |--lock.php `--udv.php
<?php include("db.php"); include("config.php"); session_start(); if($_SERVER["REQUEST_METHOD"] == "POST"){ $user = addslashes($_POST['user']); $pass = addslashes($_POST['pass']); $sql = "SELECT user FROM users WHERE user='$user' and pass='$pass'"; $conn = connectDb(); $res = mysql_query($sql); $count = mysql_num_rows($res); if($count==1){ $_SESSION['login_user']=$user; header("Location: udv.php"); }else { echo "A jelszó vagy felhasználónév érvénytelen"; } closeDb($conn); } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title><?php echo $site['title']; ?></title> </head> <body> <meta charset="utf-8"> <form action="" method="post"> <label for="user">Felhasználónév: </label> <input id="user" type="text" name="user"><br> <label for="pass">Jelszó: </label> <input id="pass" type="password" name="pass"><br> <input type="submit" value="Belépés"> </form> </body> </html>
<?php $db['host'] = 'localhost'; $db['user'] = 'sargazrt'; $db['pass'] = 'titok'; $db['name'] = 'sargazrt'; $site['title'] = "Loginteszt"; ?>
<?php session_start(); if(session_destroy()){ header("Location: login.php"); } ?>
A következő metódus ellenőrzi, hogy a munkamenet rendben van-e.
<?php include("db.php"); include("config.php"); session_start(); $user = $_SESSION['login_user']; $conn = connectDb(); $sql = "select user from users where user='$user'"; $res = mysql_query($sql); $row = mysql_fetch_assoc($res); $login_session = $row['user']; closeDb($conn); if(!isset($login_session)){ header("Location: login.php"); } ?>
<?php include("config.php"); function connectDb(){ global $db; $conn = mysql_connect($db['host'], $db['user'], $db['pass']) or die("Hiba a kapcsolat során!"); mysql_select_db($db['name'], $conn) or die("Hia az adatbázis kiválasztása során!"); return $conn; } function closeDb($conn){ mysql_close($conn); } ?>
<?php include("lock.php"); include("config.php"); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title><?php echo $site['title']; ?></title> </head> <body> <h1>Üdv <?php echo $login_session; ?></h1> <p><a href="logout.php">Kijelentkezés</a></p> </body> </html>