Я бы сделал так:
CODE:
<?php
require('auth.php');
?>
Секретная информация, которую должен видеть только зарегистрированный пользователь.
require('auth.php');
?>
Секретная информация, которую должен видеть только зарегистрированный пользователь.
Файл auth.php:
CODE:
Файл config.php:<?php
function createPwd($Size) //Создание случайного пароля
{
$pwd='';
$symbols='abcdefghijklmnopqrstuvwxyz0123456789'; //Символы
$length=strlen($symbols);
while ($Size--) //$Size символов
$pwd.=$symbols{mt_rand(0, $length-1)};
return $pwd;
}
function showForm($Text = NULL)
{
if ($Text)
echo $Text;
?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Логин: <input type="text" name="login"><br>
Пароль: <input type="password" name="pwd"><br>
<input type="submit" value="Войти">
</form><?php
die('');
}
require_once('config.php');
mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD);
mysql_select_db(MYSQL_DB);
mt_srand((double)microtime()*1000000);
$table = MYSQL_TABLE;
$ip = addSlashes(substr((isSet($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']), 0, 255));
$browser = addSlashes(substr($_SERVER['HTTP_USER_AGENT'], 0, 255));
mysql_query("UPDATE `$table` SET `temp` = NULL WHERE ADDDATE(`date`, INTERVAL 10 MINUTE) < NOW()");
if (isSet($_POST['login']) && isSet($_POST['pwd']))
{
$result = mysql_query("SELECT `id` FROM `$table` WHERE `login` = '" . addSlashes($_POST['login']) . "' AND `pwd` = '" . md5($_POST['pwd']) . "' LIMIT 1");
if (mysql_num_rows($result) == 1)
{
$id = mysql_fetch_row($result);
$id = $id[0];
$temp = createPwd(32);
mysql_query("UPDATE `$table` SET `temp` = '$temp', `ip` = '$ip', `browser` = '$browser', `date` = NOW() WHERE `id` = $id LIMIT 1");
setCookie('id', $id);
setCookie('pwd', $temp);
}
else
showForm('Неправильное имя пользователя или пароль!');
}
elseif (isSet($_COOKIE['id']) && isSet($_COOKIE['pwd']))
{
$id = (int)$_COOKIE['id'];
$temp = addSlashes($_COOKIE['pwd']);
$result = mysql_query("SELECT `id` FROM `$table` WHERE `id` = $id AND `temp` = '$temp' AND `browser` = '$browser' AND `ip` = '$ip' LIMIT 1");
if (mysql_num_rows($result) == 1)
mysql_query("UPDATE `$table` SET `date` = NOW() WHERE `id` = $id");
else
showForm();
}
else
showForm();
?>
function createPwd($Size) //Создание случайного пароля
{
$pwd='';
$symbols='abcdefghijklmnopqrstuvwxyz0123456789'; //Символы
$length=strlen($symbols);
while ($Size--) //$Size символов
$pwd.=$symbols{mt_rand(0, $length-1)};
return $pwd;
}
function showForm($Text = NULL)
{
if ($Text)
echo $Text;
?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Логин: <input type="text" name="login"><br>
Пароль: <input type="password" name="pwd"><br>
<input type="submit" value="Войти">
</form><?php
die('');
}
require_once('config.php');
mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD);
mysql_select_db(MYSQL_DB);
mt_srand((double)microtime()*1000000);
$table = MYSQL_TABLE;
$ip = addSlashes(substr((isSet($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']), 0, 255));
$browser = addSlashes(substr($_SERVER['HTTP_USER_AGENT'], 0, 255));
mysql_query("UPDATE `$table` SET `temp` = NULL WHERE ADDDATE(`date`, INTERVAL 10 MINUTE) < NOW()");
if (isSet($_POST['login']) && isSet($_POST['pwd']))
{
$result = mysql_query("SELECT `id` FROM `$table` WHERE `login` = '" . addSlashes($_POST['login']) . "' AND `pwd` = '" . md5($_POST['pwd']) . "' LIMIT 1");
if (mysql_num_rows($result) == 1)
{
$id = mysql_fetch_row($result);
$id = $id[0];
$temp = createPwd(32);
mysql_query("UPDATE `$table` SET `temp` = '$temp', `ip` = '$ip', `browser` = '$browser', `date` = NOW() WHERE `id` = $id LIMIT 1");
setCookie('id', $id);
setCookie('pwd', $temp);
}
else
showForm('Неправильное имя пользователя или пароль!');
}
elseif (isSet($_COOKIE['id']) && isSet($_COOKIE['pwd']))
{
$id = (int)$_COOKIE['id'];
$temp = addSlashes($_COOKIE['pwd']);
$result = mysql_query("SELECT `id` FROM `$table` WHERE `id` = $id AND `temp` = '$temp' AND `browser` = '$browser' AND `ip` = '$ip' LIMIT 1");
if (mysql_num_rows($result) == 1)
mysql_query("UPDATE `$table` SET `date` = NOW() WHERE `id` = $id");
else
showForm();
}
else
showForm();
?>
CODE:
Дамп таблицы:<?php
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'test');
define('MYSQL_PWD', 'test');
define('MYSQL_DB', 'test');
define('MYSQL_TABLE', 'users');
?>
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'test');
define('MYSQL_PWD', 'test');
define('MYSQL_DB', 'test');
define('MYSQL_TABLE', 'users');
?>
CODE:
Проверил у себя, вроде бы работает.
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`login` tinytext NOT NULL,
`pwd` varchar(32) NOT NULL default '',
`temp` varchar(32) default NULL,
`ip` tinytext NOT NULL,
`browser` tinytext NOT NULL,
`date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=2 ;
`id` int(11) NOT NULL auto_increment,
`login` tinytext NOT NULL,
`pwd` varchar(32) NOT NULL default '',
`temp` varchar(32) default NULL,
`ip` tinytext NOT NULL,
`browser` tinytext NOT NULL,
`date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=2 ;