Log in

View Full Version : Resolved Using session variables



dan0
04-02-2009, 04:14 PM
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?

CrazyChop
04-02-2009, 04:19 PM
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.

dan0
04-02-2009, 04:40 PM
...
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:


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($newDir, 0777);
$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>";

CrazyChop
04-02-2009, 04:46 PM
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?

dan0
04-02-2009, 05:20 PM
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.:

<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.

CrazyChop
04-02-2009, 05:55 PM
Did the Java code writes to the PHP session? If I am not wrong, PHP sessions data are stored in a separate location.

dan0
04-02-2009, 06:06 PM
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.

Twey
04-03-2009, 04:46 PM
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.

dan0
04-03-2009, 06:54 PM
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?

Twey
04-03-2009, 07:22 PM
The same way you do when a browser sends it: with session_start (http://www.php.net/session-start)().

CrazyChop
04-04-2009, 09:45 AM
session_id() will return the id that PHP is currently using to keep track of its session.

dan0
04-06-2009, 03:13 PM
The same way you do when a browser sends it: with session_start (http://www.php.net/session-start)().

Just to make sure I understand, when the applet submits the session id (which it does as an http request) I can do the following to active the session?



$sessionId = $_POST["sessionId"]; // php script receives the session id
session_start(); // starts session
session_id = $sessionId; // set the session id, so that previously set session variables can be used


If any of the above is wrong, please let me know. Thanks.

Twey
04-06-2009, 03:39 PM
No, of course not. session_id() is a function, not a variable; all variables in PHP start with $.

There's a magic request variable that PHP will automatically pick up and use as the session ID. By default, it's called PHPSESSID. You can set the current session ID by calling session_id($newid);, e.g. session_id($_POST['sessionId']);, but it's easier just to use the default variable.

dan0
04-06-2009, 04:35 PM
No, of course not. session_id() is a function, not a variable; all variables in PHP start with $.


That was a mental lapse; I was looking at java and javascript code right before my post. You're absolutely right, as session_id is a function I'd have to send the posted session id ($_POST[sessionId]) as an argument.



There's a magic request variable that PHP will automatically pick up and use as the session ID. By default, it's called PHPSESSID. You can set the current session ID by calling session_id($newid);, e.g. session_id($_POST['sessionId']);, but it's easier just to use the default variable.

Thanks for the clarification; I was concerned that by setting the session id I might "overwrite" the previously set session rather than invoking it.