データベースにメンバーテーブルを作成する
CREATE TABLE `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` char(20) NOT NULL,
`name` char(30) NOT NULL,
`email` char(40) NOT NULL,
`pass` char(20) NOT NULL,
PRIMARY KEY (`id`)
);
テーブルメンバーにユーザーを追加する
INSERT INTO `members` VALUES ('1', 'user1', 'mr. x', '[email protected]', '123');
ページconnection.php
でmysqlユーザーとして変数を変更するだけです。connection.php
<?php
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysl_database = "taxretur_login";
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db($mysl_database, $conn);
?>
login.php
<?php
include("connection.php");
if(isset($_POST["submit"])) {
$username = $_POST["username"];
$pass = $_POST["pass"];
$sql = "SELECT * FROM members
WHERE username='$username' AND pass='$pass'";
$result = mysql_query($sql);
$numRows = mysql_num_rows($result);
if($numRows==1) {
session_start();
$_SESSION["username"] = $username;
header("Location: ./profile.php");
} else {
echo "Invalid Login Information";
}
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<table>
<tr><td>User Name</td><td><input type="text" name="username" /></td></tr>
<tr><td>Password</td><td><input type="password" name="pass" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Login" /></td></tr>
</table>
</form>
profile.php
<?php
session_start();
include("connection.php");
$username = $_SESSION["username"];
$sql = "SELECT * FROM members WHERE username='$username'";
$result = mysql_query($sql);
if($row = mysql_fetch_array($result)) {
$username = $row["username"];
$name = $row["name"];
$email = $row["email"];
echo "
<table>
<tr><td>User Name</td><td> : </td><td>$username</td></tr>
<tr><td>Name</td><td> : </td><td>$name</td></tr>
<tr><td>Email</td><td> : </td><td>$email</td></tr>
</table>
";
}
?>