Adam
Adam

Reputation: 933

Maintaining $_SESSION data while making ajax requests through jquery in PHP?

I'm on the last leg of a decent project (for me at least) and I'm running into an issue where my ajax requests aren't sending $_SESSION data to the URLs they are loading. I'm trying to automate the upload of files you YouTube, using Gdata. I make an ajax request using jquery but when I test for $_SESSION['sessionToken'] in my ajax called PHP script, I get nothing.

Here's my code, that's calling the YouTube upload script:

function uploadVideos(id, upload, selected) {
        var status = document.getElementById('currentStatus');
        var fields = upload[id].split(":", 2);

        var token="<?php echo $_GET['token'];?>";
        var dataString = 'videoId='+ fields[0]; // + '&token=' + token;

        id++;
        status.innerHTML = "<b>Videos for Upload:</b>&nbsp;&nbsp;&nbsp; <h3 class='status'>Currently Updating Bout #" + fields[1] + " (" + id + " of " + upload.length + " videos)</h3>";

        $.ajax({  
            type: "GET",  
            url: "upload.php",  
            data: dataString,
            success: function(txt) {
                if (txt != 'Success') {
                    alert(txt);
                } 

                if (id < upload.length) {
                    uploadVideos(id, upload, selected);

                } else {

                    status.innerHTML = "<b>Videos for Upload:</b>&nbsp;&nbsp;&nbsp; <h3 class='status'>Completed</h3>";
                }

                //deselect the checkbox
                document.videos.video[selected[id-1]].checked = false;
                document.videos.video[selected[id-1]].style.display = 'none';
            },
            async: true

        }); 
}

How can I send sessionToken along to upload.php, to it believes I'm authenticated?

Upvotes: 2

Views: 981

Answers (1)

Christian
Christian

Reputation: 436

First: You are not sending the "token"-variable with your Ajax call. You are sending the variable dataString which does not include the token variable. You have commented the token part out...

var token="<?php echo $_GET['token'];?>"; 
var dataString = 'videoId='+ fields[0]; // + '&token=' + token;

Second: You can only send Ajax with POST or GET

in your ajax call you are using GET, so you should check for $_GET; Not $_SESSION.

Upvotes: 2

Related Questions