Unknown column error where column is a session variable? - php

I'm getting a really weird error for a query. I'm working on a CMS and the query works perfectly when I'm in the public pages and, until a couple of hours ago, it also worked in the admin pages.
The weird thing is, when I go to login to admin, it's failing due to a reference to an unknown column which isn't actually a part of the query at all, it's a session variable.
The query is;
select * from pages p, templates t, sites s where p.page_title_csv='uber,admin' AND p.page_template_id=t.template_id AND s.site_id=p.page_site_id
The error is;
Page selection query failed coz of:Unknown column 'this_site' in 'where clause'
The column it's referring to ('this_site') is in fact one of my session variables and isn't mentioned in the query at all so how it's causing the error is a total mystery to me. It's not even a column in any of my tables.
I've already checked;
the database is connecting fine
there is definitely a page with a page_title_csv of "uber,admin" (and definitely only one)
I've tried removing the aliases
I've even tried changing the 'like' to '='
Destroying $_SESSION and starting again after closing multiple browsers (Chrome, Firefox, Safari)
Restarting all services (running Windows 7,Apache 2.2.17, PHP 5.3.5 & MySQL 5.5.8)
I know I haven't altered the table structures recently enough to have caused any problems. As I say, it works fine on all of the public pages but not on any of the admin.
The only possible clue I've got left is that $_SESSION['this_site'] has one setting for the public pages while the admin ones have a different value, although why that should matter when it's not even in the query is beyond me (especially when it was fine up until now).
Any help appreciated, it's 3:30am here so giving up for the night before I make things worse... Cheers
(Last ditch is to try a full reboot, if I'm not posting here again within the hour, that didn't work either)

Fixed it!
Marc pointed the way - I started looking further down the line and found that an embedded function in the page was causing the dodgy query because I had changed the way I handled call_user_func() in the page building routines. I'm sure I would have been stuck for much longer if not for the help. (Just writing up my question helped me get it right in my head too, never underestimate the power of writing problems down lol).
Thanks all for viewing/pondering this problem- almost every other problem I've had in the last few months has been solved by just browsing Stackoverflow but this was the first time I had to post. The best site for programmer community help ever and that's partly your fault for reading and responding where you can - thank you one and all!

Related

How to close mysqli connection on AWS instance?

I am aware that similar answers were given before, but I feel that my issue is somehow context-specific. My apologies if it turns out to be an exact duplicate, I am very open to suggestions on how to make the post clearer.
I am having a hard time replicating the issue, so I have to "qualitatively" describe the issue.
What I have, is a html form that has to be submitted to a mysql db hosted on AWS. The issue is that the .php that contains the query to write on the db doesn't always work. I know it's a vague depiction of the issue, but what happens is that it works when I test it from the devices I have available, but in my real case scenario (a survey on Mturk) it misses most of the connections.
I wonder if it might be a memory issue, because I keep seeing under "Current Activity" that there are connections opened, even though I specified in my php mysqli_close($conn);
However, the survey on MTurk has been so far published in 1 batch of 9 people at a time, so even assuming the connection aren't being closed...is it a number that might generate issue if all the sql does is posting a form?
I have been suggested to look at show processlist while running the query, and this is the output after submitting the form 3-4 times in a row:

Server multiple tabs at the same time

Forgive me if this is a duplicate question, but I've spent the better part of an hour looking and couldn't find anything specific. Or I didn't know how to word the question.
Problem:
I administer a website which runs a PHP application using the CakePHP framework. We have an issue where if you want to open multiple tabs for the same domain, you have to wait for the 1st request's data to return before the other tab's data is returned.
e.g. lets say I click a link which runs a heavy DB query and takes 5 seconds to return and render the page. But after clicking that link, I also click some other links for very light pages. These other tabs will not render until the first tab receives data back from the server.
I also know that the server side processing is done async. because even if I load 2 heavy tabs, as soon as the first returns the 2nd also returns and renders the page.
So... my question is, is this a code related issue or a config issue and how would I go about correcting this?
Stack:
Apache 2.2.14
PHP 5.3.2
CakePHP 2.4
Ubuntu 10.04
Any help or pointers in the right direction would be much appreciated.
P.S. I know its possible because about a year or two ago, I managed to get this to work, but it messed something up with our sessions so I had to revert what I worked on.
Its actually a browser feature, though I can not find a good reference currently to it.
https://productforums.google.com/forum/#!topic/chrome/H0AHdeAXSFo
is a topic but doesnt really show much. Is it possible to load the page and then load the results in though AJAX ? You do not then suffer from the same issues.
EDIT:
Answer above is wrong...
This is caused by session locking.
Example 4 on http://php.net/manual/en/function.session-start.php
<?php
// If we know we don't need to change anything in the
// session, we can just read and close rightaway to avoid
// locking the session file and blocking other pages
session_start([
'cookie_lifetime' => 86400,
'read_and_close' => true,
]);

Random session data loss in PHP

