Skip to content

Instantly share code, notes, and snippets.

@Minetristeru777
Created April 1, 2025 13:30
Show Gist options
  • Select an option

  • Save Minetristeru777/47094e98d93fc01f00eebc977e473aea to your computer and use it in GitHub Desktop.

Select an option

Save Minetristeru777/47094e98d93fc01f00eebc977e473aea to your computer and use it in GitHub Desktop.
Code
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['video'])) {
$title = $_POST['title'];
$description = $_POST['description'];
$video_file = $_FILES['video'];
// Validate video file
$allowed_types = ['video/mp4', 'video/webm', 'video/ogg'];
if (in_array($video_file['type'], $allowed_types)) {
$upload_dir = 'uploads/videos/';
// Check if the upload directory exists, if not, create it
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true); // Create the directory with proper permissions
}
$video_path = $upload_dir . basename($video_file['name']);
// Move uploaded file to the server
if (move_uploaded_file($video_file['tmp_name'], $video_path)) {
// Insert video information into the database
$stmt = $conn->prepare("INSERT INTO videos (user_id, title, description, video_path) VALUES (?, ?, ?, ?)");
$stmt->bind_param("isss", $user_id, $title, $description, $video_path);
$stmt->execute();
header('Location: dashboard.php'); // Reload the page to show new video
} else {
echo "Error uploading video: " . error_get_last()['message']; // Show error details
}
} else {
echo "Invalid video file type.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Dashboard</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
.header {
background-color: #333;
color: white;
padding: 10px;
text-align: center;
}
.content {
padding: 20px;
}
.video-list {
list-style-type: none;
padding: 0;
}
.video-item {
background-color: white;
margin: 10px 0;
padding: 10px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.video-item h3 {
margin-top: 0;
}
.logout-btn {
padding: 10px;
background-color: #f44336;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
}
.logout-btn:hover {
background-color: #d32f2f;
}
.upload-form {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
.upload-form input,
.upload-form textarea {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
border: 1px solid #ccc;
}
.upload-form button {
background-color: #007bff;
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.upload-form button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="header">
<h1>Welcome, <?php echo htmlspecialchars($user['username']); ?>!</h1>
</div>
<div class="content">
<!-- Upload Video Form -->
<div class="upload-form">
<h2>Upload New Video</h2>
<form method="POST" enctype="multipart/form-data">
<input type="text" name="title" placeholder="Video Title" required>
<textarea name="description" placeholder="Video Description" required></textarea>
<input type="file" name="video" accept="video/*" required>
<button type="submit">Upload Video</button>
</form>
</div>
<h2>Your Videos</h2>
<ul class="video-list">
<?php foreach ($videos as $video): ?>
<li class="video-item">
<h3><?php echo htmlspecialchars($video['title']); ?></h3>
<video width="320" height="240" controls>
<source src="<?php echo htmlspecialchars($video['video_path']); ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
<p><?php echo htmlspecialchars($video['description']); ?></p>
<a href="video.php?id=<?php echo $video['id']; ?>">Watch Video</a>
</li>
<?php endforeach; ?>
</ul>
<button class="logout-btn" onclick="window.location.href='logout.php'">Logout</button>
</div>
</body>
</html>
<?php
$servername = "servername"; // Адреса сервера MySQL (на хостингу це може бути інше)
$username = "Username"; // Ваш логін MySQL
$password = "password"; // Ваш пароль MySQL
$dbname = "dbname"; // Назва вашої бази даних
// Створення підключення
$conn = new mysqli($servername, $username, $password, $dbname);
// Перевірка підключення
if ($conn->connect_error) {
// Логування помилки (для розробки можна записати в файл або вивести)
error_log("Connection failed: " . $conn->connect_error);
die("Підключення не вдалося: " . $conn->connect_error);
}
// Встановлення кодування UTF-8 для роботи з текстом
if (!$conn->set_charset("utf8")) {
error_log("Error loading character set utf8: " . $conn->error);
die("Помилка при встановленні кодування UTF-8: " . $conn->error);
}
?>
<?php
session_start();
require_once 'db.php';
// Check if user is logged in
if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
echo "<h1>Welcome, $username!</h1>";
} else {
echo "<h1>Welcome to vUKRideo!</h1>";
}
?>
<!-- Login and Registration Links -->
<?php if (!isset($_SESSION['username'])): ?>
<p><a href="login.php">Login</a> | <a href="register.php">Register</a></p>
<?php else: ?>
<p><a href="logout.php">Logout</a></p>
<?php endif; ?>
<!-- Display Videos Section -->
<h2>Latest Videos</h2>
<?php
$videos = [];
$result = $conn->query("SELECT * FROM videos ORDER BY created_at DESC LIMIT 5");
while ($row = $result->fetch_assoc()) {
$videos[] = $row;
}
?>
<?php foreach ($videos as $video): ?>
<div class="video-item">
<h3><?php echo htmlspecialchars($video['title']); ?></h3>
<video class="video-player" width="320" height="240" controls>
<source src="<?php echo htmlspecialchars($video['video_path']); ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
<p><?php echo htmlspecialchars($video['description']); ?></p>
<a class="video-link" href="video.php?id=<?php echo $video['id']; ?>">Watch Video</a>
</div>
<?php endforeach; ?>
<!-- Add Style -->
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h1, h2, h3 {
color: #333;
}
h1 {
font-size: 2em;
margin-bottom: 10px;
}
h2 {
font-size: 1.5em;
margin-top: 20px;
}
.video-item {
background-color: white;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
padding: 20px;
transition: transform 0.3s ease-in-out;
}
.video-item:hover {
transform: scale(1.05);
}
.video-player {
border-radius: 8px;
margin-top: 10px;
max-width: 100%;
}
.video-link {
display: inline-block;
margin-top: 10px;
color: #007bff;
text-decoration: none;
font-weight: bold;
}
.video-link:hover {
text-decoration: underline;
}
p {
color: #555;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
<?php
session_start();
require_once 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'];
$password = $_POST['password'];
// Check if user exists
$stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header('Location: index.php');
} else {
echo "<p class='error'>Incorrect email or password!</p>";
}
}
?>
<form method="POST" class="login-form">
<h2>Login</h2>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>
<!-- Add Style -->
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 40px;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.login-form {
background-color: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
.login-form h2 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.login-form input {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
.login-form button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.login-form button:hover {
background-color: #0056b3;
}
.error {
color: red;
text-align: center;
font-size: 14px;
margin-top: 10px;
}
input:focus {
outline: none;
border-color: #007bff;
}
</style>
<?php
session_start();
require_once 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
// Check if the email is already registered
$stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
echo "<p class='error'>Email is already registered!</p>";
} else {
// Insert the new user into the database
$stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $email, $password);
$stmt->execute();
$_SESSION['username'] = $username;
$_SESSION['user_id'] = $conn->insert_id;
header('Location: index.php');
}
}
?>
<form method="POST" class="register-form">
<h2>Register</h2>
<input type="text" name="username" placeholder="Username" required>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Register</button>
</form>
<!-- Add Style -->
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 40px;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.register-form {
background-color: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
.register-form h2 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.register-form input {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
.register-form button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.register-form button:hover {
background-color: #0056b3;
}
.error {
color: red;
text-align: center;
font-size: 14px;
margin-top: 10px;
}
input:focus {
outline: none;
border-color: #007bff;
}
</style>
<?php
session_start();
require_once 'db.php';
if (!isset($_GET['id'])) {
die('Video not found.');
}
$video_id = $_GET['id'];
// Load the video
$stmt = $conn->prepare("SELECT videos.*, users.username AS uploader FROM videos JOIN users ON videos.user_id = users.id WHERE videos.id = ?");
$stmt->bind_param("i", $video_id);
$stmt->execute();
$video = $stmt->get_result()->fetch_assoc();
if (!$video) {
die('Video not found.');
}
// Handle like/dislike
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['like'])) {
$user_id = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT * FROM likes WHERE video_id = ? AND user_id = ?");
$stmt->bind_param("ii", $video_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Toggle like status
$stmt = $conn->prepare("UPDATE likes SET liked = !liked WHERE video_id = ? AND user_id = ?");
$stmt->bind_param("ii", $video_id, $user_id);
$stmt->execute();
} else {
// Add like
$stmt = $conn->prepare("INSERT INTO likes (video_id, user_id, liked) VALUES (?, ?, ?)");
$stmt->bind_param("iii", $video_id, $user_id, 1);
$stmt->execute();
}
}
// Handle comment
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comment'])) {
$comment = $_POST['comment'];
$stmt = $conn->prepare("INSERT INTO comments (video_id, user_id, comment) VALUES (?, ?, ?)");
$stmt->bind_param("iis", $video_id, $_SESSION['user_id'], $comment);
$stmt->execute();
}
// Get comments
$comments = [];
$stmt = $conn->prepare("SELECT comments.*, users.username AS commenter FROM comments JOIN users ON comments.user_id = users.id WHERE video_id = ?");
$stmt->bind_param("i", $video_id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$comments[] = $row;
}
?>
<h2><?php echo htmlspecialchars($video['title']); ?></h2>
<video class="video-player" width="640" height="360" controls>
<source src="<?php echo htmlspecialchars($video['video_path']); ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
<p>Uploaded by: <?php echo htmlspecialchars($video['uploader']); ?></p>
<p><?php echo htmlspecialchars($video['description']); ?></p>
<!-- Like / Dislike -->
<form method="POST" class="like-form">
<button type="submit" name="like" class="like-button">Like</button>
</form>
<!-- Comments -->
<form method="POST" class="comment-form">
<textarea name="comment" placeholder="Your comment" required class="comment-textarea"></textarea>
<button type="submit" class="comment-button">Post Comment</button>
</form>
<h3>Comments</h3>
<div class="comments-section">
<?php foreach ($comments as $comment): ?>
<p><strong><?php echo $comment['commenter']; ?>:</strong> <?php echo htmlspecialchars($comment['comment']); ?></p>
<?php endforeach; ?>
</div>
<!-- Add Style -->
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h2 {
color: #333;
text-align: center;
}
.video-player {
display: block;
margin: 20px auto;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
p {
color: #555;
text-align: center;
}
.like-form, .comment-form {
display: flex;
justify-content: center;
margin: 20px 0;
}
.like-button, .comment-button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.like-button:hover, .comment-button:hover {
background-color: #0056b3;
}
.comment-textarea {
width: 80%;
padding: 10px;
font-size: 14px;
border-radius: 5px;
border: 1px solid #ccc;
margin-right: 10px;
resize: vertical;
}
.comments-section p {
padding: 8px;
background-color: white;
margin-bottom: 10px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
h3 {
text-align: center;
color: #333;
}
</style>
<?php
session_start();
require_once 'db.php';
if (!isset($_GET['id'])) {
die('Video not found.');
}
$video_id = $_GET['id'];
// Load the video
$stmt = $conn->prepare("SELECT videos.*, users.username AS uploader FROM videos JOIN users ON videos.user_id = users.id WHERE videos.id = ?");
$stmt->bind_param("i", $video_id);
$stmt->execute();
$video = $stmt->get_result()->fetch_assoc();
if (!$video) {
die('Video not found.');
}
// Handle like/dislike
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['like'])) {
$user_id = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT * FROM likes WHERE video_id = ? AND user_id = ?");
$stmt->bind_param("ii", $video_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Toggle like status
$stmt = $conn->prepare("UPDATE likes SET liked = !liked WHERE video_id = ? AND user_id = ?");
$stmt->bind_param("ii", $video_id, $user_id);
$stmt->execute();
} else {
// Add like
$stmt = $conn->prepare("INSERT INTO likes (video_id, user_id, liked) VALUES (?, ?, ?)");
$stmt->bind_param("iii", $video_id, $user_id, 1);
$stmt->execute();
}
}
// Handle comment
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comment'])) {
$comment = $_POST['comment'];
$stmt = $conn->prepare("INSERT INTO comments (video_id, user_id, comment) VALUES (?, ?, ?)");
$stmt->bind_param("iis", $video_id, $_SESSION['user_id'], $comment);
$stmt->execute();
}
// Get comments
$comments = [];
$stmt = $conn->prepare("SELECT comments.*, users.username AS commenter FROM comments JOIN users ON comments.user_id = users.id WHERE video_id = ?");
$stmt->bind_param("i", $video_id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$comments[] = $row;
}
?>
<h2><?php echo htmlspecialchars($video['title']); ?></h2>
<video class="video-player" width="640" height="360" controls>
<source src="<?php echo htmlspecialchars($video['video_path']); ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
<p>Uploaded by: <?php echo htmlspecialchars($video['uploader']); ?></p>
<p><?php echo htmlspecialchars($video['description']); ?></p>
<!-- Like / Dislike -->
<form method="POST" class="like-form">
<button type="submit" name="like" class="like-button">Like</button>
</form>
<!-- Comments -->
<form method="POST" class="comment-form">
<textarea name="comment" placeholder="Your comment" required class="comment-textarea"></textarea>
<button type="submit" class="comment-button">Post Comment</button>
</form>
<h3>Comments</h3>
<div class="comments-section">
<?php foreach ($comments as $comment): ?>
<p><strong><?php echo $comment['commenter']; ?>:</strong> <?php echo htmlspecialchars($comment['comment']); ?></p>
<?php endforeach; ?>
</div>
<!-- Add Style -->
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h2 {
color: #333;
text-align: center;
}
.video-player {
display: block;
margin: 20px auto;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
p {
color: #555;
text-align: center;
}
.like-form, .comment-form {
display: flex;
justify-content: center;
margin: 20px 0;
}
.like-button, .comment-button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.like-button:hover, .comment-button:hover {
background-color: #0056b3;
}
.comment-textarea {
width: 80%;
padding: 10px;
font-size: 14px;
border-radius: 5px;
border: 1px solid #ccc;
margin-right: 10px;
resize: vertical;
}
.comments-section p {
padding: 8px;
background-color: white;
margin-bottom: 10px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
h3 {
text-align: center;
color: #333;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment