Webbserv1: Källkod
Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/projects/slutprojekt/

blala.php
chat.php
createtable.php
delete.php
deletetable.php
deletetables.php
fetch_messages.php
filhantering/
footer.php
header.php
home.php
login.php
new_password.php
password_reset.php
profile.php
send_message.php
signup.php
verify.php

chat.php

197 lines UTF-8 Windows (CRLF)
<?php
session_start
();
?>
<!doctype html>
<head>
<style>
  .profile-table {
  width: 100%;
  border-collapse: collapse;
}

.profile-row {
  cursor: pointer;
  transition: background-color 0.3s;
}

.profile-row:hover {
  background-color: #f4f4f4;
}

.profile-row td {
  padding: 10px;
  vertical-align: middle;
}

.profile-img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
  display: block;
}

.profile-info {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  display: flex;
  align-items: center;
  gap: 10px;
}
#selected {
  background-color: lightgrey;
}
.content-wrapper {
  display: flex;
  gap: 20px;
  align-items: flex-start;
}
.info-box {
  width: 60%;
  background: #f9f9f9;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  margin: auto;
  padding: 5%;
}
.message {
  width: 100%;
  display: flex;
  gap: 20px;
  justify-content: center;
  height: 50px;
}
.right {
  text-align: right;
  justify-content: flex-end;
}
.left {
  text-align: left;
  justify-content: flex-start;
}
    </style>
</head>
<html>
<body>
<?php
include ('header.php');
include (
'../../dbconnection.php');
$message null;
?>
<div class="content-wrapper"><?php
    
try {
      
$sql "SELECT CASE 
                WHEN friends.user1Id = ? THEN friends.user2Id 
                ELSE friends.user1Id 
              END AS friendId
              FROM friends
              WHERE (friends.user1Id = ? OR friends.user2Id = ?)"
;
      
$stmt $dbconn->prepare($sql);
      
$stmt->execute([$_SESSION['id'], $_SESSION['id'], $_SESSION['id']]);
      
      
$friends $stmt->fetchAll(PDO::FETCH_COLUMN);

      
$output "<table class'profile-table'>";
      foreach (
$friends as $friendId) {
        
$sql "SELECT webusers.fName, webusers.lName, pictures.picture, friends.status, friends.user1Id
        FROM webusers
        INNER JOIN pictures ON webusers.id = pictures.userId
        INNER JOIN friends ON (webusers.id = friends.user1Id AND friends.user2Id = ?)
                           OR (webusers.id = friends.user2Id AND friends.user1Id = ?)
                           WHERE webusers.id = ?"
;
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute([$_SESSION['id'], $_SESSION['id'], $friendId]);
        
$user $stmt->fetch(PDO::FETCH_ASSOC);

        
$fName $user['fName'];
        
$lName $user['lName'];
        
$picture $user['picture'];
        
$status $user['status'];

        
$output .= "<tr onclick=\"window.location.href='?id=$friendId'\" class='profile-row'";
        if ( isset(
$_GET['id']) && $friendId==$_GET['id']) {
          
$output .= "id='selected'";
        }
        
$output .= ">
        <td>
            <img src='
$picture' alt='Profilbild' class='profile-img'>
        </td>
        <td class='profile-info'>
            <span class='profile-name'>
$fName $lName</span>
            
$status
        </td>
    </tr>"
;
      }
      
$output .= "</table>";
      echo 
"$output";
      
?>
      <div class="info-box">
      <div id="messageContainer">
      </div>
        <form autocomplete="off" id="messageForm">
        <input type="text" name="message" id="messageInput">
        <button>Skicka</button>
        </form>
      </div>
      <?php
      
}
    catch(
PDOException $e)  {
        echo 
$sql "<br>" $e->getMessage();
    }
?>
</div>
  
<?php
  
include ('footer.php');
  
$dbconn null;
?>
<script>
  const form = document.getElementById('messageForm');
  const input = document.getElementById('messageInput');
  const messageContainer = document.getElementById('messageContainer');
  const receiverId = new URLSearchParams(window.location.search).get('id');

  form.addEventListener('submit', async (e) => {
    e.preventDefault();
    const message = input.value.trim();
    if (!message) return;

    try {
      const response = await fetch('send_message.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: new URLSearchParams({
          message: message,
          receiverId: receiverId
        })
      });

      if (response.ok) {
        input.value = '';
        await loadMessages();
      }
    } catch (error) {
      console.error('Fel vid skick:', error);
    }
  });

  async function loadMessages() {
    if (!receiverId) return;

    try {
      const response = await fetch(`fetch_messages.php?receiverId=${receiverId}`);
      if (response.ok) {
        const messages = await response.text();
        messageContainer.innerHTML = messages;
      }
    } catch (error) {
      console.error('Fel vid hämtning:', error);
    }
  }

  loadMessages();
  setInterval(loadMessages, 2000);
</script>
</body>
</html>