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!
I need to design some code that will figure out if a member is logged in or not, then if they are, display a message like Hello "customer name" on the index page.
mthierfelder wrote:I need to design some code that will figure out if a member is logged in or not, then if they are, display a message like Hello "customer name" on the index page.
It doesn't do anything so far, and I'm pretty stumped.
Your code is a little wonky. First off, are you calling session_start()? Second, are you setting $_SESSION['AgentID'] to anything (PS Notice the single quotes around AgentID). Next, try cleaning your code a little bit...
Ok, well first off I'm not sure, and secondly I'm not sure....I don't know what I'm doing!
All I want to do is for mysql to get the table called 'yellow_agents' where the cell 'FirstName' is that of the person who is logged in, and display it. That's IT! I admit, I know very little about authoring PHP logic...although I can understand it just fine.
If you are not sure about whether you called session_start() or whether you are setting a value to a variable, then you may want to consider starting a little slower. Using MySQL to authenticate users in conjunction with Sessions is a little involved. You need to be able to start somewhere. This community can help you but it won't do the work for you.
Do you have something that you are starting with (a current code base) or are you tackling this from scratch? How long have you been developing in PHP/MySQL and what other work have you done in it? We might have to take a few baby steps to get you in the swing of using the more powerful features of the two.
I am working with a finished script, I'm just trying to augment it a little bit here and there. Mostly, I'm a web designer but have been moving towards more development in the past few years. My PHP/MySQL experience has been with OsCommerce, which I have been successful with making all kinds of changes requested by clients.
I have always just changed things or moved them slightly though, I have never tried to author anything on my own. That is to say, I understand the syntax pretty well if you put a piece of code in front of me but if you told me to sit down and author that same piece of code I'd be lost.
This almost works, but for whatever reason it refuses to pull the data from the 'FirstName' cell in my database...all it gives me is "Welcome, 3" instead which is the 'AgentID', which is the primary key of that table.
The code you posted above assumes that $_SESSION['AgentID'] is an array. To get the data from the database you are going to have to look at your query and how the PHP code interacts with it. Then you are going to have to figure out the field name used in the query for FirstName (sometimes it is the database column name, sometimes it is an alias). Once you have that, you need to set the value of $_SESSION['AgentID'] to a corresponding value of the resultset from the database query. This is just the setting of the session variable.
There needs to be more interaction than what you have shown to do what you want to do. If you can, provide more details or more code. Maybe there is something in the code that we can help you with in terms of setting and getting your session values.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I understand what you're saying. I've done some digging and come up with a more elaborate script, and now I'm 9/10ths of the way ( I THINK)
Here she is:
[syntax="php"]//WELCOME GREETING
$data = mysql_query("SELECT * FROM yellow_agents")
or die(mysql_error());
$info = mysql_fetch_array( $data );
/*while($info = mysql_fetch_array($data))*/
if ($_SESSION['AgentID'] == ''){ //empty
print "Welcome, guest";
} elseif ($_SESSION['AgentID'] != ''){
print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"border: 1px dashed #cccccc; margin: 10px; padding: 10px;\">\n
<tr>\n
<td valign=\"top\">Welcome Back, " . $info['FirstName'] . "<br><br>\n";
print "Account Status: " . $info['AccountStatus'] . "</td>\n</tr>\n</table>\n";
}
Unfortunately, this code only returns the name of the very first person listed in the "yellow_agents" table...Leann.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Unfortunately, this code only returns the name of the very first person listed in the "yellow_agents" table...Leann.
I think this has to do with the way you are trying to get information out of your result array. But, first things first, try to use the *PHP* button instead of the *CODE* button when posting PHP (you could also use the [ syntax="php ] BBCode tag as well).
<?php
// This query is pulling every record from yellow_agents table
// Is this what you want?
$data = mysql_query("SELECT * FROM yellow_agents") or die(mysql_error());
$info = mysql_fetch_array($data);
while($info = mysql_fetch_array($data))
{
if (empty($_SESSION['AgentID'])) { //empty
echo 'Welcome, guest!';
} else {
echo '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border: 1px dashed #cccccc; margin: 10px; padding: 10px;">';
echo '<tr>';
echo '<td valign="top">Welcome Back, ' . $info['FirstName'] . '<br><br>';
echo 'Account Status: ' . $info['AccountStatus'] . '</td></tr></table>';
}
}?>
This assumes that there is a field in the yellow_agents table with the name of AccountStatus and that you are selecting every row of data in the yellow_agents table. Another thing to consider is that you are comparing against the SESSION value for AgentID but you are using the database value if it is not empty. This would be a great place to use the session value.
sticky so I'll try that one next time.
I just want to return the value for the currently logged in member, or current session...not every single record in that table. But not sure how to do that.
Any help appreciated.
You may want to go back to basics. What you are after is a login/authentication routine using information stored in a database. That is not really terribly involved, but you are going to know SQL and how to use PHP to get at what you want. You might want to search here for "login scripts" or "user authentication" to see what comes up.
From your previous reply this thread is going to turn into a long tutorial on database interaction, sessions and user authentication. That might be a bit outside the scope of a single forum thread.
I thought that maybe if I provided a bit more of my code so you can see the context that it exists in you might be able to help.
And I actually DO have an authentication script in service, that is to say there is a registration process that envolves a username/password to get in.