Forum Moderators: open

Message Too Old, No Replies

Session management

session

         

Ann_G

4:46 pm on Sep 26, 2006 (gmt 0)

10+ Year Member



I have a database that was working fine until our hosting service switched over to a new server and since then it doesn't work anymore, but no one can figure out why.

The problem is that the user in a table doesn't get recorded after he/she logs in. The field for user just records 0, whereas in the past it would give the user a number.

There are two tables, one registers the users and that table works fine. The other table records the titles of books a user has read. I use php's session feature to keep track of the users:

In the login php:
// Start the session, register the values & redirect.
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];

And in the page where user adds a title.
// Start output buffering and initialize a session.
ob_start();
session_start();?>

I can see that the titles get entered in the second table, but no number for the user.

My php for the table that doesn't record the user looks like this:
$query = "INSERT INTO books (book_id, user_id, title)
VALUES ('$book_id', '$user_id', '$title')";

Any ideas as to why my table doesn't work anymore is greatly welcomed.

volatilegx

9:13 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe that number field doesn't have the autoincrement flag set, so it's always set to zero?

physics

9:27 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes that sounds right (auto increment). How did you migrate the database? There's a bug in mysqldump related to this so maybe that was it. What do you use to administer your db (phpmyadmin?), what type of db is it (mysql?)?

coopster

9:44 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Details are sketchy here but it certainly sounds like you are firing a non-NULL value into an AUTO_INCREMENT field. I seen this issue rising back almost two years ago, mentioned it in a PHP thread:

Autoincrementing id fields from a post form... [webmasterworld.com]

I don't know if it is related but it is something to check into ... your version upgrade is the tip-off.

Ann_G

8:28 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



Thank you for all your responses.

My first thought was that I needed to set the auto increment for the user_id field but when I do that I get this response:

"Incorrect table definition; there can be only one auto column and it must be defined as a key"

My primary field, book_id, in this table is set to auto increment and in the other table, the users table, the primary field is user_id and it is auto incremented. So I don't know how I get this field in the book table to record a user when he enters a title into his reading log. I thought that was what the Session function did. Like I said, it worked at one time before my hosting service moved to another server.

I use MYSQL 4.1.21 and PHPMyAdmin 2.8.24

This is a mystery that might have to do with this server change since I'm pretty certain I haven't changed anything in my PHP scripts.

Thank you for trying to help me.

coopster

2:01 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



So you aren't creating a new user, you are updating a "book reading log" table for an existing user that has logged in and identified themselves to you -- you have authenticated them against your user table and stored their unique userID in your session variable.

Have you checked the $_SESSION superglobal after a successful login to ensure your variables are being populated? Put a line of code in place that will dump it out for you. I often check the userID before dumping it so that I am not interrupting any other developers ...

<?php 
// session_start and other stuff has already happened here
if ($userName == 'Ann_G') {
print '<pre>'; print_r($_SESSION); exit('</pre>');
}
?>

Ann_G

8:03 pm on Sep 28, 2006 (gmt 0)

10+ Year Member



Yes you summed it up correctly.

I just assumed the Superglobal session was being populated because after I log in the page with the Welcome and the user's name is returned.

coopster

4:53 pm on Sep 29, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How about that ID though? Was that holding the value you expected?

Ann_G

6:14 pm on Sep 29, 2006 (gmt 0)

10+ Year Member



How do I check if the ID holds the values I want? I'm very uneven in my knowledge of PHP and MYSQL.

Ann_G

2:40 pm on Sep 30, 2006 (gmt 0)

10+ Year Member



I can see in my MYSQL table that the user_id remains 0. How else do I check if the id holds what i expected?

coopster

12:43 pm on Oct 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



By using the code snippet earlier.

After a successful login you said that the username is displaying on the page correctly which you are pulling from the $_SESSION superglobal. I am assuming the userID is in the same $_SESSION array so by dumping it out at certain points in your process you can tell whether or not you are carrying it throughout your session, especially right before you use it to log the entries in the database table where you are seeing your issues.

Ann_G

5:16 pm on Oct 2, 2006 (gmt 0)

10+ Year Member



When I put in that snippet of code nothing changes on my pages.

In have a header in my includes file that has this code you see below and which I read would be the correct way to track a user.
But since I get nothing with the code you gave me, shall I assume the session is working (although the user is greated by name once logged in)?

This is very confusing.

I do appreciate your taking time to help. If you want to give up I understand, I'm about ready to give up.

// Start output buffering and initialize a session.
ob_start();
session_start();

coopster

2:53 am on Oct 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, I would probably start at the login page, after the user has submitted their login information you are likely pushing the user through some form of login routine or off to a landing page after a successful login. One way or another you are likely populating some $_SESSION indexes at that point. That would be a good starting point. Don't forget to change the if control structure here to match your userName session variable and your userName. Or, if you aren't concerned about disrupting others you can leave the whole if statement out altogether. You'll be halting any users using the site at the time, though.
<?php  
// session_start and other stuff has already happened here
if ($_SESSION['userName'] == 'Ann_G') {
print '<pre>'; print_r($_SESSION); exit('</pre>');
}
?>

Ann_G

4:23 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



I did what you told me and then I get this error message:

An error occurred in script /mydirectory/myfilename/includes/config.inc
line 12:ob_flush()[ref.outcontrol]: failed to flush buffer: No buffer to flush.

