Path : /home/vishqocm/pcib.in/debug/database/
File Upload :
Current File : /home/vishqocm//pcib.in/debug/database/fix_conn_variable.php

<?php
/**
 * Fix Connection Variable in Header
 * 
 * This script checks if the admin/includes/header.php file properly includes
 * the database connection and fixes the "Undefined variable $conn" error.
 * 
 * Run this script if you encounter:
 * "Warning: Undefined variable $conn in C:\xampp\htdocs\admin\includes\header.php"
 */

// Include database configuration
require_once '../../admin/database/db_config.php';

// Display header
echo "<!DOCTYPE html>
<html>
<head>
    <title>Database Maintenance - Fix Connection Variable</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; color: #333; }
        h1 { color: #2c3e50; border-bottom: 2px solid #eee; padding-bottom: 10px; }
        h2 { color: #3498db; margin-top: 20px; }
        .container { max-width: 800px; margin: 0 auto; background: #f9f9f9; padding: 20px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .success { color: #27ae60; background-color: #d5f5e3; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
        .error { color: #c0392b; background-color: #f8d7da; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
        .info { color: #2980b9; background-color: #d6eaf8; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
        .warning { color: #f39c12; background-color: #fef9e7; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
        table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        table, th, td { border: 1px solid #ddd; }
        th, td { padding: 12px; text-align: left; }
        th { background-color: #f2f2f2; }
        a { color: #3498db; text-decoration: none; }
        a:hover { text-decoration: underline; }
        .btn { display: inline-block; background: #3498db; color: white; padding: 8px 16px; border-radius: 4px; text-decoration: none; margin-top: 15px; }
        .btn:hover { background: #2980b9; text-decoration: none; }
        pre { background: #f8f8f8; padding: 10px; border-radius: 4px; overflow-x: auto; }
        code { font-family: monospace; font-size: 14px; }
        .diff-highlight { background-color: #fff59d; }
    </style>
</head>
<body>
    <div class='container'>
        <h1>Fix Connection Variable in Header</h1>
        <p>This script checks and fixes the undefined \$conn variable error in header.php files.</p>";

try {
    // Check for admin header file
    $admin_header_path = '../../admin/includes/header.php';
    $public_header_path = '../../includes/header.php';
    $db_config_path = '../../admin/database/db_config.php';
    
    echo "<h2>Checking File Existence:</h2>";
    echo "<table>
            <tr>
                <th>File</th>
                <th>Status</th>
            </tr>
            <tr>
                <td>Admin Header</td>
                <td>" . (file_exists($admin_header_path) ? "<span class='info'>Found</span>" : "<span class='warning'>Not Found</span>") . "</td>
            </tr>
            <tr>
                <td>Public Header</td>
                <td>" . (file_exists($public_header_path) ? "<span class='info'>Found</span>" : "<span class='warning'>Not Found</span>") . "</td>
            </tr>
            <tr>
                <td>Database Config</td>
                <td>" . (file_exists($db_config_path) ? "<span class='info'>Found</span>" : "<span class='error'>Not Found - Critical</span>") . "</td>
            </tr>
          </table>";
    
    // Check and fix admin header file
    if (file_exists($admin_header_path)) {
        $header_content = file_get_contents($admin_header_path);
        
        // Check if the file already includes the database config
        $has_db_include = (strpos($header_content, 'require_once') !== false && 
                          (strpos($header_content, 'db_config.php') !== false || 
                           strpos($header_content, 'database/db_config.php') !== false));
        
        echo "<h2>Admin Header File Analysis:</h2>";
        echo "<table>
                <tr>
                    <th>Check</th>
                    <th>Status</th>
                </tr>
                <tr>
                    <td>Database config include/require</td>
                    <td>" . ($has_db_include ? "<span class='info'>Found</span>" : "<span class='warning'>Missing</span>") . "</td>
                </tr>
                <tr>
                    <td>\$conn variable checks/protection</td>
                    <td>" . (strpos($header_content, 'isset($conn)') !== false ? "<span class='info'>Found</span>" : "<span class='warning'>Missing</span>") . "</td>
                </tr>
              </table>";
        
        // If database include is missing, add it at the top of the file
        if (!$has_db_include) {
            // Create backup of original file
            $backup_path = $admin_header_path . '.bak.' . date('Ymd_His');
            if (copy($admin_header_path, $backup_path)) {
                echo "<div class='info'>Created backup of admin header file: " . basename($backup_path) . "</div>";
                
                // Add include statement at the top after opening PHP tag
                $modified_content = preg_replace(
                    '/^<\?php/',
                    '<?php
// Include database configuration
require_once __DIR__ . "/../database/db_config.php";

// Make sure we have a database connection
if (!isset($conn) || !($conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . "/../database/db_config.php";
}',
                    $header_content
                );
                
                // Write modified content back to file
                if (file_put_contents($admin_header_path, $modified_content)) {
                    echo "<div class='success'>✅ Successfully updated admin header file with database connection include!</div>";
                    
                    echo "<h2>Changes Made:</h2>";
                    echo "<pre><code><span class='diff-highlight'>// Include database configuration
require_once __DIR__ . \"/../database/db_config.php\";

// Make sure we have a database connection
if (!isset(\$conn) || !(\$conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . \"/../database/db_config.php\";
}</span></code></pre>";
                } else {
                    echo "<div class='error'>❌ Failed to write modified content to admin header file. Please check file permissions.</div>";
                }
            } else {
                echo "<div class='error'>❌ Failed to create backup of admin header file. Please check file permissions.</div>";
            }
        } else {
            // If the inclusion is already there but no check for $conn variable, add the check
            if (strpos($header_content, 'isset($conn)') === false) {
                // Create backup of original file
                $backup_path = $admin_header_path . '.bak.' . date('Ymd_His');
                if (copy($admin_header_path, $backup_path)) {
                    echo "<div class='info'>Created backup of admin header file: " . basename($backup_path) . "</div>";
                    
                    // Add check after existing include statement
                    $pattern = '/(require|include)(_once)?\s+[\'"].*db_config\.php[\'"]\s*;/';
                    $replacement = '$0' . "\n\n" . '// Make sure we have a database connection
if (!isset($conn) || !($conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . "/../database/db_config.php";
}';
                    $modified_content = preg_replace($pattern, $replacement, $header_content);
                    
                    // Write modified content back to file
                    if (file_put_contents($admin_header_path, $modified_content)) {
                        echo "<div class='success'>✅ Successfully added \$conn variable check to admin header file!</div>";
                        
                        echo "<h2>Changes Made:</h2>";
                        echo "<pre><code><span class='diff-highlight'>// Make sure we have a database connection
if (!isset(\$conn) || !(\$conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . \"/../database/db_config.php\";
}</span></code></pre>";
                    } else {
                        echo "<div class='error'>❌ Failed to write modified content to admin header file. Please check file permissions.</div>";
                    }
                } else {
                    echo "<div class='error'>❌ Failed to create backup of admin header file. Please check file permissions.</div>";
                }
            } else {
                echo "<div class='success'>✅ Admin header file already has proper database connection and variable checks. No changes needed.</div>";
            }
        }
    } else {
        echo "<div class='error'>❌ Could not find admin header file. Manual intervention required.</div>";
    }
    
    // Also check public header file if it exists
    if (file_exists($public_header_path)) {
        $header_content = file_get_contents($public_header_path);
        
        // Check if the file already includes the database config
        $has_db_include = (strpos($header_content, 'require_once') !== false && 
                          (strpos($header_content, 'db_config.php') !== false || 
                           strpos($header_content, 'database/db_config.php') !== false));
        
        echo "<h2>Public Header File Analysis:</h2>";
        echo "<table>
                <tr>
                    <th>Check</th>
                    <th>Status</th>
                </tr>
                <tr>
                    <td>Database config include/require</td>
                    <td>" . ($has_db_include ? "<span class='info'>Found</span>" : "<span class='warning'>Missing</span>") . "</td>
                </tr>
                <tr>
                    <td>\$conn variable checks/protection</td>
                    <td>" . (strpos($header_content, 'isset($conn)') !== false ? "<span class='info'>Found</span>" : "<span class='warning'>Missing</span>") . "</td>
                </tr>
              </table>";
        
        // If database include is missing, add it at the top of the file
        if (!$has_db_include) {
            // Create backup of original file
            $backup_path = $public_header_path . '.bak.' . date('Ymd_His');
            if (copy($public_header_path, $backup_path)) {
                echo "<div class='info'>Created backup of public header file: " . basename($backup_path) . "</div>";
                
                // Add include statement at the top after opening PHP tag
                $modified_content = preg_replace(
                    '/^<\?php/',
                    '<?php
// Include database configuration
require_once __DIR__ . "/../admin/database/db_config.php";

// Make sure we have a database connection
if (!isset($conn) || !($conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . "/../admin/database/db_config.php";
}',
                    $header_content
                );
                
                // Write modified content back to file
                if (file_put_contents($public_header_path, $modified_content)) {
                    echo "<div class='success'>✅ Successfully updated public header file with database connection include!</div>";
                    
                    echo "<h2>Changes Made to Public Header:</h2>";
                    echo "<pre><code><span class='diff-highlight'>// Include database configuration
require_once __DIR__ . \"/../admin/database/db_config.php\";

// Make sure we have a database connection
if (!isset(\$conn) || !(\$conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . \"/../admin/database/db_config.php\";
}</span></code></pre>";
                } else {
                    echo "<div class='error'>❌ Failed to write modified content to public header file. Please check file permissions.</div>";
                }
            } else {
                echo "<div class='error'>❌ Failed to create backup of public header file. Please check file permissions.</div>";
            }
        } else {
            // If the inclusion is already there but no check for $conn variable, add the check
            if (strpos($header_content, 'isset($conn)') === false) {
                // Create backup of original file
                $backup_path = $public_header_path . '.bak.' . date('Ymd_His');
                if (copy($public_header_path, $backup_path)) {
                    echo "<div class='info'>Created backup of public header file: " . basename($backup_path) . "</div>";
                    
                    // Add check after existing include statement
                    $pattern = '/(require|include)(_once)?\s+[\'"].*db_config\.php[\'"]\s*;/';
                    $replacement = '$0' . "\n\n" . '// Make sure we have a database connection
if (!isset($conn) || !($conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . "/../admin/database/db_config.php";
}';
                    $modified_content = preg_replace($pattern, $replacement, $header_content);
                    
                    // Write modified content back to file
                    if (file_put_contents($public_header_path, $modified_content)) {
                        echo "<div class='success'>✅ Successfully added \$conn variable check to public header file!</div>";
                        
                        echo "<h2>Changes Made to Public Header:</h2>";
                        echo "<pre><code><span class='diff-highlight'>// Make sure we have a database connection
if (!isset(\$conn) || !(\$conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . \"/../admin/database/db_config.php\";
}</span></code></pre>";
                    } else {
                        echo "<div class='error'>❌ Failed to write modified content to public header file. Please check file permissions.</div>";
                    }
                } else {
                    echo "<div class='error'>❌ Failed to create backup of public header file. Please check file permissions.</div>";
                }
            } else {
                echo "<div class='success'>✅ Public header file already has proper database connection and variable checks. No changes needed.</div>";
            }
        }
    } else {
        echo "<div class='warning'>⚠️ Could not find public header file. No changes made to it.</div>";
    }
    
    // Display manual instructions as a fallback
    echo "<h2>Manual Fix Instructions:</h2>";
    echo "<p>If the automatic fix doesn't work, you can manually add the following code at the top of your header.php files:</p>";
    
    echo "<pre><code>// Include database configuration
require_once __DIR__ . \"/../database/db_config.php\";  // For admin/includes/header.php
// OR
require_once __DIR__ . \"/../admin/database/db_config.php\";  // For includes/header.php

// Make sure we have a database connection
if (!isset(\$conn) || !(\$conn instanceof mysqli)) {
    // Attempt to reconnect or initialize connection
    require_once __DIR__ . \"/../database/db_config.php\";  // Adjust path as needed
}</code></pre>";
    
    echo "<div class='links'>
            <a href='../../index.php' class='btn'>Return to Homepage</a>
            <a href='../../admin/index.php' class='btn'>Admin Dashboard</a>
            <a href='../index.php' class='btn'>Debug Tools</a>
          </div>";
    
} catch (Exception $e) {
    echo "<div class='error'>Error: " . $e->getMessage() . "</div>";
    echo "<div class='links'>
            <a href='../../index.php' class='btn'>Return to Homepage</a>
            <a href='../../admin/index.php' class='btn'>Admin Dashboard</a>
            <a href='../index.php' class='btn'>Debug Tools</a>
          </div>";
}

echo "</div></body></html>";
?>