I just finished making this quick and dirty random YouTube video player for fun and learning. Before I get started, by "random" I mean selecting a video from a list of videos using PHP RNG, not truly random and not random as in "any video from YouTube". It also uses the YouTube API to get information like the video title from just the ID and accepts inputs of the entire YouTube video URL or just the ID. I want to know if anything I did in this is a bad practice or if there are better ways of doing the things I did because I am not a professional PHP coder and I want to start coding with good habits so later I am not making mistakes.
Here is the github repo: https://github.com/desultory/RandomYoutubeVideo
here is the code for each component: config.php:
<?php
$ytapikey= 'your youtube API key here';
edit.php:
<html>
<body>
<div>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<h1>Input video ID of a video to add to the list<h1>
<input type="text" name="videoid" placeholder="Video ID of video to add" autofocus>
<br><br>
<h1>Select a video to remove<h1>
<select name="remove">
<option>None</option>
<?php
//checks to see if the file exists before continuing
if (file_exists('videos')) {
//reads and unserializes the videos file
$videos = unserialize(file_get_contents('videos'));
//loops through the videos array to get components of each video
foreach($videos as $element) {
$videoid = $element[0];
$videotitle = $element[1];
//formats the video details for html
echo "\t\t<option value=\"$videoid\">$videotitle</option>";
}
}
?>
</select>
<br><br>
<input type="submit" name="submit" text="Update list"/>
</form>
</div>
</body>
</html>
<?php
//includes the config to get youtube api token
include 'config.php';
//checks to see if the video id is set and not empty
if (isset($_POST['videoid']) && $_POST['videoid'] !== "") {
//gets video id from post
$videoid = $_POST['videoid'];
//checks to see if the video id contains the string youtube so it can extract the video id
if (stripos($videoid, 'youtube') !== FALSE) {
//parses the string to get query information
parse_str(parse_url($videoid, PHP_URL_QUERY), $videoquery);
//sets the video ID to be just the v query which is the video id
$videoid = $videoquery['v'];
}
//gets the title snippet using the youtube api
$videoinfo = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet&id='.$videoid.'&key='.$ytapikey.'&fields=items(snippet(title))'));
//sets videotitle to be just the title portion of the snippet
$videotitle = $videoinfo->items[0]->snippet->title;
//checks to see if videos exists so it can read it and then concatenate it
if (file_exists('videos')) {
//reads and unserializes the videos file
$videos = unserialize(file_get_contents('videos'));
//adds a new element containing the new video id and title
$videos[count($videos)] = [$videoid, $videotitle];
} else {
//makes a new array for videos and adds the video id and title
$videos[0] = [$videoid, $videotitle];
}
//checks to see if the video is valid based on the title found before writing
if ($videotitle !== NULL) {
//serializes and writes new video file
file_put_contents('videos', serialize($videos));
//refreshes the page
header("Refresh:0");
} else {
echo 'Invalid video';
}
}
//checks to see if remove is set and isn't none
if (isset($_POST['remove']) && $_POST['remove'] !== "None") {
//gets the video id to be removed from the post
$videoid = $_POST['remove'];
//reads and unserializes the videos file
$videos = unserialize(file_get_contents('videos'));
//sets the counter to 0
$counter = 0;
//loops through the videos array
foreach($videos as $element) {
//checks to see if the element contains the video ID to be removed
if(strpos($element[0], $videoid) !== FALSE) {
//Removes the array element that contains that video ID
array_splice($videos, $counter, 1);
}
//increments the counter
$counter++;
}
//serializes and writes new videos file
file_put_contents('videos', serialize($videos));
}
?>
index.php:
<?php
//checks to see if video file exists
if (file_exists('videos')) {
//reads and unserializes the videos file
$videos = unserialize(file_get_contents('videos'));
//selects a random video and gets the ID
$videoid = $videos[array_rand($videos)][0];
} else {
echo 'No video list found, if you have not configured it, go to edit.php';
}
?>
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<head>
<title>Infinite Random Videos</title>
</head>
<style>
html, body{
margin:0;
padding:0;
height: 100%;
width: 100%;
}
.video{
overflow: hidden;
position: abslute;
width: 100%;
height: 100%;
}
</style>
<body>
<div class="video">
<iframe width="100%" height="100%" frameborder="0" src="https://youtube.com/embed/<?php echo $videoid;?>?autoplay=1&loop=1&controls=0&disablekb=1&fs=0&iv_load_policy=3&rel=0&showinfo=0&modestbranding=1"></iframe>
</div>
</body>
</html>
videos:
a:4:{i:0;a:2:{i:0;s:11:"HdGf0NhJ6Wc";i:1;s:17:"Learning to Yo-yo";}i:1;a:2:{i:0;s:11:"3AIInqUwQE8";i:1;s:16:"Learning to Swim";}i:2;a:2:{i:0;s:11:"gQXUQRFNhUw";i:1;s:12:"Making Toast";}i:3;a:2:{i:0;s:11:"UlFPsVu8b5Y";i:1;s:8:"Pet Rock";}}
Note:
I could use SQL as a storage method but I want this to be "portable" and to be able to work on a weberver without SQL access