ref.outcontrol is a hotlink in the error message, but of course when I click on it, I get "page cannot be found".

I know the config.inc just sets the error reporting for the site.

Is the error message referring to my include/header file and this:

// Start output buffering and initialize a session.
ob_start();
session_start();

I'm at loss. I haven't edited any of these files since the time it worked and the user_id was recorded.

coopster

5:07 pm on Oct 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks like you are trying to print the $_SESSION superglobal out before you have started your session. You'll have to jockey the dumped output snippet around so you can see what is in the array once your session has been started.

Ann_G

4:04 pm on Oct 12, 2006 (gmt 0)

10+ Year Member



Thanks! I finally got it to dump out the user_id. So now I know that the Session auto global works.

But my problem, to get the the user_id recorded in my MYSQL table, remains. When I use this statement:

$query = "INSERT INTO books (book_id, user_id, title, author) VALUES('$book_id', '$user_id', '$title', '$author')";

// Run the query.
$result = @mysql_query ($query);

all the info is entered into the table correctly but the user_id column remains at 0.

coopster

8:53 pm on Oct 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Start with that then. Dump that statement out first ...
$query = "INSERT INTO books (book_id, user_id, title, author) VALUES('$book_id', '$user_id', '$title', '$author')"; 
exit($query);
// Run the query.
$result = @mysql_query ($query);

That exit [php.net] statement will print out your query and stop processing right there. You can have a look at your query statement and see if the value where $user_id is supposed to be actually has a value or not. If not, then you have to track back up through your code to figure out where it is losing it's value (since you said it is indeed in your $_SESSION superglobal).

You do know that you should be moving that $_SESSION value into $user_id or using the $_SESSION value, correct? Old PHP programming and scripts used to have register_globas [php.net] turned on but upon realizing the security implications the developers have turned it off by default on PHP installations now. I wonder if that is the issue you are experiencing?

Ann_G

4:50 pm on Oct 13, 2006 (gmt 0)

10+ Year Member



This is what I get when I dump it out and use this -- exit($query);
in the same php script.

Welcome, Ann_G!
Welcome, 1!
INSERT INTO books (book_id, user_id, title) VALUES('', '0', 'test')

So the session user_id registers since there is a 1 there, but then it doesn't go into the values. Am I overlooking something very obvious? (PHP can really make you feel stupid)

I believe I'm using the superglobal $_SESSION
For example, in my login file I have this.
// Start the session, register the values & redirect.
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];

Doesn't that mean my $_SESSION value is moved into my $user_id?
As I said I'm very spotty in my knowledge of php.

I have learnt a lot even if my problem isn't solved. So please don't hesitate to quit, I realize it's very time consuming to help php dabblers like me.

coopster

5:05 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No worries, Ann_G, I don't mind helping when I have a moment.

$_SESSION['user_id'] = $row[0];

That line looks like it is moving a value that was retrieved from the database into your $_SESSION superglobal array under the index 'user_id'. A very typical expression. Now, we know that value is '1' because you are seeing it when you print out some of your $_SESSION information ...

Welcome, Ann_G! 
Welcome, 1!

At least, that is what I am guessing that you are doing here. Am I correct? The line of PHP code that prints out that second line there probably looks something like this ...

print 'Welcome, ' . $_SESSION['user_id'];

Now, the big question is, does the $user_id variable ever get populated with the $_SESSSION['user_id'] value at any point prior to you using it in your query? Something like this ...

$user_id = $_SESSION['user_id'];

It doesn't happen magically. I say that, not being sarcastic, but very serious. You see, in the recent past PHP used to auto-magically populate variables like the $user_id variable here because of that

register_globals
setting mentioned earlier. Now, because of security issues, PHP installations turn that directive Off and variables don't get magically populated with their related global counterparts, in this case $_SESSION['user_id']. You have to do that manually, basically forcing you to recognize that you are using a variable with possible user-supplied (and possibly malicious) values and you have made sure it is good to use.

See if you can tell where you are populating the $user_id variable, if anywhere, and how it is populated. If you don't see a line anywhere like I have shown here, then you may have found your issue.

Ann_G

9:17 pm on Oct 13, 2006 (gmt 0)

10+ Year Member



Yes, you are correct.
The PHP that prints out the Welcome 1 is this:
echo '<h1>Welcome';
if (isset($_SESSION['user_id'])) {
echo ", {$_SESSION['user_id']}!";
}
echo '</h1>';

I put this user_id value on every page.
$user_id = $_SESSION['user_id'];
But the value in the Insert statement still reads 0.

It's Friday. I need to forget about this to Monday.
Hope you have a good weekend!

coopster

10:11 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well I think you are on the right track. Have a great weekend.

Ann_G

3:39 pm on Oct 16, 2006 (gmt 0)

10+ Year Member



It works! I can't thank you enough for helping me and being so very patient with me. And I'm so glad I didn't give up.

I followed your steps and having put that
variable $user_d = $_SESSION['user_id']
and still not getting a value in the query
made no sense at all.
So I took one more look at my query and
discovered that the order didn't match
the MYSQL table rows' order.
I had two inserts andvalues switched around.

I didn't check that the order matched very
carefully before, because I didn't realize
it could screw up the $_Session insert.

Once again thanks!

coopster

9:34 pm on Oct 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hey! Great news, glad you got it sorted ;)