Page 1 of 1

mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 4:21 am
by Teonnyn
Okay, I have an issue that's a real hair-puller so far. I've created an application that allows users to upload their images through flash into the system so that they can reuse them later or currently. That part of it works. The system also properly recognizes my INSERT query ... for the most part. The upload script is what handles the insert into the database... now for the line of code I'm having issues with:

Code: Select all

 
$userSes = $_session['usernameSession'];
$insert = mysql_query("INSERT INTO geekAvatars (userName, avatarFile) VALUES ('$userSes', '$newName')");
It recognizes and handles the "$newName" insert just fine (a renamed file image that is handled by this particular file. However, for whatever reason it refuses to insert the userSes variable. I tested it by echoing it out, and $userSes reads just fine. Does session only work when the user is looking at a particular file?

Re: mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 9:11 am
by JakeJ
That's not quite enough information but perhaps your session information is blank.

Try

Code: Select all

<?php
If(isset($_session['usernameSession'])) {
$userSes = $_session['usernameSession'];
}
Else {
echo "Variable $_session['usernameSession'] is not set.";
}
?>
That will tell you at least if you have empty session information which means you need to look elsewhere for your problem.

Re: mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 10:07 am
by AbraCadaver
1. $_SESSION holds session vars, not $_session
2. You must start the session before using session vars
3. Where did you set this session var, did you start the session there?

Re: mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 11:30 am
by Teonnyn
session_start() is declared at the very start of the script, where it should be. The session itself isn't blank.. I tested it out in an echo and visited the page, where it showed up the way it should. The name of the file is inserted into the database, but the variable for the username is not.

Code: Select all

 
<?PHP
session_start();
$seed = rand(1000,9000);
$filename = $_FILES['Filedata']['name'];
$newName = $seed . $filename;
 
move_uploaded_file($_FILES['Filedata']['tmp_name'], "****/avatars/".$newName); 
chmod("****/avatars/".$newName, 0777); 
dbConn();
mysql_select_db("****");
$userSes = $_SESSION['usernameSession'];
$insert = mysql_query("INSERT INTO geekAvatars (userName, avatarFile) VALUES ('$userSes', '$newName')");
mysql_error();
?>
 
This is the full script, however the only thing in it that is not working is the username session insert. The session itself is set on the main website, and this page is stored in an /inc/ folder with session_start being it's own real link.

Re: mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 12:28 pm
by JakeJ
Is it possible that the user name contains characters that may need to be escaped before putting them in the database? Perhaps you need some data validation. Just a thought. Everything else looks ok. Strip out the whitespace, etc. Are you doing all that?

Re: mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 12:36 pm
by Teonnyn
The test username is simply "Teonnyn". I can load the upload.php script with nothing else running, and almost all of the information gets inserted perfectly (except for the file, with no image in the que - that just loads a seed.) It seems like upload.php, when executed by Flash, ignores the session entirely. Would it be better to use a cookie, and if so, what is the best way to do that? I did try the cookie method and that resulted in a "Cannot declare cookie because of other header" issues.

Re: mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 1:23 pm
by JakeJ
Sorry, the flash stuff is out of my league, sorry. I haven't worked with flash.

Re: mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 1:27 pm
by AbraCadaver
Teonnyn wrote:The test username is simply "Teonnyn". I can load the upload.php script with nothing else running, and almost all of the information gets inserted perfectly (except for the file, with no image in the que - that just loads a seed.) It seems like upload.php, when executed by Flash, ignores the session entirely. Would it be better to use a cookie, and if so, what is the best way to do that? I did try the cookie method and that resulted in a "Cannot declare cookie because of other header" issues.
I don't see how this session var would be set. Maybe your flash form is posting? If so, check the $_POST var.

Re: mySQL refusing to recognize session variable

Posted: Sat Jan 23, 2010 6:42 pm
by Teonnyn
I'm trying to send a variable from PHP to Flash, using Actionscript 2. I can't use LoadVars in this situation because I don't want the user to see the variable.

Re: mySQL refusing to recognize session variable

Posted: Sun Jan 24, 2010 1:28 am
by Teonnyn
Okay, I keep trying things. I've even tried a $_COOKIE over a $_SESSION and the same result occurs when I use the script.

Code: Select all

 
$user = $_COOKIE['username'];
$insert = mysql_query("INSERT INTO geekAvatars (userName, avatarFile) VALUES ('$user', '$newName')");
mysql_error();
 
From a browser, this works. But if Flash calls it... '$user' is ignored entirelly. '$newName', no matter how complex it might actually be, is inserted perfectly. It seems to consistently skip over '$user' when called by another script, and a COOKIE is making no more difference - the cookie itself is set when the user logs in. Sending the username from Flash is also failing.

Re: mySQL refusing to recognize session variable

Posted: Sun Jan 24, 2010 4:24 am
by Eran
Under certain browsers, flash does not send the original session ID with requests. You need to send it and set it manually using session_id()

Re: mySQL refusing to recognize session variable

Posted: Sun Jan 24, 2010 6:21 am
by Teonnyn
It does actually seem to be an ID problem. After hours of research into it, I finally came across a post where a user was complaining of the same issue, both cookies and sessions failing when an upload script from Flash called the .php server-side script. How do I set a cookie or session ID? On cookies apparently there's more too it then just declaring "setcookie".