2

ログインとログアウトのセッションを作成したい

MySQL データベースのテーブルは次のようになっています

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 ;

および名前が付けられたMySQL接続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

<?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、別のページに移動しないことです。このコードを見て、私が間違っているときは教えてください。

ありがとう。

4

5 に答える 5

0

使用中securedpage.php:

...
if (!isset($_SESSION['username'])==null)
{
    header('Location: index.php');
}
...

それ以外の:

...
if (!isset($_SESSION['username']))
{
    header('Location: index.php');
}
...

で同じことを行いますindex.php

于 2014-03-31T06:14:08.487 に答える
0

まず、同じ資格情報を持つ複数のユーザーがいる可能性があります。

次に、入力したユーザー名またはパスワードを持つユーザーがいない可能性があります。

第三に、SQLクエリでpassword = '" . mysql_real_escape_string(md5($_POST['password']))

着替える

password = '" .    md5(mysql_real_escape_string($_POST['password']))

テーブル名「login」または「user」を確認してください??

これを試して

于 2013-06-18T12:11:04.790 に答える