Jump to content
Larry Ullman's Book Forums

Welcome Back! Message


Recommended Posts

Hello,

 

I have been attempting a "Welcome Back" type message using a session variable in order to give the user the idea that they have actually logged in.

 

I also understand that his is done with cookies.

 

However, when I put the "user_id" session variable code on my page, I get the registrant's user id so that makes me think I am using the right code, but that is not what I want the user or the world to see. When I use the same code and insert "username", I don't get the registrant's username I get MY actually username for the database.

 

I am basing my test website on the Knowledge of Power site and would like this kind of thing to come up on all pages that the user might pull up as they go through the site.

 

I am still learning PHP and it seems like there are many different ways of writing this language.

 

Thanks,

 

Marie

Link to comment
Share on other sites

That code doesn't do anything at all like you might expect it to. First of all, all that code does is attempt to assign to $_SESSION['username'] the value of $row[1]. It doesn't attempt to print or display anything. And that code will only do something if sessions have been started and $row[1] has a value.

Link to comment
Share on other sites

Thanks Larry,

 

Okay the session is started and the user has logged in and now gone to the Reset Password page.

 

When I have this code inserted - <p> Welcome Back <?php echo $_SESSION['user_id'];?> - I get the logged in user's id number which seems to always be correct when I check the data base.

 

When I have this code inserted - <p> Welcome Back <?php echo $_SESSION['username'];?> - I get my personal, supposed, secret username that lets me into my database NOT the logged in member's username.

 

With the first code I am actually getting into the database and with the second code I am not getting past the database.

 

Am I even close? Or do I get the result I want with a cookie.

 

Marie

Link to comment
Share on other sites

Hello,

 

I looked through the PHP book on the section called Sessions and have inserted this code into my page — <p></p> <p> 'Welcome Back, ' . <?php echo $_SESSION['username'] . '!' ?>;<p></p> and I still get the username for the database not the username for my fake registrants.

 

Should I change the name of the session? I have noticed that the other session name is user_id, and the other is just username. How does the database find user_id when the name of the table is actually users with an s?

 

Thanks,

 

Marie

Link to comment
Share on other sites

I have been trying various codes and scripting.

 

When I put this on my ResetPassword page -

$_SESSION['username'] = $_POST ['username'];

print "<h1>Welcome Back {$_SESSION['username']}!<//h1>";

 

I get this error message -

Undefined index: username - referring to the ResetPassword page.

 

If I try to change the Session name on this page I get the undefined index on the login,inc.php page.

 

Thanks

 

Marie

Link to comment
Share on other sites

When I have this code inserted -

Welcome Back <?php echo $_SESSION['username'];?> - I get my personal, supposed, secret username that lets me into my database NOT the logged in member's username.

 

Then you've assigned the wrong value to $_SESSION['username'] at some other point in your code.

 

Should I change the name of the session? I have noticed that the other session name is user_id, and the other is just username. How does the database find user_id when the name of the table is actually users with an s?

 

The database doesn't find anything and sessions are not directly tied to the database. At some point you assigned the wrong value to $_SESSION['username'].

 

When I put this on my ResetPassword page -

$_SESSION['username'] = $_POST ['username'];

print "

Welcome Back {$_SESSION['username']}!/h1>";

 

I get this error message -

Undefined index: username - referring to the ResetPassword page.

 

That will only work if the page receives data from a form that uses the POST method and has a 'username' element.

 

It seems that in your attempts to do this, you're getting confused by some of the fundamentals. Have you read up on the key concepts of sessions? If so, where?

Link to comment
Share on other sites

Hello all,

 

Thank you for your help. I am still wrestling with this and have been working on it throughout the week.

 

Larry, I appreciate you answering my post. I have two of you other books and have been referring to the PHP book and the eCommerce book most often. I thought I had a good understanding of sessions before I encountered this site and tying to integrate the code into my own site.

 

I am using the Knowledge is Power as my model. In the login.inc.php there is a query on the database where the id, username, type and whether or not the account is valid has been selected. Then they have been assigned to a session.

 

// Store the data in a session:

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

$_SESSION['username'] = $row[1];

 

So, I presumed that a value and session regarding the username has already been established. However, when I put in $_Session['user_id"] I will get the registrant's id number so that session makes sense although I do not understand where you get the $row[0] from. So I would think that $_Session['username'] would give me the registrant's user name which could be anything - like "happy cat walking" or "fall leaves dropping" or whatever. I ALMOST had it working at one point but it was not consistant. So I usually get my username for the database, as I mentioned above.

 

What I understand is that $_SESSION['username'] is merely the name of the session and the value would be $row[1] although I am not too sure how you determined that value - where it came from.

 

My site is live although sitting in a subdirectory of another site while I do testing. I have been able to reset and change passwords just fine.

 

Wondering why the Knowledge of Power site has a username when it does not appear to be used in the site anywhere.

 

When I was doing this kind of coding in Dreamweaver when my site was on the local host it was working fine. When I upgraded to Snow Leopard I lost the MySql connection and the files are now hidden, so I decided to take it up a notch and make it live.

 

I think this should be simple and of course it would be for you. I guess I don't understand the values that you have assigned to the SESSIONS.

 

Since you have already queried the database I did not want to do it again. SO, is this the route I now take. Query the database for the username only, assign it to another SESSION?

 

Any help at all would be appreciated.

 

Thanks,

 

Marie

Link to comment
Share on other sites

Hello Marie,

 

This is simple, really. At some point in your code, you assign the wrong value to $_SESSION, as I wrote before. If I understand you correctly, you're confused by the use of $row[0], $row[1], et al. When you fetch a query result row into the $row variable, and you use the MYSQLI_NUM parameter, then $row[0] is the first selected item (in the query), $row[1] is the second, and so forth.

 

I would search your code looking for every line that assigns something to $_SESSION['username']. I would also verify the query results. This is standard debugging, and should be used any time you're confused by the result of a database interaction.

Link to comment
Share on other sites

  • 5 months later...

Hello,

 

I revisited this and the following code seems to work for me when I place this in the appropriate spot on any page.

 

<?php

 

$q = "SELECT id, username FROM users WHERE id={$_SESSION['user_id']}";

 

$r = mysqli_query($connect, $q);

 

if (mysqli_num_rows($r) > 0) {

 

while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {

 

echo "<h3>Welcome $row[1]! You can change your password here.</h3>";

}

}

echo '</p>';

?>

Link to comment
Share on other sites

 Share

×
×
  • Create New...