Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Using session variables

  1. #1
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Using session variables

    I'm trying to use session variables to create folders on-demand. The intended process to creating sub-folders is as follows:

    A user fills out an html form, which stores the information in $_SESSION variables. Once the form is saved the user uploads files via a java applet. With the session variables set, once the web server receives the HTTP request, the php script creates sub-folders and processes the files via move_uploaded_file.

    What I figured out is that when the user fills out the form, the session variables are set BUT on the php script page when it tries to use the session variables to create the sub-folders - the session variables are empty.

    When I use a traditional html form to upload files, the php script works just fine - the session variables are defined and used.

    Anyone know why in spite of the session variables being set, the php script shows them to be unset when I upload files via the java applet?
    Last edited by dan0; 04-06-2009 at 07:15 PM.

  2. #2
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    If the HTML form works, and the script works with the HTML form, then the suspect is the Java Applet.

    Second, does the php script page which is supposed to create the folders have session_start() called before attempting to read information from the session?

    Does the Java Applet upload communicates with PHP? PHP reads the header for information such as the file name and etc.

  3. #3
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by CrazyChop View Post
    ...
    Second, does the php script page which is supposed to create the folders have session_start() called before attempting to read information from the session?

    Does the Java Applet upload communicates with PHP? PHP reads the header for information such as the file name and etc.
    Yes, the php script page that is supposed to create the folders does call session_start() before it tries to access the session variables. Below is the code.

    The java applet creates and sends the HTTP request just as an html form does. When I have the php script move the uploaded files to a static (previously) created folder that does not rely on session variables - the script successfully moves all uploaded files (from the java applet) to the static destination folder.

    PHP script that creates on-demand sub-folders & moves uploaded files:
    PHP Code:
    session_start();

    if ( isset(
    $_SESSION["Id"]) && isset($_SESSION["topicId"]) && isset($_SESSION["topicTitle"]) ){
        
    $newDir "D:/Apache/htdocs/webtools/data/" $_SESSION["Id"] . "/feature_image_galleries/" $_SESSION["topicId"];
        
    mkdir($newDir0777);
        
    $path $newDir "/";
    } else {
        return 
    false;
    }

    if ( 
    file_exists($path) ){    
        for (
    $i 0$i count($_FILES['imgFile']); $i++){
            if ( 
    file_exists($_FILES['imgFile']['tmp_name'][$i]) ){
                
    $targetPath $path basename($_FILES['imgFile']['name'][$i]);    
                if ( 
    move_uploaded_file($_FILES['imgFile']['tmp_name'][$i], $targetPath) ){
            
                }
            } else {
                continue;
            }
        }
    } else {
        echo 
    "Directory does not exist: " $path;
    }

    echo 
    "<pre>";
    echo 
    print_r($_SESSION);
    echo 
    "</pre>";
    echo 
    "<br/>";
    echo 
    "<pre>";
    echo 
    print_r($_FILES);
    echo 
    "</pre>"

  4. #4
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    Is it possible to let me look at the script that uploads the files? There don't seem to be a problem with the one you have provided.

    Just curious - you say the page with static form set the $SESSION variables. Is that with input from the form? If you are using the Java Applet, do you also extract out that information (such as topicID) from the form and set it into the session?

  5. #5
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by CrazyChop View Post
    Is it possible to let me look at the script that uploads the files? There don't seem to be a problem with the one you have provided.

    Just curious - you say the page with static form set the $SESSION variables. Is that with input from the form? If you are using the Java Applet, do you also extract out that information (such as topicID) from the form and set it into the session?
    By script that uploads the files, do you mean the java code? If yes, sure I can post it. But if you mean the php script that handles the files once they have been uploaded to the server that script is in my previous post.

    There is no "static form." In an attempt to figure out where the problem was, I used a traditional html form - i.e.:
    Code:
    <form id="sessionTest" name="sessionTest" method="post" enctype="multipart/form-data" action="files_upload.php">
    <input type="file" id="imgFile[]" name="imgFile[]"/>
    <input type="submit" id="sessionSave" value="save"/>
    </form>
    to upload the files rather than the java applet. Everything else was the same i.e. the information collection form and the php script. When I used the above html upload form - everything worked.

    Then to test my java applet, instead of moving the uploaded files to a folder created on-demand, I set the destination folder to be a previously created one. What this means, is that the session variables that were set in the information form are not used. When I did this the java applet uploaded the files to the server and the php script moved the uploaded files to the destination folder.

    The applet does not extract any information, all it does is create an HTTP request (just like a traditional html form does) and sends it to the target php page.

  6. #6
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    Did the Java code writes to the PHP session? If I am not wrong, PHP sessions data are stored in a separate location.

  7. #7
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by CrazyChop View Post
    Did the Java code writes to the PHP session? If I am not wrong, PHP sessions data are stored in a separate location.
    No, the java code does not write to the php session. The information collection form the user submits prior to using the applet writes to the session.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You need to make sure your Java applet is sending the appropriate PHP session ID, either via GET or in a cookie. If you don't, the session will not be preserved for the request submitted by the Java applet.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    You need to make sure your Java applet is sending the appropriate PHP session ID, either via GET or in a cookie. If you don't, the session will not be preserved for the request submitted by the Java applet.
    I ended up passing the session variables to the applet, then having the applet submit the parameters to the target php script.

    If I had sent the php session id instead, how would I active that session in the php script?

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The same way you do when a browser sends it: with session_start().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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