# ADMIN LOGIN FIX - Quick Solution

## ❌ Problem: "Invalid username or password" error

This happens when the default admin password hash in the database doesn't match the expected password.

---

## ✅ SOLUTION 1: Use Setup Script (Recommended - 2 minutes)

### Step 1: Access the Setup Script
Visit in your browser:
```
https://yourdomain.com/project-planner/setup_admin.php
```

### Step 2: Click "Setup Admin Account"
The script will:
- Create/reset the admin account
- Set password to: `admin123`
- Verify the hash works correctly

### Step 3: Login
- Username: `admin`
- Password: `admin123`

### Step 4: IMPORTANT - Secure Your Installation
1. **Immediately change the admin password** (after logging in, go to Profile)
2. **Delete setup_admin.php file** (for security)
3. Set `DEBUG_MODE = false` in config.php

---

## ✅ SOLUTION 2: Manual SQL Fix (Advanced Users)

### Option A: Using phpMyAdmin

1. Login to cPanel → phpMyAdmin
2. Select your database
3. Click "SQL" tab
4. Run this query:

```sql
UPDATE users 
SET password = '$2y$10$rZ5fGwFw5qVYqKxLZz.Zo.vYJ9WzQ6RbqLzV7HvJ4nK.MBVqGCKYy' 
WHERE username = 'admin';
```

5. Try logging in again with: `admin` / `admin123`

### Option B: Re-import Database Schema

1. In phpMyAdmin, drop these tables (in order):
   - activity_log
   - file_uploads
   - deliverables
   - milestones
   - tasks
   - projects
   - users

2. Re-import `database_schema.sql`

3. Try logging in with: `admin` / `admin123`

---

## ✅ SOLUTION 3: Create New Admin Account

If all else fails, manually insert a new admin:

1. Go to phpMyAdmin → your database → users table
2. Click "Insert" tab
3. Fill in:
   - username: `admin`
   - email: `admin@example.com`
   - password: Use `generate_hash.php` to create hash (see below)
   - full_name: `System Administrator`
   - is_active: `1`

### Using generate_hash.php:

1. Upload `generate_hash.php` to your server
2. Visit: `https://yourdomain.com/project-planner/generate_hash.php`
3. Copy the generated hash
4. Use that hash in the password field
5. Delete `generate_hash.php` after use

---

## 🔍 TROUBLESHOOTING

### Still Can't Login?

**Check 1: Verify Database Connection**
Enable debug mode in `config.php`:
```php
define('DEBUG_MODE', true);
```
Try logging in - you'll see detailed error messages.

**Check 2: Verify User Exists**
In phpMyAdmin, run:
```sql
SELECT * FROM users WHERE username = 'admin';
```
If no results, the user doesn't exist - use Solution 1 or 3.

**Check 3: PHP Version**
Password hashing requires PHP 7.4+. Check your PHP version:
- Create file: `info.php`
- Content: `<?php phpinfo(); ?>`
- Visit: `https://yourdomain.com/project-planner/info.php`
- Look for PHP version at top
- Delete `info.php` after checking

**Check 4: BASE_URL Configuration**
In `config.php`, verify BASE_URL:
```php
// Must end with trailing slash!
define('BASE_URL', 'https://yourdomain.com/project-planner/');

// NOT like this:
// define('BASE_URL', 'https://yourdomain.com/project-planner');
```

**Check 5: Session Configuration**
In `config.php`, temporarily set:
```php
ini_set('session.cookie_secure', 0); // If you don't have HTTPS
```

---

## 🔒 AFTER FIXING - SECURITY CHECKLIST

Once you can login:

1. ✅ Change admin password immediately
2. ✅ Delete `setup_admin.php`
3. ✅ Delete `generate_hash.php`
4. ✅ Set `DEBUG_MODE = false` in config.php
5. ✅ Delete `info.php` if you created it
6. ✅ Set proper file permissions:
   - config.php: 640 or 600
   - All PHP files: 644
   - All folders: 755
   - uploads folder: 755

---

## 📞 NEED MORE HELP?

### Common Issues:

**"Database connection error"**
→ Check config.php database credentials

**"Table 'users' doesn't exist"**
→ Import database_schema.sql

**Blank page after login**
→ Check file permissions, enable DEBUG_MODE

**Session errors**
→ Check session.save_path permissions

---

## 🎯 RECOMMENDED SOLUTION

**Use Solution 1 (Setup Script)** - It's the fastest and most reliable method.

1. Visit: `https://yourdomain.com/project-planner/setup_admin.php`
2. Click button
3. Login with admin/admin123
4. Change password
5. Delete setup_admin.php

**Total time: 2 minutes**

---

## 📝 WHY THIS HAPPENED

The default password hash in `database_schema.sql` may have been:
- Truncated during copy/paste
- Incompatible with your PHP version
- Corrupted during file transfer
- Not properly escaped in SQL

The setup script generates a fresh hash using your server's PHP installation, ensuring compatibility.

---

**Need to start fresh?**

Drop all tables and re-import database_schema.sql, then use setup_admin.php to create the admin account properly.
