View Full Version : sessions don't work though javascript

12-04-2008, 07:57 PM
This is in another thread but it went off topic

I have a class called session, it is called in header.php, also it is started and session_start() is called.

$session->username seems to be returning "Guest" instead of the actual logged in user, in all the other scripts it works fine...

The ONLY different between this script and others is that it is called by javascript and not through the browser

A compact version of the relevant code:

$user = $session->username;
$session->changeDir('upload/' . $user . '/pictures');

The whole code of Upload.php:

/* Note: This thumbnail creation script requires the GD PHP Extension.
If GD is not installed correctly PHP does not render this page correctly
and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
// Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug

ini_set("html_errors", "0");

// Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
echo "ERROR:invalid upload";
$img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
if (!$img) {
echo "ERROR:could not create image handle ". $_FILES["Filedata"]["tmp_name"];

function resizeImage($img,$target_width,$target_height){
$width = imageSX($img);
$height = imageSY($img);

if (!$width || !$height) {
echo "ERROR:Invalid width or height";
$target_ratio = $target_width / $target_height;

$img_ratio = $width / $height;

if ($target_ratio > $img_ratio) {
$new_height = $target_height;
$new_width = $img_ratio * $target_height;
} else {
$new_height = $target_width / $img_ratio;
$new_width = $target_width;

if ($new_height > $target_height) {
$new_height = $target_height;
if ($new_width > $target_width) {
$new_height = $target_width;

$new_img = ImageCreateTrueColor($target_width, $target_height);
if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) { // Fill the image black
echo "ERROR:Could not fill new image";

if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height)) {
echo "ERROR:Could not resize image";

return $new_img;

// Build the thumbnail
$thumbImage = resizeImage($img,100,100);
$realImage = resizeImage($img,800,600);

if (!isset($_SESSION["file_info"])) {
$_SESSION["file_info"] = array();

$id = md5(100 + rand(1, 10));
$thumbName = 'thumb_'. $id . '.jpg';
$name = 'pic_'. $id . '.jpg';

$code = rand(1, 999999999);
//$user = $session->username;
$user = $session->username;
$sql = "INSERT INTO images (user, file, thumb, folder, code) VALUES ('$user', '$name', '$thumbName', 'root', '$code')";

$result = mysql_query($sql);

// Use a output buffering to load the image into a variable


$session->changeDir('upload/' . $user . '/pictures');

// Create image
imagejpeg($realImage, $name, 60);


// Create thumb
imagejpeg($thumbImage, $thumbName, 60);

// Output thumb
imagejpeg($thumbImage, NULL, 60);


echo "FILEID:" . $code; // Return the file id to the script


in all other pages $session->username; would return the username, in my case, adam.

This is an upload script, the files are uploaded to Guest and not adam.

12-04-2008, 09:38 PM
$user = $session->username;
$sql = "INSERT INTO images (user, file, thumb, folder, code) VALUES ('$user', '$name', '$thumbName', 'root', '$code')";

Maybe I mis-understand what you're doing, but I don't see where $session is defined. You have some kind of sessions interface code included somewhere?

EDIT: Doh! Now I see in the original post where you mentioned it. So that leaves me wondering what you mean by this being called by javascript.

12-04-2008, 10:08 PM
Ok, instead of it being loaded as a page in the browser, it is loaded using Ajax, Javascript

12-04-2008, 10:18 PM
Is upload.php in the same directory as the calling script and everything else that works correctly? Have you tried killing the upload script with an echo to see what $user and $sql contain?

12-04-2008, 10:39 PM
Everything else works fine, If I set
$user = "adam";
It works...

echo $user returns:

Everything gets inserted into the database correctly, except that user = Guest