用php和mysql做一个邮箱页面包含登录注册,详细代码展示出来
时间: 2024-03-06 18:49:47 浏览: 68
好的,下面是一个简单的邮箱页面包含登录和注册功能的代码实现,代码中包含了基本的HTML、CSS和PHP代码。需要注意的是,这只是一个基础的实现,您可以根据具体的需求进行修改和扩展。
文件结构:
```
- index.php
- login.php
- register.php
- logout.php
- mail.php
- mysql.php
- style.css
```
index.php
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Mailbox</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Welcome to My Mailbox</h1>
<?php
session_start();
if (isset($_SESSION['username'])) {
echo '<p>Welcome, ' . $_SESSION['username'] . '</p>';
echo '<a href="mail.php">Go to Mailbox</a>';
echo '<a href="logout.php">Logout</a>';
} else {
echo '<a href="login.php">Login</a>';
echo '<a href="register.php">Register</a>';
}
?>
</div>
</body>
</html>
```
login.php
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login - My Mailbox</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Login</h1>
<form action="mysql.php" method="post">
<input type="hidden" name="action" value="login">
<label>Username:</label>
<input type="text" name="username" required>
<label>Password:</label>
<input type="password" name="password" required>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
```
register.php
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register - My Mailbox</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Register</h1>
<form action="mysql.php" method="post">
<input type="hidden" name="action" value="register">
<label>Username:</label>
<input type="text" name="username" required>
<label>Password:</label>
<input type="password" name="password" required>
<label>Email:</label>
<input type="email" name="email" required>
<input type="submit" value="Register">
</form>
</div>
</body>
</html>
```
logout.php
```php
<?php
session_start();
session_destroy();
header('Location: index.php');
?>
```
mail.php
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Mailbox</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>My Mailbox</h1>
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: index.php');
}
echo '<p>Welcome, ' . $_SESSION['username'] . '</p>';
echo '<a href="logout.php">Logout</a>';
?>
<h2>Inbox</h2>
<?php
require('mysql.php');
$conn = connect();
$username = $_SESSION['username'];
$sql = "SELECT * FROM mails WHERE receiver='$username' ORDER BY timestamp DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo '<table>';
while ($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $row['sender'] . '</td>';
echo '<td>' . $row['subject'] . '</td>';
echo '<td>' . date('Y-m-d H:i:s', $row['timestamp']) . '</td>';
echo '<td><a href="mail_detail.php?id=' . $row['id'] . '">View</a></td>';
echo '</tr>';
}
echo '</table>';
} else {
echo '<p>No mail found.</p>';
}
$conn->close();
?>
<h2>Compose</h2>
<form action="mysql.php" method="post">
<input type="hidden" name="action" value="send_mail">
<label>Recipient:</label>
<input type="email" name="receiver" required>
<label>Subject:</label>
<input type="text" name="subject" required>
<label>Content:</label>
<textarea name="content" required></textarea>
<input type="submit" value="Send">
</form>
</div>
</body>
</html>
```
mysql.php
```php
<?php
function connect() {
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'my_mailbox';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
return $conn;
}
function register($username, $password, $email) {
$conn = connect();
$sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')";
if ($conn->query($sql) === TRUE) {
echo '<p>Registration successful.</p>';
} else {
echo '<p>Error: ' . $conn->error . '</p>';
}
$conn->close();
}
function login($username, $password) {
$conn = connect();
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
session_start();
$_SESSION['username'] = $username;
header('Location: mail.php');
} else {
echo '<p>Invalid username or password.</p>';
}
$conn->close();
}
function send_mail($sender, $receiver, $subject, $content) {
$conn = connect();
$timestamp = time();
$sql = "INSERT INTO mails (sender, receiver, subject, content, timestamp) VALUES ('$sender', '$receiver', '$subject', '$content', '$timestamp')";
if ($conn->query($sql) === TRUE) {
echo '<p>Mail sent successfully.</p>';
} else {
echo '<p>Error: ' . $conn->error . '</p>';
}
$conn->close();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$action = $_POST['action'];
switch ($action) {
case 'register':
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
register($username, $password, $email);
break;
case 'login':
$username = $_POST['username'];
$password = $_POST['password'];
login($username, $password);
break;
case 'send_mail':
session_start();
$sender = $_SESSION['username'];
$receiver = $_POST['receiver'];
$subject = $_POST['subject'];
$content = $_POST['content'];
send_mail($sender, $receiver, $subject, $content);
break;
default:
break;
}
}
?>
```
style.css
```css
.container {
max-width: 800px;
margin: 0 auto;
}
h1, h2 {
text-align: center;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
tr:hover {
background-color: #f5f5f5;
}
label {
display: block;
margin-top: 16px;
}
input[type="text"],
input[type="password"],
input[type="email"],
textarea {
display: block;
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-bottom: 16px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
}
a {
display: inline-block;
margin-right: 8px;
}
p {
margin-bottom: 16px;
}
```
以上是一个简单的邮箱页面包含登录和注册功能的实现,希望对您有所帮助。
阅读全文