Propmp
Propmp
File Structure:
Copy
instagram_clone/
├── assets/
│ ├── css/ # For CSS files
│ ├── js/ # For JavaScript files
│ └── images/ # For uploaded images
├── includes/ # For PHP includes (e.g., database connection)
├── pages/ # For individual pages (e.g., login, signup)
├── index.php # Homepage
├── login.php # Login page
├── signup.php # Signup page
└── profile.php # Profile page
3. Set Up MySQL Database
Open phpMyAdmin:
Create a Database:
Create Tables:
We’ll design the database schema in the next step. For now, just create the
database.
php
Copy
<?php
echo "PHP is working!";
?>
Open http://localhost/instagram_clone/test.php in your browser. You should see "PHP
is working!".
php
Copy
<?php
$host = 'localhost';
$user = 'root'; // Default XAMPP MySQL username
$password = ''; // Default XAMPP MySQL password
$dbname = 'instagram_clone';
// Create connection
$conn = new mysqli($host, $user, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
?>
Open http://localhost/instagram_clone/db_test.php in your browser. You should see
"Connected successfully!".
Next Steps
Once you’ve completed the above steps, let me know, and we’ll move on to Step 2:
Database Design. I’ll provide the MySQL schema for the users, posts, likes,
comments, and follows tables, along with their relationships.
next
Great! Let’s move on to Step 2: Database Design.
1. Table: users
This table will store user information, including their credentials and profile
details.
sql
Copy
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL, -- Hashed password
profile_picture VARCHAR(255) DEFAULT 'default.jpg', -- Path to profile picture
bio TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Fields:
2. Table: posts
This table will store posts made by users.
sql
Copy
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
image_path VARCHAR(255) NOT NULL, -- Path to the uploaded image
caption TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
Fields:
user_id: Foreign key referencing the users table to associate the post with a user.
3. Table: likes
This table will store likes on posts.
sql
Copy
CREATE TABLE likes (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
post_id INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
UNIQUE KEY unique_like (user_id, post_id) -- Ensure a user can like a post only
once
);
Fields:
user_id: Foreign key referencing the users table to associate the like with a user.
post_id: Foreign key referencing the posts table to associate the like with a post.
4. Table: comments
This table will store comments on posts.
sql
Copy
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
post_id INT NOT NULL,
comment TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE
);
Fields:
user_id: Foreign key referencing the users table to associate the comment with a
user.
post_id: Foreign key referencing the posts table to associate the comment with a
post.
5. Table: follows
This table will store follow relationships between users.
sql
Copy
CREATE TABLE follows (
id INT AUTO_INCREMENT PRIMARY KEY,
follower_id INT NOT NULL, -- User who is following
following_id INT NOT NULL, -- User who is being followed
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (follower_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (following_id) REFERENCES users(id) ON DELETE CASCADE,
UNIQUE KEY unique_follow (follower_id, following_id) -- Ensure a user can
follow another user only once
);
Fields:
follower_id: Foreign key referencing the users table for the user who is following.
following_id: Foreign key referencing the users table for the user who is being
followed.
UNIQUE KEY: Ensures a user can follow another user only once.
6. Create the Tables
Open phpMyAdmin (http://localhost/phpmyadmin).
Go to the SQL tab and paste the above CREATE TABLE statements one by one.
users
posts
likes
comments
follows
Next Steps
Once you’ve created the tables, let me know, and we’ll move on to Step 3: Create
User Authentication. This will include:
next
Great! Let’s move on to Step 3: Create User Authentication. This step will cover:
html
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signup - Instagram Clone</title>
<link rel="stylesheet" href="assets/css/styles.css">
</head>
<body>
<div class="container">
<h2>Signup</h2>
<form action="signup.php" method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<input type="password" name="confirm_password" placeholder="Confirm
Password" required>
<button type="submit" name="signup">Signup</button>
</form>
<p>Already have an account? <a href="login.php">Login</a></p>
</div>
</body>
</html>
Run HTML
Process the Signup Form:
php
Copy
<?php
session_start();
if (isset($_POST['signup'])) {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$confirm_password = trim($_POST['confirm_password']);
// Validate inputs
if (empty($username) || empty($email) || empty($password) ||
empty($confirm_password)) {
echo "All fields are required!";
} elseif ($password !== $confirm_password) {
echo "Passwords do not match!";
} else {
// Check if username or email already exists
$sql = "SELECT * FROM users WHERE username = ? OR email = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $username, $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
echo "Username or email already exists!";
} else {
// Hash the password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Insert user into the database
$sql = "INSERT INTO users (username, email, password) VALUES
(?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sss", $username, $email, $hashed_password);
if ($stmt->execute()) {
echo "Signup successful! <a href='login.php'>Login</a>";
} else {
echo "Error: " . $stmt->error;
}
}
}
}
?>
Create the Database Connection File:
php
Copy
<?php
$host = 'localhost';
$user = 'root'; // Default XAMPP MySQL username
$password = ''; // Default XAMPP MySQL password
$dbname = 'instagram_clone';
// Create connection
$conn = new mysqli($host, $user, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
2. Create the Login System
Create the Login Form:
html
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - Instagram Clone</title>
<link rel="stylesheet" href="assets/css/styles.css">
</head>
<body>
<div class="container">
<h2>Login</h2>
<form action="login.php" method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit" name="login">Login</button>
</form>
<p>Don't have an account? <a href="signup.php">Signup</a></p>
</div>
</body>
</html>
Run HTML
Process the Login Form:
php
Copy
<?php
session_start();
if (isset($_POST['login'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
// Validate inputs
if (empty($username) || empty($password)) {
echo "All fields are required!";
} else {
// Fetch user from the database
$sql = "SELECT * FROM users WHERE username = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$user = $result->fetch_assoc();
// Verify password
if (password_verify($password, $user['password'])) {
// Set session variables
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
php
Copy
<?php
session_start();
session_unset();
session_destroy();
header("Location: login.php");
exit();
?>
4. Test the Authentication System
Signup:
Go to http://localhost/instagram_clone/signup.php.
Fill out the form and submit it. You should see a success message.
Login:
Go to http://localhost/instagram_clone/login.php.
Enter your credentials and submit the form. You should be redirected to index.php.
Logout:
Go to http://localhost/instagram_clone/logout.php.
Next Steps
Once you’ve tested the authentication system, let me know, and we’ll move on to
Step 4: User Profiles. This will include:
next
Great! Let’s move on to Step 4: User Profiles. In this step, we’ll:
php
Copy
<?php
session_start();
php
Copy
<?php
session_start();
if (in_array($file_ext, $allowed_extensions)) {
$new_file_name = uniqid('', true) . '.' . $file_ext;
$upload_path = 'assets/images/' . $new_file_name;
if (move_uploaded_file($file_tmp, $upload_path)) {
// Delete old profile picture if it's not the default
if ($user['profile_picture'] !== 'default.jpg') {
unlink('assets/images/' . $user['profile_picture']);
}
Go to http://localhost/instagram_clone/profile.php.
You should see your profile information, including the default profile picture and
bio.
Edit Profile:
Go to http://localhost/instagram_clone/edit_profile.php.
Submit the form and check if the changes are reflected on the profile page.
Next Steps
Once you’ve tested the profile system, let me know, and we’ll move on to Step 5:
Post Creation. This will include: