mySQL refusing to recognize session variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

mySQL refusing to recognize session variable

Post 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?
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: mySQL refusing to recognize session variable

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: mySQL refusing to recognize session variable

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: mySQL refusing to recognize session variable

Post 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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: mySQL refusing to recognize session variable

Post 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?
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: mySQL refusing to recognize session variable

Post 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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: mySQL refusing to recognize session variable

Post by JakeJ »

Sorry, the flash stuff is out of my league, sorry. I haven't worked with flash.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: mySQL refusing to recognize session variable

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: mySQL refusing to recognize session variable

Post 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.
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: mySQL refusing to recognize session variable

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: mySQL refusing to recognize session variable

Post 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()
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: mySQL refusing to recognize session variable

Post 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".
Post Reply