创建登录和注销会话 [英] create login and logout session

查看:92
本文介绍了创建登录和注销会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建登录和注销会话

I want to create login and logout session

我在MySQL数据库中的表看起来像这样

My table in MySQL database is looking like this

CREATE TABLE `login` (
  `id` int(10) NOT NULL auto_increment,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

和名为config.inc

<?php

 $hostname = 'localhost';        // Your MySQL hostname. Usualy named as 'localhost',     so you're NOT necessary to change this even this script has already     online on the internet.
$dbname   = ''; // Your database name.
$username = '';             // Your database username.
$password = '';                 // Your database password. If your database has no     password, leave it empty.

// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed,     perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');

?>

我的index.php页面看起来像这样

<?php

// Inialize session
session_start();

// Check, if user is already login, then jump to secured page
if (isset($_SESSION['username'])) {
header('Location: securedpage.php');
}

?>
<html>

<head>
<title>PHPMySimpleLogin 0.3</title>
</head>

<body>

 <h3>User Login</h3>

<table border="0">
<form method="POST" action="loginproc.php">
<tr><td>Username</td><td>:</td><td><input type="text" name="username" size="20">        </td></tr>
<tr><td>Password</td><td>:</td><td><input type="password" name="password"     size="20">    </td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" value="Login"></td></tr>
</form>
</table>

</body>

</html>

检查名为loginproc.php

<?php

// Inialize session
session_start();

// Include database connection settings
include('config.inc');

// Retrieve username and password from database according to user's input
$login = mysql_query("SELECT * FROM user WHERE (username = '" .     mysql_real_escape_string($_POST['username']) . "') and (password = '" .     mysql_real_escape_string(md5($_POST['password'])) . "')");

// Check username and password match
 if (mysql_num_rows($login) == 1) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
// Jump to secured page
 header('Location: securedpage.php');
}
else {
// Jump to login page
header('Location: index.php');
}

?>

我的安全页面代码名为securedpage.php

My secure page code named as securedpage.php

<?php

// Inialize session
session_start();

// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
header('Location: index.php');
}

?>
<html>

<head>
<title>Secured Page</title>
</head>

<body>

<p>This is secured page with session: <b><?php echo $_SESSION['username']; ?></b>
<br>You can put your restricted information here.</p>
<p><a href="logout.php">Logout</a></p>

</body>

</html>

最后退出页面名为logout.php

<?php

// Inialize session
session_start();

// Delete certain session
unset($_SESSION['username']);
// Delete all session variables
// session_destroy();

// Jump to login page
header('Location: index.php');

?>

现在我的问题是,当我输入用户名和密码时,它将仅停留在index.php,而不会转到其他页面.请查看此代码,并告诉我什么时候做错了.

Now my problem is when I am entering username and password it will stay only at index.php , it is not going to another page. Please see this code and tell me when I am doing wrong.

谢谢.

推荐答案

首先,首先是

First of all, the regular mysql functions will be deprecated in PHP 5.5.0.

其次,您在名为login的表中搜索<​​c6>.

Secondly, you're searching inside a table called user while you named yours login.

而且...您不需要过滤md5.

And ... you don't need to filter md5.

祝你好运.

这篇关于创建登录和注销会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