Jump to content
MakeWebGames

Recommended Posts

Posted (edited)

Whilst collaborating with CrazyElk on their game to include the Video feature Crazyelk wanted it so when Videos where viewed they didnt continuously count up when same user viewed same video so 1 view 1 count per user makes sense...

I was going to add this to the database so when a user views that video their ID is stored so the count wont continue to rise However it dawned on me why not just use a TXT file with the user details of any video they watch the video ID is placed into the TXT file

so heres how I did this...

 

<?PHP
$_GET['view'] = array_key_exists('view', $_GET) && in_array($_GET['view'], ['videoID', 'uid', 'artist', 'tracktitle', 'genre', 'videourl', 'thumbs', 'playerswflink', 'switch', 'allowaccess', 'width', 'height', 'autostart']) ? $_GET['view'] : 'videoID';

// Increment the views
if (isset($_GET['id'])) {
    $videoID = $_GET['id'];
	$path = "videovotes/views/" . $User->id . "/";   
   mkdir($path, 0777, true);

			
			/* end the txt data */
 if ( file_exists("videovotes/views/" . $User->id . "/$videoID.txt") ) {
		
     }
	 else {
		    if ( file_exists("videovotes/views/" . $User->id . "/$videoID.txt") ) {
		
     }
		$omit = fopen("videovotes/views/" . $User->id . "/$videoID.txt", "w");
				fwrite($omit, "
		
");
		fclose($omit);
               $DBO->query("UPDATE `user_videos` SET views = views + 1 WHERE videoID = '$videoID'");
	 }     
	
}

 

The script above adds a folder to the videovotes/views/ folder for the user whos watching a video when a video is watched it leaves a TXT file behind with the video ID on it so if the user views the video again it will track the folder for the user and if the Video is there it wont update the Views..

Ive used the same method for LIKES and DISLIKES of a video you can also use the same method to ban videos example below for ban and unban

 

<?PHP

if ($_GET['b'] == 'ban') {
   if ( file_exists("videovotes/banned/".$VideoID->id.".txt") ) {
		
     }
	 
		$omit = fopen("videovotes/banned/$videoID.txt", "w");
chmod("videovotes/banned/$videoID.txt", 0755);
				fwrite($omit, "<?php
				
				echo'<center>BANNED '".$VideoID->tracktitle."' {$row['tracktitle']}  $tracktitle $videoID</center>';
		
?>");
		fclose($omit);
		
}
if ($_GET['u'] == 'unban') {
	$path = $_SERVER['DOCUMENT_ROOT'].'/videovotes/banned/"'.$videoID.'".txt';
unlink($path);
	unlink('videovotes/banned/"'.$videoID.'".txt');

		
}

 

Hope this helps someone in someway...

 

 

Edited by Uridium
  • Like 1
Posted (edited)

Nice. I use to use this basis as a web hit counter and my last game used it as a click counter. 

I do wonder what uses it could do with enough skill like use it as a kind of dB for users, each user has its own text file. Tho I doubt how secure it could be.

Edited by peterisgb
  • Like 1
Posted

Note: In higher-traffic areas, this may introduce race conditions. In that scenario, I'd recommend using the database (the one your project is already using) to track this information; amongst other things, most RDBMS support query queuing as default.

For low-traffic areas and areas where you don't foresee an upsurge in activity (refer to your metrics), then this file-based approach may be perfectly sufficient for your needs

  • Like 1
Posted

could you not do it much cleaner , personally i would do something more like this , much safer , and cleaner 
 

<?php
// 1. Allowed 'view' values
$allowedViews = ['videoID', 'uid', 'artist', 'tracktitle', 'genre', 'videourl', 'thumbs', 'playerswflink', 'switch', 'allowaccess', 'width', 'height', 'autostart'];
$view = (isset($_GET['view']) && in_array($_GET['view'], $allowedViews, true)) ? $_GET['view'] : 'videoID';

// 2. Require valid user and video
if (!empty($_GET['id']) && isset($User->id)) {
    $videoID = (int) $_GET['id'];  // cast to int for safety
    $userId  = (int) $User->id;

    try {
        // 3. Insert view if not already viewed
        $stmt = $DBO->prepare("
            INSERT INTO video_views (user_id, video_id)
            VALUES (:user_id, :video_id)
            ON DUPLICATE KEY UPDATE user_id = user_id
        ");
        $stmt->execute(['user_id' => $userId, 'video_id' => $videoID]);

        // 4. If a new row was inserted, increment views
        if ($stmt->rowCount() > 0) {
            $updateStmt = $DBO->prepare("
                UPDATE user_videos
                SET views = views + 1
                WHERE videoID = :video_id
            ");
            $updateStmt->execute(['video_id' => $videoID]);
        }
    } catch (PDOException $e) {
        error_log("DB error: " . $e->getMessage());

 

and then just add a simple table to the existing db?

eg
 

CREATE TABLE video_views (
    user_id INT NOT NULL,
    video_id INT NOT NULL,
    PRIMARY KEY (user_id, video_id)



you could even take it further and make it ip specific though which could also be advantageous 
 

Posted
On 8/10/2025 at 8:01 PM, m1ll1k3n said:
ON DUPLICATE KEY UPDATE

Ive never used nor seen that statement before that could come in handy thank you 🙂

 

  • Like 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...