Честно говоря, я не знаю ни одного места для того, что вы хотите, потому что я выучил это сам. Потому что на самом деле это легко.
Основной принцип, лежащий в основе всего этого, заключается в проверке правильности введенной информации для входа в систему (проверяя ее через BL и DA, если вы знаете, о чем я говорю), а затем сохраняйте произвольную информацию о пользователе в $ _ SESSION переменная типа $_SESSION['name'] = 'guest'
, а затем написать простое утверждение в начале каждой страницы, для которой требуется вход в систему, чтобы проверить содержимое массива $ _SESSION и, если он не установлен, перенаправить на другую страницу и так далее. ..
Это оно! : D
Надеюсь, я смогу ответить на то, что вы искали! ; -]
EDIT:
Вот простая страница входа:
<?php
session_start(); //required if you want to use the $_SESSION array
$username = $_REQUEST['txtUsername']; //storing the value of the "txtUsername" text box in the form sent by client after the form has been submited
$password = $_REQUEST['txtPassword']; //same as $username
if (isset($username) && isset($password))
{
/*
* this section depends on the implementation of your BL and DA layers.
* assume that my BL selects a member by it's username and returns an array
* containing his/her info
*/
require_once('mybl.php'); //assume that my BL tier is implemented in the "mybl.php" file
MyBL $bl = new MyBL();
$queryResult = $bl->selectByUsername($username);
//authenticating user
if ($queryResult['username'] == $username && $queryResult['password'] == $password)
{
//the user has been authenticated and can proceed to other pages available for members only
/*
* i'm storing the user's username in the session so that I could refer to it in other pages
* in case I want to update/modify database tables for this specific user.
*/
$_SESSION['username'] = $username;
//store anything else you want for the current user:
$_SESSION['name'] = $queryResult['name']; //for welcome prompt
header('Location: welcome.php'); //redirecting the user
}
else //in case of wrong username/password
{
$message = "Incorrect username/password";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LoginPage</title>
</head>
<body>
<form method="post" action="login.php">
<table style="border: 1px dashed">
<tr>
<td>Username:</td>
<td><input name="txtUsername" type="text" />
</tr>
<tr>
<td>Password:</td>
<td><input name="txtPassword" type="password" />
</tr>
<tr>
<td colspan="2" align="center"><input name="btnSubmit" type="submit" value="Login" />
</tr>
</table>
</form>
<span style="color: red;"><?php echo $message; ?> </span>
</body>
</html>
Страница приветствия :
<?php
session_start();
//checking to see if the user is logged in
if (!isset($_SESSION['username']))
header('Location: login.php'); //the user is not logged in, he/she is redirected to the login page.
/*
* Basically you might want to put this code at the beginning of every page that requires
* logging in. This way a user that hasn't logged in can't see the contents of the page
* and you don't have to worry about extra checking and conditions that could raise errors
* due to empty "$_SESSION" array.
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login successful</title>
</head>
<body>
Login successfull!<br />
Welcome <?php echo $_SESSION['name']; ?>
</body>
</html>