Page 2 of 2
Posted: Tue Apr 11, 2006 7:56 pm
by RobertGonzalez
mthierfelder wrote:
Code: Select all
<?
require_once("conn.php");
require_once("includes.php");
require_once("templates/HeaderTemplate.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 (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>';
}
}
?>
OK, I think I can see what you are trying to do. However, your code is doing the following:
- Including 'conn.php' (DB connection details I assume)
- Including 'includes.php' (Functions, constant, etc I assume)
- Including 'templates/HeaderTemplate.php' (Your markup I assume)
- Grabbing all records that are in the yellow_agents table and reading the resource into a variable called $data
- Reading the resulting array of records into an array var called $info
- Checking to see if there is no value for a session var called 'AgentID'
- If there is no value then echoes out a generic text messsage
- If there is any value at all in the session var (including a single space character), echoes out a custom message with an unknown array var value from the query result $info
What I would do is, before anything has been processed in the script, check to see if your session is still active. Normally I would set session vars as soon as the session started. That means that all of the information you are querying in the above script should already be in a series of session vars. So when the page loads, if there is no session values, run a generic interface or redirect back to the place where the session would start. If a session does exist, instead of hitting the database again, use your previously set session vars. This reduces calls to the database server and reduces load on the server as well as reduces processing time.
Posted: Wed Apr 12, 2006 1:42 pm
by mthierfelder
Yes, Everah your making correct assumptions all the way along about the included files, what they do and how they work.
In conn.php, there is this:
So, the session gets started here, and this is included for all pages. What you are saying about using sessions instead of a database call makes good sense to me. I guess the only place I'm not sure about is how the session id will relate to the identity of the person who is logged in, that is, how do I say in code:
while ($info = mysql_fetch_array($data) {
eval until agent_id matches session id or whatever }
The session id is a long string of numbers, while the agent id tends to be a 1 or 2 digit number. Maybe if you could give me some pointers on how to set up a session variable properly that would be usefull in the end script that I am using, I could finish it off?
Posted: Wed Apr 12, 2006 3:24 pm
by RobertGonzalez
What I usually do is create a series of functions that create your session, update information in the database and set session vars. I include this in a utility file which is included in all pages in the site. I always make sure there is nothing included before the session code to make sure that nothing will cause the session to crap out.
Depending on the needs you have for your sessions, you should develop your session functions to return an array of data that includes all of the pieces of data that you will use throughout your script. A pretty good example of this process is phpBB's session handling code. It can be adapted to suit your needs I'm sure.