Here is the problem we have been facing for the past few weeks.
1/ Our setup
PHP 5.4 + MySQL
2 dedicated servers, load-balanced
Sessions are replicated between the 2 servers using memcached
3 applications running on these servers :
One custom-developped application, using default php session settings
Another custom-developped application, using different session settings (cookie name, path)
One Wordpress CMS
2/ The problem
The problem occurs on our first application.
Some of our users reported that they sometimes get disconnected after a few minutes (when the session is setup to last 3 hours). It can happen to them several time in the same day, then no disconnection for a few days, but the problem always comes back.
So far the fraction of users impacted is small, but I would like to solve this before it "spreads" to other users.
The problem seems to occur in different places of the application, though we have identified 3 scenarii where most of the errors occur :
Some involve submitting a form ($_SESSION variable is modified)
Other simply involve opening a popup page, with no modification of the session data
We have tried to reproduce the different scenarii described by the users : sometimes we have been able to, but most of the time we don't have any problem, which makes it hard to debug.
Other notes :
The problem is recent, this application had been running for years without any problem.
It doesn't seem to be related to our server load, because the problem still occured during the summer break when our trafic was low
It only affects one session/users at a time: all the other users logged in at the same time don't experience this problem
The problem occured on all the different browsers (IE, Firefox, Chrome)
3/ Technical analysis
When a disconnect occurs, the user is redirected to a page "Your session has expired or you don't have the right to view". When this page is loaded, we get a technical email with a dump of the $_SESSION variable.
When a session expires the normal way, the email we get shows that the $_SESSION variable is empty (normal behavior).
When an unexpected disconnect occurs, what is interesting is that the $_SESSION is not entirely empty : out of the ~20 elements the array contained, only one is left (always the same).
So this would mean the session is not expired, but not enough data is left to "identify" the user, hence the "no rights" page displayed. As a confirmation when this occurs, we can check in memcached that this session still holds some data.
These are the potential problem causes we have identified so far, and what we have done to rule them out :
Memcached indicates between 70 et 80% freespace, so we don't think it is the problem.
We removed Memcached and went back to using a NFS shared directory for session files: the problem actually got worse. This would point to an applicative bug, because NFS being slower to write data, session loss would occur more often.
We have browsed all the different forums (including SO) talking about PHP session data loss, and reviewed our code accordingly. The code base is big, but we have used automated tools and scripts to avoid missing a file.
session_start() is called at the beginning of each page.
exit() is called after each header("Location...")
register_globals is Off
We have tested the possible interractions between our 2 other applications and the problematic one, though they don't share any code, database or session handling. Nothing identified there.
We have analyzed our access logs around the times of the disconnections, to check for behavior patterns : no luck here either.
So we have no idea what causes this problem, as it seems to occur randomly, so my questions are :
The problem could come from our code: did we miss anything to check ? This solutions seems unlikely as the code works most of the time for all our users, but I am still considering it.
The problem could come from another application/process that would "empty" part of the session variable array. We have also reviewed the code from the other applications, but didn't find anything that could cause this.
And if another process is doing this, why would it only empty some sessions and not all of them ?
Thanks for your help.
I don't think you'll get a definitive answer to your question. There are too many probable causes and you haven't shown any code.
Still, my guess is that you have memcached.sess_locking turned Off, or if you have a custom session implementation - that it doesn't implement locking at all.
Eventually, this leads to a race condition between two simultaneous HTTP requests.
My guess is based on the often seen bad advice to turn off locks or free them as soon as possible, in order to achieve higher performance.
If this problem "suddenly" occurred, check what has changed. Did you do any work on the application? If so check committed code (you talked about automated tools so I expect there to be a repository which would allow for accurate finding of code changes).
Did you change anything on the server? Like upgrade software, upgrade/change hardware, make changes to the other two applications ?
One thing that popped to mind, did you check the drives you use for caching? It could be a corrupted part of the file system. Which would explain the random user part.
I couple of things I always to is:
Try to determine the moment of first occurrence as accurate as possible. At my work this occasionally triggers someone saying "oh yeah that might have to do with when I changed/updated/created this or that" so this might help. On the other hand it can sometimes takes days, weeks or more before something gets noticed so start expanding that time-frame if nothing comes up.
You have already a couple of scenario, find the common factor in these. If they don't share any code, stop looking there. If they DO share code search there. Of course sharing (part of) it here might allow us to help you search.
Do an organised search. I usually do the main application check when I am the one working most on the application (or even better when I created it). A colleague will check surrounding applications that might have influence on it. In your case those 2 other applications. Finally our sysadmin will check for newly installed or updated software on the server(s) and he will also check with our network guys if anything changed hardware wise or network related (for other people this could be the hosting provider).
It could be as simple as a WordPress plugin that uses sessions and calls either session_name() or session_id() with a different value, overlapping your custom applications with default session settings.
Since WordPress itself does not use sessions, plugins are often written from the perspective of having free rein with sessions. I just did a search on a WordPress test site and found sessions used in a gallery plugin, a plugin for putting a background image on the page, a shopping cart plugin, and a plugin I was writing that needed to carry an uploaded file from one admin page to another.

After recent shared hosting updates to Php, MySQL and Apache my site is not pulling data from MySQL (1064 syntax error)

