Forum Moderators: coopster

Message Too Old, No Replies

passing a variable from cookie into php

         

eatspinach

3:32 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



Hi

i have a flash audio player embedded on my website next to a file upload section, so what is supposed to happen is the user uploads a file, this sets the cookie audioFileOne to the location of the file ie. audio/audio-1.mp3 then presses play to preview the audio.

I think to explain my problem i need to show you my code,

<script language="JavaScript" src="audio/audio-player.js"></script>
<object type="application/x-shockwave-flash" data="audio/player.swf" id="audioplayer1" height="24" width="290">
<param name="movie" value="audio/player.swf">
<param name="FlashVars" value="playerID=1&amp;soundFile=<?php echo $audioFileOne; ?>">
<param name="quality" value="high">
<param name="menu" value="false">
<param name="wmode" value="transparent">
</object>

So the part that i am cant get working is this part soundFile=<?php echo $audioFileOne; ?>"> because when the page loads the variable $audioFileOne is blank.

does anyone know how i could fix this?

thanks for any help in advance

PokeTech

4:25 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



Have you set the variable yet or is that the first time you use it?

To get a cookie: $_COOKIE['name']
Session: $_SESSION['name']
more: $_GET['name'], $_POST['name']

Those are some ways you can retrieve a variable you are trying to pass between pages. If your using cookies just replace: <?php echo $_COOKIE['audioFileOne']; ?> or whatever your cookie is called.

It might be a little easier if you do this: <?=$_COOKIE['name']?> just makes it a little shorter nothing different really.

[edited by: PokeTech at 4:26 pm (utc) on June 22, 2009]

eatspinach

10:27 am on Jun 23, 2009 (gmt 0)

10+ Year Member



No that's my problem the variable isn't set until the after the page loads, if i view the html source this is what i am getting..

<script language="JavaScript" src="audio/audio-player.js"></script>

<object type="application/x-shockwave-flash" data="audio/player.swf" id="audioplayer1" height="24" width="290">
<param name="movie" value="audio/player.swf">
<param name="FlashVars" value="playerID=1&amp;soundFile=<?=$_COOKIE['audioFileOne']?>">
<param name="quality" value="high">
<param name="menu" value="false">
<param name="wmode" value="transparent">
</object>

so the problem here is not so clear to me?

eatspinach

10:37 am on Jun 23, 2009 (gmt 0)

10+ Year Member



Actually I made a mistake with that post, for some reason this "<?" has to be "<?php" so i did that and now i am getting

Notice: Undefined index: audioFileOne in C:\Program Files\EasyPHP 3.0\www\sentientmis\questionInfo\question1.php on line 70

jatar_k

1:14 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



looks like you have the wrong var

you can use this to take a little look at what is actually in the cookie

echo '<pre>';
print_r($_COOKIE);
echo '</pre>';

eatspinach

1:48 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



ahh thank you what a handy piece of code..

when i do that i am getting,

Array
(
[ques1] => One
[ques2] => two
[ques3] => three
[ques4] => four
[PHPSESSID] => ee8b4e30e3c311c9b69dfc7eaff95fb6
)

which is strange because i have definitely set the cookie using setcookie("audioFileOne", $cookie);

jatar_k

2:21 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> handy piece of code..

works for $_SESSION, $_POST, $_GET or any other array ;)

odds are then the cookie isn't being set properly

what's in this var - $cookie - though I imagine it's a fulename

maybe extand your setcookie to something like

setcookie("TestCookie", $cookie, time()+3600, "/", ".example.com");

it may make a difference, obviously change all the values to your own

eatspinach

2:39 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



i shall definitely be using that piece of code again.. I tried your test cookie but still no luck, i think the problem is because the cookie is getting set in this way, first i have a form..

<form action="upload/upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
<input type="hidden" name="AudioFile-1" value=""/>
<p id="f1_upload_process"><!--Loading...--><br/><img src="loader.gif" /></p>
<p id="f1_upload_form" align="center">

<input name="myfile" type="file" size="30" />

<label>
<input type="submit" name="submitBtn" class="sbtn" value="Upload" />
</label>
</p>

<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>

then that form posts to this page upload.php where i set the cookie..

<?php

include("../include/globals.php");

// Edit upload location here
//$destination_path = getcwd().DIRECTORY_SEPARATOR;
$destination_path = "../audio/";

$result = 0;

$target_path = $destination_path . basename( $_FILES['myfile']['name']);

if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;

setcookie("audioFileOne", $target_path);
setcookie("TestCookie", $cookie, time()+3600, "/", ".example.com");
}

sleep(1);

?>

<script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result; ?>);</script>

i have this

echo '<pre>';
print_r($_COOKIE);
echo '</pre>';

on the page and i am not seeing the new cookie value come in at all..

jatar_k

2:50 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



since you already have a session cookie you are obviously using a session, why not just put that info into the session?

eatspinach

3:14 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



ok to do a little testing i tried putting this line at the top of the page...

$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views'];

this returned Pageviews = 1 at the top of the page but when i put the line

$_SESSION['views'] = 1; // store session data

into my usual spot "upload.php" page and put this line into the main page

echo "Pageviews = ". $_SESSION['views'];

I get this error Notice: Undefined variable: _SESSION in C:\Program Files\EasyPHP 3.0\www\sentientmis\survey-step2.php on line 71

this is getting fairly frustrating :)

jatar_k

3:46 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



seems things are getting a little loose too ;)

alright back to the beginning, you are trying to capture the filename to pass it to the display page

can you capture the filename/path?
is the player code you showed above in the same script or is it on another page?

eatspinach

4:18 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



hooray i think i got this baby working, i took your advice on putting things in the same page and in the one directory and i did a few changes and i seem to be able to set and change the cookie value, so i am only half way there, i will fix up my code and post a proper reply in the morning, cheers dude's

eatspinach

8:48 am on Jun 24, 2009 (gmt 0)

10+ Year Member



Yes it seems like the problem was resolved by putting all the files in the one directory. Which makes the project a little messier but its ok.. Thanks for all your help, very much appreciated.