Style
Style
Updated register.php:
<?php
session_start(); // Start session
$host = 'localhost';
$db = 'user_system';
$user = 'root'; // Change if needed
$pass = ''; // Change if needed
$fullname = trim($_POST['fullname']);
$school = trim($_POST['school']);
$cell = trim($_POST['cell']);
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
if ($check->num_rows > 0) {
echo "Email or username already exists!";
} else {
$stmt = $conn->prepare("INSERT INTO users (fullname, school, cell, username, email,
password) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $fullname, $school, $cell, $username, $email, $password);
if ($stmt->execute()) {
// Redirect to success page or index page after successful registration
header("Location: success.php"); // You can also use "index.php" here
exit();
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
$check->close();
$conn->close();
?>
We’ll create a login_check.php file that you can include at the top of each page where you
need to verify if the user is logged in.
<!DOCTYPE html>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<h1>Welcome to the Home Page!</h1>
<p>You are logged in as <?php echo $_SESSION['username']; ?></p>
<a href="logout.php">Logout</a>
</body>
</html>
login.php (Form)
<form action="login_handler.php" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" value="Login" />
</form>
$host = 'localhost';
$db = 'user_system';
$user = 'root'; // Change if needed
$pass = ''; // Change if needed
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $_POST['username'];
$password = $_POST['password'];
if ($stmt->num_rows > 0) {
// Verify password
if (password_verify($password, $hashed_password)) {
// Password is correct, set session variables
$_SESSION['user_id'] = $id;
$_SESSION['username'] = $username;
header("Location: index.php"); // Redirect to index page
exit();
} else {
echo "Invalid password!";
}
} else {
echo "Username not found!";
}
$stmt->close();
$conn->close();
?>
<?php
session_start();
session_unset(); // Remove all session variables
session_destroy(); // Destroy the session
header("Location: login.php"); // Redirect to login page
exit();
?>
Now, every page where you include login_check.php will require the user to be logged in. If
not, they will be redirected to the login page.
Would you like me to help you refine any part of this system?
Certainly! Here's how you can style your user registration form, login form, and other
elements by adding them to a style.css file. This will keep your CSS separate from the HTML
structure, making it easier to maintain and manage.
/* Form Container */
form {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
/* Form Heading */
form h2 {
text-align: center;
margin-bottom: 20px;
font-size: 1.5em;
}
/* Input Fields */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="submit"] {
width: 100%;
padding: 12px;
margin-bottom: 15px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 1em;
}
/* Focus State */
input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus {
border-color: #007bff;
outline: none;
}
/* Submit Button */
input[type="submit"] {
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
a:hover {
text-decoration: underline;
}
/* Success Page */
.success-message {
text-align: center;
font-size: 1.2em;
color: #28a745;
}
/* Error Messages */
.error-message {
text-align: center;
font-size: 1em;
color: #dc3545;
}
/* Responsive Design */
@media (max-width: 600px) {
body {
padding: 20px;
}
form {
width: 100%;
}
}
<head>
<meta charset="UTF-8">
<title>User Registration</title>
<link rel="stylesheet" href="style.css"> <!-- Link to your CSS file -->
</head>
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Success</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="success-message">
<h2>Registration Successful!</h2>
<p>Welcome, <?php echo $_SESSION['username']; ?>!</p>
<a href="index.php">Go to Home</a>
</div>
</body>
</html>
This will display a success message in a neat style with a link to go back to the homepage.
<?php
session_start();
session_unset();
session_destroy();
header("Location: login.php");
exit();
?>
Responsive Design
The CSS also includes a media query to ensure the form is responsive and adapts for smaller
screens (e.g., smartphones). It adjusts the form width to 100% on screens smaller than 600px.
Final Notes:
● Organize your file structure: Keep style.css in the same directory as your HTML files
or in a css/ folder.
● Adjust form elements: Customize placeholders, labels, or button styles to suit your
design preferences.