Newbie here. My hosting company recently upgraded their server(s) and that's where my website problems began:
FYI:
MySQL upgraded from 5.1.70 to version 5.5.32
Apache upgraded from 2.2.25 to version 2.4.6
PHP upgraded from 5.3.27 to version 5.4.22
I have a shared hosting account
For the last 2 days I've been searching Google, online manuals and developer/tech forums for possible answers to my "issues" and I'm having no luck finding good answers that help me fix my code to make my website display properly. Very frustrating. Obviously I need to learn more.
BKGD: I hired a developer to build a CMS for my website a couple of years ago and have since been trying to learn so I have a VERY basic understanding of Php, MySQL and HTML... but obviously not enough to fix these issues. Can you/anyone please help me get closer to fixing this nightmare.
I know my original website code worked fine a week ago - prior to the hosting updates.
The Index page and other "main Sections" currently display just fine for some reason. But if you click on any of the links to "articles", "news" and other "pages", those pages are not connecting properly >> not pulling table data from the associated MySQL database. Those pages showed a couple of different error messages.... initially displayed ONLY a "1064 syntax error" message OR "no page found" error (no "header", "footer", "page title", or any other info). I've since tweaked the code (I.e., I "hid" some of it with "//") and have it displaying the header and footer but nothing from MySQL database.
The original "php.ini" file needed extra code >> "register_globals=on" << to work properly after the second-to-last Php & MySQL update. That is now "deprecated" and "removed".
This newbie thinks that the following code is ONE EXAMPLE that isn't working properly (that I believe needs to be edited/updated to allow it to connect to MySQL). I think if I figure this one out I can apply similar tweaks to the other "news" and "article" pages:
⁣
//-- START: SAMPLE CODE ----
include "init.php";
$res = mysql_query("SELECT * FROM {$prefix}pages WHERE pag_name='$page_name'") or report();
if (mysql_num_rows($res)==1)
cdie("no page found");
else
$row=fetch($res);
$title = "$row[pag_title]";
$desc = "$row[pag_description]";
//------- END: SAMPLE CODE --
I realize I might be way off on this being the issue (E.g., the problem might be in the functions.php file) but that's where I'm at right now. Clueless. Ugh.
Any thoughts or suggestions? Need more info (just ask and I'll reply as soon as I get the email).
If your code relies on register_globals, you are going to run into undefined variable problems.
For example, if the $page_name variable comes from a query string like index.php?page_name=test, you could access it before like $page_name but now you must access it like $_GET['page_name'].
You need to check all your code to see if your variables are defined (a good IDE can help with that).
As a sidenote, you also need to double-check to see if you don't have any sql injection problems, injecting variables directly in a query is normally not a good sign. You should really switch to PDO or mysqli and prepared statements but at the very least use mysql_real_escape_string on your variables before you use them in your queries.

Correct SQL Queries Giving Different Results in Joomla Articles

I have a few modules, but I will focus on one in particular. This module was working fine, and it appears in two articles. One article is viewable to registered users and the other article to the public.
I query my database from both of these articles. However, in the public article the module does what is supposed to, but in the registered article, it doesn't.
Before posting code and examples, is there anything obvious that I'm missing that I can try to overcome this?
EDIT
Here are the results of debugging mode:
Working article:
SELECT name, specialty, location
FROM people
WHERE name = 'larry' OR specialty = 'testing' OR location = 'seattle'
Non-working article:
SELECT name, specialty, location
FROM people
WHERE name = '' OR specialty = '' OR location = ''
The same query should be at work in both cases, and is this:
SELECT name, specialty, location, description
FROM people
WHERE name = '$name' OR specialty = '$specialty' OR location = '$location'
where these variables are POST data, eg, $name = cleaned_up($_POST['name']);
So basically the "registered" users version of the form doesn't post data to the query but the "guest" users version does.
EDIT 2
I discovered that when I remove the module on the left side of my page (I have two modules in divs, side by side, that share POST variables - which is probably part of my problem), the form that doesn't work, starts to work properly.
So I built an identical module, thinking if I pulled that form out and gave the code to another module it would work, but it gave same results as above.
Any help is greatly appreciated.
EDIT 3
I threw a var_dump($_POST); in there in several locations, one at a time, and this gave the expected POST data. So now I'm really confused, because the SQL query is correct and the POST data are getting to the file that needs them.
Although the question is a bit vague without specifics, I would suggest turning on debug profiling in Joomla. If you turn this on, you'll be able to see the exact SQL queries being executed by the module. Locate the queries used by your module, run them via phpMyAdmin or MySQL Workbench, and make sure you get the expected result.
If the issue is not SQL related, then you may need to provide more specific examples.
I found a work around that seems to solve my problem. The original weird issue still exists, but this work around gets my site working and gives me something to go on.
I created as separate variable file for each access state, registered and public. Apparently in the registered state the variables were getting conflicted and the POST data wasn't making it to the SQL query and thus wasn't showing up in my results table.
I think it may be a bug in Joomla but I'd like to get someone else feedback before reporting it. BUT... I posted on forum.joomla.org weeks ago with not response so I have little faith in that support site at all. Actually, if this is as good as their support forums get, then I think I might look into another CMS.

Categories