# Generating a random URL for music



## reneirwolf878 (Feb 17, 2002)

I have a myspace page and use this code...

<p<center
MUSIC URL[/B]]*MUSIC URL*</embed

I removed a few ">"'s and I want to know if it is possible to have a random URL generated in the code so that the same song doesn't always play when a person visits my page.


----------



## Howelly (Sep 22, 2006)

I'm stumpted  I was going down the line of recoding the well known (random image generator) but for mp3 files.

I have done this i think, but whenever i try and embed the file im getting nothing. Anyway ill put the code up here and im sure someone better can figure out whats wrong with it 


```
<?php

   //$dir is the directory music is stored in.
   $dir = "music";
   $handle = @opendir($dir);
   $files=array();
   if ($handle=opendir($dir)) {
       while(false !== ($file = readdir($handle))) { 
                 if ($file != "." && $file != "..") {
                 if(substr($file,-3)=='mp3') $files[count($files)] = $file;
                 }
       }
   }
   closedir($handle);

   $random=rand(0,count($files)-1);
   if(substr($files[$random],-3)=='mp3') header("Content-type: audio/mpeg");
   readfile("$path/$files[$random]");
 ?>
```


----------



## thecoalman (Mar 6, 2006)

reneirwolf878 said:


> I have a myspace page and use this code...
> .


Under the assumption you can use Javascript on MySpace you'll find something here: http://www.codingforums.com/showthread.php?t=50666


----------



## thecoalman (Mar 6, 2006)

@reneirwolf878 , you can't use the following code it requires PHP.



Howelly said:


> I'm stumpted  I was going down the line of recoding the well known (random image generator) but for mp3 files.


I've been using this with success, it will generate a random m3u list each time the page is loaded. It's not my code and I'm not quite sure where I grabbed it from or I'd link to it. Anyhow............

1. First create a directory for the player then two subdirectories.

http://www.yoursite.com/radio/
http://www.yoursite.com/radio/list/
http://www.yoursite.com/radio/files/

2. Upload all your music files to the files directory.

3. Open up notepad or you code editor and place the URL's for each file on a seperate line. Save it as playlist.txt and upload it to http://www.yoursite.com/radio/list/


```
http://www.yoursite.com/radio/files/file1.wma
http://www.yoursite.com/radio/files/file2.mp3
http://www.yoursite.com/radio/files/file3.wma
```
4. Open up notepad and place the following, save as .htaccess and upload to http://www.yoursite.com/radio/list/. You need this so the browser doesn't cache the randomlist for future visits by the user:


```
Header set Expires "Thu, 01 Dec 2003 16:00:00 GMT"
Header set Cache-Control "no-store, no-cache, must-revalidate"
Header set Pragma "no-cache"
```
5. The code for the player itself, it generates a new file each time the page is loaded. Upload to http://www.yoursite.com/radio/.

<?php
$list = array(); 
$handle = fopen("list/playlist.txt","r"); 
$i = 0;
while (!feof($handle))
{
$temp=fgets($handle);
$list[$i]=$temp;
$i++;
}
shuffle($list);
$z=0;
fclose($handle);
$handle=fopen("list/randomlist.m3u","w");
while ($z < $i)
{
$temp=$list[$z];
fputs($handle,$temp);
$z++;
}
fclose($handle);
?>

list/randomlist.m3u


----------



## covert215 (Apr 22, 2006)

thecoalman said:


> Under the assumption you can use Javascript on MySpace you'll find something here: http://www.codingforums.com/showthread.php?t=50666


It is not allowed.


----------



## Howelly (Sep 22, 2006)

Theres gotta be a way, with the random image generator you could store your random image on 1 server:

ie http://www.server1.com/randomimage.php

then you could call it on the 2nd server:


```
<img src="http://www.server1.com/randomimage.php" />
```
So surely the same should can be with a random mp3


----------



## reneirwolf878 (Feb 17, 2002)

@ thecoalman

I will try your instructions later tonight after I get back. All I've ever really known is HTML and that knowledge is not too advanced. Myspace allows me to *"enter HTML/DHTML or CSS in any text field. Javascript is not allowed. "* So I don't know if php will work. I'll try out your post tonight and post what happens. Until then...anyone else have any ideas or know if thecoalman's suggestion will work?

And myspace does not allow the OBJECT tag, only EMBED, would replacing object with embed ruin the code?


----------



## thecoalman (Mar 6, 2006)

reneirwolf878 said:


> @ thecoalman
> 
> I will try your instructions later tonight after I get back.


As i said: *@reneirwolf878 , you can't use the following code it requires PHP.*

PHP is server side scripting language, you would need your own host.


----------



## thecoalman (Mar 6, 2006)

Howelly said:


> ```
> <img src="http://www.server1.com/randomimage.php" />
> ```
> So surely the same should can be with a random mp3


I haven't tested it but you might be able to do it with the script above by altering it. But you need your ownserver to host the PHP file to begin with.


----------

