Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

Can you help me work on website, im using php,css,javascript and MySQL(PhpMyAdmin). So…

  1. User

    Can you help me work on website, im using php,css,javascript and MySQL(PhpMyAdmin). So far i made 8 files, here is the code :

    1. index.php :

    if (_SERVER['REQUEST_METHOD'] == 'POST') { username = POST[′username′];_POST['username']; password = $_POST['password'];

    if (empty($username) || empty($password)) {
        echo "<div class='alert alert-danger' onclick='hideOverlay()'>All fields are required</div>";
    } else {
        $stmt = $conn->prepare("SELECT * FROM users_data WHERE username = ? AND password=?");
        $stmt->bind_param("ss", $username , $password);
        $stmt->execute();
        $result = $stmt->get_result();
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            if($row["id"] == 1){
                header("Location: admin_page/admin_page.php");
                exit();
            }
            else{
                header("Location: user_page/user_page.php");
                exit();
            }
        } else {
            echo "<div class='alert alert-danger' onclick='hideOverlay()'>Invalid username or password</div>";
    
        }
    }
    

    } ?>

    1. index.css :

    @import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');

    • { font-family: 'Montserrat', sans-serif; margin: 0; } body { background: linear-gradient(to bottom, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8)), url("../assets/img/hero_1.jpg") no-repeat center center fixed; background-size: cover; }

    .header { /* make header transparent and with a dark purple color / background-color: rgba(75, 0, 130, 0.5); padding: 20px; text-align: center; margin: 0; width: 100%; / make header stick to the device screen */ position: fixed; top: 0; left: 0; }

    .header h1 a{ color: white; } h1 { color: #333333; font-size: 36px; }

    h2 { color: #666666; font-size: 20px; }

    .container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; height: 200px; border: 2px solid white; border-radius: 10px; padding: 20px; background-color: rgba(255, 192, 203, 0.5); }

    h3 { /* make h3 in different color / color: #ffcc00; text-align: center; / make h3 bigger */ font-size: 24px; }

    input { display: block; width: 80%; margin: 10px auto; padding: 10px; border-radius: 40px; }

    button { display: block; width: 30%; margin: 10px auto; /* make button same color as h3 / background-color: #ffcc00; border: none; font-weight: bold; border-radius: 40px; / make button slightly bigger */ padding: 10px; }

    .overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 9999; display: none; } .overlay .error-container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; padding: 20px; border-radius: 5px; text-align: center; }

    .error-container { background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); text-align: center; }

    3)index.js :

    if (errors.length > 0) {
        // show the overlay
        document.querySelector('.overlay').style.display = 'block';
    
        // hide the overlay after 2 seconds
        setTimeout(hideOverlay, 2000);
    }
    
    // function to hide the overlay
    function hideOverlay() {
        document.querySelector('.overlay').style.display = 'none';
    }
    
    // add an event listener to the overlay element to hide it when clicked
    document.querySelector('.overlay').addEventListener('click', hideOverlay);
    

    4)database.php :

    5)user_page.php :

            // include your database connection file
            include '../database.php';
    
            // get the user's ID from the session
    
            if(isset($user_id['id'])){
                // do something with $your_array['id']
             } else {
                $user_id = $_SESSION['id'];
             }
            $user_id = $_SESSION['id'];
    
            // get the user's data from the database
            $stmt = $conn->prepare("SELECT * FROM users_data WHERE id=?");
            $stmt->bind_param("i", $user_id);
            $stmt->execute();
            $result = $stmt->get_result();
    
            if ($result->num_rows > 0) {
                // get the user's data
                $user = $result->fetch_assoc();
    
                // get the username
                $username = $user['username'];
            }
        ?>
    </body>
    <script src="user_page.js"></script>
    

    6)user_page.css :

    @import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');

    • { font-family: 'Montserrat', sans-serif; margin: 0; } body { background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8)), url("../assets/img/hero_1.jpg"); background-size: cover; background-repeat: no-repeat; } .overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: none; }

    .Welcome-container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 20px; } .container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; height: 200px; border: 2px solid white; border-radius: 10px; padding: 20px; background-color: white; }

    7)user_page.js :

    // get the overlay element var overlay = document.querySelector('.overlay');

    // show the overlay for 2 seconds overlay.style.display = 'block'; setTimeout(function() { overlay.style.display = 'none'; }, 2000);

    // hide the overlay when clicked overlay.addEventListener('click', function() { overlay.style.display = 'none'; });

    8)admin_page.php:

    </head>
    <body>
        <div class="header">
            <h1>AutoAD</h1>
            <h2>Автоматизация работы учебного отдела</h2>
        </div>
        <div class="container">
            <h3>Welcome, Admin!</h3>
            <p>Here you can manage the users and the courses.</p>
            <a href="users.php">View Users</a>
            <a href="courses.php">View Courses</a>
            <a href="logout.php">Log Out</a>
        </div>
    </body>
    
  2. Assistant

    It seems you have a basic structure of your project set up. I will provide some recommendations for you to improve your code.

    1. Separate PHP and HTML in your files: Instead of mixing PHP and HTML, you can separate the PHP parts into different files and include them in your HTML files, which makes your project more manageable.

    For example, create a new file called index_process.php and move the PHP code from index.php to this new file.

    index_process.php:

    <?php
    include 'database.php';
    session_start();
    
    function process_login()
    {
        global $conn;
    
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $username = $_POST['username'];
            $password = $_POST['password'];
            
            if (empty($username) || empty($password)) {
                return array('error' => 'All fields are required');
            } else {
                $stmt = $conn->prepare("SELECT * FROM users_data WHERE username = ? AND password=?");
                $stmt->bind_param("ss", $username , $password);
                $stmt->execute();
                $result = $stmt->get_result();
                if ($result->num_rows > 0) {
                    $row = $result->fetch_assoc();
                    if($row["id"] == 1){
                        header("Location: admin_page/admin_page.php");
                        exit();
                    }
                    else{
                        header("Location: user_page/user_page.php");
                        exit();
                    }
                } else {
                    return array('error' => 'Invalid username or password');
                }
            }
        }
    }
    
    $errors = process_login();
    

    Now include this file in your index.php file just before the HTML opening tag:

    index.php:

    <?php
    include 'index_process.php';
    ?>
    <!DOCTYPE html>
    <html>
      ...
    </html>
    

    You can also refer to this guide on How to Separate PHP and HTML Properly.

    1. Use modern frontend tools and libraries: You are currently using plain HTML, CSS, and JavaScript without using any frontend frameworks or libraries. You can use modern frontend tools and libraries like React, Angular, Vue, or even Bootstrap to make your project more modern, fast, and user-friendly.

    2. Implement a proper session management: Currently, you're initiating sessions but not managing them properly. Make sure to store user data in session after successful login, and create a proper authentication system to identify users before they can access user-specific pages.

    3. Use prepared statement for all database queries: In your user_page.php file, you have included database.php but not used prepared statements. Make sure to always use prepared statements for all database queries to avoid SQL injection vulnerabilities.

    4. Implement a proper logout functionality by destroying the user session when the user clicks on the "Log Out" button.

    5. Validate user input: Before processing any form input, ensure that the input data is valid and doesn't contain any special characters that might cause security issues or unexpected behavior.

    In summary, you have a basic structure, but there's room for improvement in terms of code organization, frontend design, user experience, session management, and security. Focus on these aspects to enhance your project. Keep coding and good luck with your project!