1.11M Members

Help with redirect type thing

 
0
 

I'm thinking of hosting a name that tune type game.

I don't know anything about PHP or even if it's the languge to use, but I'm hoping someone
might put me straight on that matter too.

Here is an outline of my needs/scenario.
I post a link to a song.mp3 I have uploaded to my host, the user has to guess the song and artist.
The problem is, as I'm sure most here will have immediately envisioned is the link to the song title
will be visible to anyone with some knowledge of how this sort of thing works.
I could change the title and meta data of the file, but that seems overkill.
I want to make a link to say Guess.mp3, but when that link is clicked it serves up an entirely different
Song.mp3 which is determined by a php script which cannot be seen or found without a fair bit of jiggery pokery.

I hope you follow what I mean and can advise if such a thing is even possible.
My hopes are that the link can be posted not on my own website, but for instance here at daniweb.
Any advice or suggestions of other methods are very welcome and much appreciated.

 
0
 

You can point the audio to a PHP file (guess.php). It can return anything you want. If you set the right headers it can return an audio file too.

 
0
 

Any pinters as to how to do that?
If I put http://www.domian.com/guess.php in [media][/media] tags it just shows the url.

 
0
 
<audio controls autoplay>
    <source src="guess.php" type="audio/mpeg" />
    Your browser does not support the audio element.
</audio>

Then your guess.php has to read and return an mp3 file with the right headers.

 
0
 

Thanks for your time.

I have this in my guess.php file

//http://www.longtailvideo.com/support/forums/jw-player/setup-issues-and-embedding/3819/serving-a-mp3-file-with-php/

<?php
$fc = file_get_contents("NewMp3.mp3");
$size = filesize("NewMp3.mp3");
header('Content-type: audio/mpeg');
header('Content-length: ' . $size);
header('Content-Disposition: filename=NewMp3.mp3');
header('X-Pad: avoid browser bug');
Header('Cache-Control: no-cache');
echo $fc;
?>

If I navigate to the page http://www.domian.com/guess.php then a media player shows and the song plays. But I need the same to happen withing media tags on another website/forum.

 
0
 

I can't try right now, but any media method you use that accepts an url, should also accept your php script.

 
0
 

Hi Suzie99,

Can you add the content transfer encoding field and tell it's binary.
Content-transfer-encoding: binary
header('Content-transfer-encoding: binary')
Let me know what happened.

 
0
 

Hi gon1387, Thank you for reply, unfortunately there is no change.
Does it matter the order of header values?

 
0
 

No actually, unless you put the contents on top. LOL :D
Anyway joking asside. I've seen this one, I think it's the content type.
Q&A

Anyway, I;ve checked your script, you got a capitalized header func:

me=NewMp3.mp3');
header('X-Pad: avoid browser bug');
Header('Cache-Control: no-cache'); \\make the 'H' lower-case
 
0
 

I noticed that earlier and changed it, no luck :(
(edit)It does not seem to matter, I just tried them all capitalised, without change.

 
0
 

I was checking on the different encoding for MP3 and content-types. Any luck with the mime types in their solution?

 
0
 

Make it all small-case. PHP Header Function

 
0
 

Hi, I tried without success.
I also read in that article that nothing should precede header, so had to remove filesize
Code now looks like this, but still no good.

<?php
header('Content-type: audio/mpeg');
header('Content-Disposition: filename=NewMp3.mp3');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
header('Content-transfer-encoding: binary');
$fc = file_get_contents("NewMp3.mp3");
echo $fc;
?>
 
0
 

Actually, this method might not be very good, because if I navigate to the php page in browser and view source, the meta data is the first thing to see at the top with artist and title.

:(

 
0
 

What it's trying to say with "No output before the header" is there should be data to be output in the stream befor ethe header. When the HTML tag was included above, and before the '<?php' it already send a set of character to the output stream, which is the "<html>".

So the reason behind this is when you send the out put using the HTTP protocol(not in the context of PHP) it would look like this:

<html>
Location: http://www.example.com/
... other http fields ...

Which means, if read by the reciever is a malformed HTTP. Another way of expressing the sample in the PHP Manual is this:

<?php
echo "<html>";
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
?>

So what you did earlier is right. You have to put the length especially if it's a get request, so the reciever, which is the browser would know when to stop listening for incoming data.

Anyhow, here's the sample code from SO, using your code:

<?php
$fc = file_get_contents("NewMp3.mp3");
$size = filesize("NewMp3.mp3");
header("Content-Transfer-Encoding: binary");
header('Content-type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3');
header('Content-length: ' . $size);
header('Content-Disposition: filename="NewMp3.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');

echo $fc;

Let's hope this one works :D

 
0
 

Anyway, it's working on my end. How 'bout yours?

 
0
 

Nope, unless I'm doing something wrong.
I still get the link to it, and also, it does not try to play, and rather wants to download, and my browser dialog opens for me to choose a location.

I have just the php code, with no html at all on the page.

 
0
 

One question though, if you access in your browser.
yourdomain.com/mp3.php

does it download the right mp3 file you specified in your php file?

 
0
 

If you can download the right MP3 file. Then, create an HTML file http://www.w3schools.com/html/html_sounds.asp
CHeck if it plays on your end. be sure to reference the src which is yourdomain.com/mp3.php in the embed tag.

 
0
 

Yes, and plays fine with all meta data in tact.
I really do appreciate the time you have put into your generous assistance.
I'm beginning to think I might as well just remove meta data, and rename files, just seems too much.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article