Onlinevoting System Project In Php And Mysql Source Code Github Link

Essay: Development of an Online Voting System Using PHP and MySQL

In the digital age, the demand for efficient, secure, and accessible voting mechanisms has grown significantly. Traditional paper-based voting is often time-consuming, resource-intensive, and prone to human error or manipulation. To address these challenges, I developed an Online Voting System using PHP for server-side scripting and MySQL for database management. This project aims to provide a streamlined, user-friendly platform for conducting elections in academic institutions, small organizations, or local communities. The complete source code is available on GitHub: https://github.com/yourusername/online-voting-system-php.

2. Cast Vote Logic (Ensuring One Vote)

session_start();
if (!isset($_SESSION['user_id'])) 
    header('Location: login.php');
    exit();

$voter_id = $_SESSION['user_id']; $candidate_id = $_POST['candidate_id'];

// Check if already voted $check = "SELECT is_voted FROM users WHERE id='$voter_id'"; $result = mysqli_query($conn, $check); $user = mysqli_fetch_assoc($result); Essay: Development of an Online Voting System Using

if ($user['is_voted'] == 0) // Insert vote $insert_vote = "INSERT INTO votes (voter_id, candidate_id, vote_date) VALUES ('$voter_id', '$candidate_id', NOW())"; mysqli_query($conn, $insert_vote);

// Update candidate vote count
$update_candidate = "UPDATE candidates SET vote_count = vote_count + 1 
                     WHERE id='$candidate_id'";
mysqli_query($conn, $update_candidate);
// Mark user as voted
$update_user = "UPDATE users SET is_voted=1 WHERE id='$voter_id'";
mysqli_query($conn, $update_user);
echo "Vote cast successfully!";

else echo "You have already voted!";

3. Functional Analysis

Most Core PHP/MySQL voting projects found on GitHub share a standard workflow: else echo "You have already voted

  1. Database Schema:
    • admin table (credentials).
    • voters table (ID, name, password, status: voted/pending).
    • candidates table (name, position, photo).
    • votes table (voter_id, candidate_id, timestamp).
  2. User Flow:
    • Voters register or are pre-registered by Admin.
    • Voter logs in.
    • System checks if status == 'voted'. If yes, redirect to results page. If no, show ballot.
    • Voter selects candidates and submits.
    • Database updates votes table and flips voter status to 'voted'.

How to Run the Project Locally

  1. Install XAMPP/WAMP on your local machine.
  2. Download Source Code from the GitHub repository (link provided below).
  3. Extract the folder to htdocs (XAMPP) or www (WAMP).
  4. Start Apache and MySQL from control panel.
  5. Open phpMyAdmin, create a database named voting_system.
  6. Import the provided database.sql file.
  7. Update database credentials in config/db.php.
  8. Run the project in browser: http://localhost/online-voting-system/

Security Measures They Added (Important!)

| Threat | Solution | |--------|----------| | Double voting | UNIQUE(voter_id, election_id) + has_voted flag | | SQL Injection | Prepared statements in every query | | Session hijacking | Regenerate session ID after login, set short session timeout | | Fake votes via URL | Validate election status and user role on every vote request | | XSS | htmlspecialchars() on all candidate names and party names | | CSRF | Add CSRF tokens on vote submission forms |