Webmail mail login php
Here is an example of a simple webmail login system using PHP:
index.php
<?php
session_start();
// Configuration
$db_host = 'localhost';
$db_username = 'your_username';
$db_password = 'your_password';
$db_name = 'your_database';
// Connect to database
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ". $conn->connect_error);
}
// Login form submission
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// Query to check username and password
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($query);
if ($result->num_rows > 0) {
// Login successful, start session
$_SESSION['username'] = $username;
header('Location: inbox.php');
exit;
} else {
$error = 'Invalid username or password';
}
}
// Display login form
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" name="submit" value="Login">
</form>
<?php if (isset($error)) {?>
<p style="color: red;"><?php echo $error;?></p>
<?php }?>
inbox.php
<?php
session_start();
// Check if user is logged in
if (!isset($_SESSION['username'])) {
header('Location: index.php');
exit;
}
// Display inbox
?>
<h1>Inbox</h1>
<?php
// Query to retrieve emails
$query = "SELECT * FROM emails WHERE recipient='".$_SESSION['username']."' ORDER BY date DESC";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo '<p>'.$row['subject'].': '.$row['message'].'</p>';
}
} else {
echo '<p>No emails found.</p>';
}
?>
emails table structure
CREATE TABLE emails (
id INT PRIMARY KEY AUTO_INCREMENT,
sender VARCHAR(255),
recipient VARCHAR(255),
subject VARCHAR(255),
message TEXT,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
users table structure
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255),
password VARCHAR(255)
);
This is a very basic example and you should consider implementing additional security measures such as:
- Hashing passwords using a library like PHPass
- Validating user input
- Using prepared statements to prevent SQL injection
- Implementing a secure password reset system
- Using a secure connection (HTTPS) to encrypt data in transit
Also, this example assumes that you have a users
table with a username
and password
column, and an emails
table with columns for sender
, recipient
, subject
, message
, and date
. You will need to modify the code to match your specific database schema.