Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts

    sessions don't work though javascript

    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:

    PHP Code:
    include("include/header.php");
    include(
    "include/footer.php");
    $user $session->username;
    $session->changeDir('upload/' $user '/pictures'); 
    The whole code of Upload.php:

    PHP Code:
    <?php
    include("include/header.php");
    include(
    "include/footer.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
         */
         
    error_reporting(E_ALL);
        
    // 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";
            exit(
    0);
        }
        
    $img imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
        if (!
    $img) {
            echo 
    "ERROR:could not create image handle "$_FILES["Filedata"]["tmp_name"];
            exit(
    0);
        }
        
                function 
    resizeImage($img,$target_width,$target_height){
                    
    $width imageSX($img);
                    
    $height imageSY($img);
                
                    if (!
    $width || !$height) {
                        echo 
    "ERROR:Invalid width or height";
                        exit(
    0);
                    }
                    
    $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_img00$target_width-1$target_height-10)) {    // Fill the image black
                        
    echo "ERROR:Could not fill new image";
                        exit(
    0);
                    }
                
                    if (!@
    imagecopyresampled($new_img$img, ($target_width-$new_width)/2, ($target_height-$new_height)/200$new_width$new_height$width$height)) {
                        echo 
    "ERROR:Could not resize image";
                        exit(
    0);
                    }
                
                    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(110));   
        
    $thumbName 'thumb_'$id '.jpg';
        
    $name 'pic_'$id '.jpg'
        
        
    $code rand(1999999999);
        
    //$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
        
        
    ob_start();
        
        
    $session->changeDir('upload/' $user '/pictures');
        
        
    // Create image
        
    imagejpeg($realImage$name60);
        
        
    $session->changeDir('thumbnails');
        
        
    // Create thumb
        
    imagejpeg($thumbImage$thumbName60);
        

        
    // Output thumb
        
    imagejpeg($thumbImageNULL60);

        
    ob_end_clean();



        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.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    PHP Code:
        $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.
    Last edited by tomws; 12-04-2008 at 08:41 PM.

  • #3
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts
    Ok, instead of it being loaded as a page in the browser, it is loaded using Ajax, Javascript

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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?

  • #5
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts
    Everything else works fine, If I set
    $user = "adam";
    It works...

    echo $user returns:
    Guest

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •