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

home.php

184 lines UTF-8 Windows (CRLF)
<?php
session_start
();
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
  <style>
    .profile-container {
      background: white;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
      padding-bottom: 20px;
      text-align: center;
      max-width: 400px;
      width: 100%;
      margin: auto;
      margin-top: 20px;
    }
    .profile-img {
      width: 400px;
      height: 400px;
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
      object-fit: cover;
      margin-bottom: 15px;
    }
    .profile-name {
      font-size: 24px;
      font-weight: bold;
      margin-bottom: 10px;
    }
    .profile-text {
      font-size: 14px;
      color: #555;
      margin-bottom: 10px;
    }
    .profile-city {
      font-size: 16px;
      font-weight: bold;
      color: #333;
    }
    .button {
      background-color: #007bff;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 5px;
      cursor: pointer;
      font-size: 16px;
      width: 48%;
    }
    .button:hover {
      background-color: #0056b3;
    }
    a {
      text-decoration: none;
      color: black;
    }
    a:hover {
      color: darkblue;
    }
    </style>
</head>
<body>
<?php
include ('header.php');
include (
'../../dbconnection.php');
$message null;
    
    try {
      if (
$_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['next']) || $_SERVER["REQUEST_METHOD"] == "GET") {
        
$sql "SELECT id FROM webusers WHERE id != ? ORDER BY RAND() LIMIT 1";
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute([$_SESSION['id']]);
        
$result $stmt->fetch(PDO::FETCH_ASSOC);

        
$id $result['id'];
        
$_SESSION['currentId'] = $id;
       
        
$sql "SELECT webusers.fName, webusers.lName, webusers.bio, webusers.city, interests.interest, pictures.picture
        FROM webusers
        INNER JOIN pictures ON webusers.id = pictures.userId
        INNER JOIN interests ON webusers.id = interests.userId
        WHERE webusers.id = ?"
;
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute([$id]);
        
$user $stmt->fetch(PDO::FETCH_ASSOC);
      }
      else {
        
$id $_SESSION['currentId'];
        
$sql "SELECT webusers.fName, webusers.lName, webusers.bio, webusers.city, interests.interest, pictures.picture
        FROM webusers
        INNER JOIN pictures ON webusers.id = pictures.userId
        INNER JOIN interests ON webusers.id = interests.userId
        WHERE webusers.id = ?"
;
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute([$id]);
        
$user $stmt->fetch(PDO::FETCH_ASSOC);
      }
    }
    catch(
PDOException $e)  {
        echo 
$sql "<br>" $e->getMessage();
    }
?>
  <div class="profile-container">
    <img src="<?php echo htmlspecialchars($user['picture']); ?>" alt="Profilbild" class="profile-img">
    <div class="profile-name"><?php echo "<a href='profile.php?id=".$id."'>".htmlspecialchars($user['fName'], ENT_QUOTES'UTF-8')." ".htmlspecialchars($user['lName'])."</a>"?></div>
    <div class="profile-text"><?php echo $user['bio']; ?></div>
    <div class="profile-text"><strong>Intressen:</strong> <?php echo $user['interest']; ?></div>
    <div class="profile-city"><?php echo $user['city']; ?></div>
    <div class="buttons">
      <form method="post" action=""> 
        <button class="button" name="next">Nästa</button>
        <button class="button" name="add">
          <?php 
          $sql 
"SELECT COUNT(*) FROM friends 
          WHERE user1Id = ? AND user2Id = ?"
;
          
$stmt $dbconn->prepare($sql);
          
$stmt->execute([$_SESSION['id'], $id]);
          
$count1 $stmt->fetchColumn();

          
$sql "SELECT COUNT(*) FROM friends 
          WHERE user1Id = ? AND user2Id = ?"
;
          
$stmt $dbconn->prepare($sql);
          
$stmt->execute([$id$_SESSION['id']]);
          
$count2 $stmt->fetchColumn();
          
$count $count1 $count2;

          if (
$count == 0) {
            if (
$_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add'])) {
              
$sql "INSERT INTO friends (user1Id, user2Id, status) 
              VALUES (?, ?, ?)"
;
              
$stmt $dbconn->prepare($sql);
              
$data = array($_SESSION['id'], $id'pending');
              
$stmt->execute($data);
    
              echo 
"Skickat vänförfrågan";
            } else {echo 
"Lägg till";}
          }
          else {
            
$sql "SELECT * FROM friends WHERE (user1Id = ? AND user2Id = ?) OR (user1Id = ? AND user2Id = ?)";
            
$stmt $dbconn->prepare($sql);
            
$stmt->execute([$_SESSION['id'],$id$id$_SESSION['id']]);
            
$friend $stmt->fetch(PDO::FETCH_ASSOC);
            if (
$count1==1) {
              echo 
$friend['status'];
            }
            elseif (
$count2){
              if (
$_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add']) && $friend['status'] = "pending") {
                
$sql "UPDATE friends SET status = ? 
                WHERE user1Id = ? AND user2Id = ?"
;
                
$stmt $dbconn->prepare($sql);
                
$stmt->execute(['friends'$id$_SESSION['id']]);
      
                echo 
"ni är vänner";
              } elseif (
$friend['status'] == "friends") {
                echo 
$friend['status'];
              } else {echo 
"acceptera vänförfrågan";}
            }
          }
?>
        </button>
      </form>
    </div>
  </div>
<?php
  $dbconn 
null;
  include (
'footer.php');
?>
<script>
  window.addEventListener("beforeunload", () => {
    localStorage.setItem("scrollPosition", window.scrollY);
  });

  window.addEventListener("load", () => {
    const savedScroll = localStorage.getItem("scrollPosition");
    if (savedScroll !== null) {
      window.scrollTo(0, parseInt(savedScroll));
    }
  });
</script>

</body>
</html>