Theory / Design Question for You Experts

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
kevmatic
Forum Newbie
Posts: 7
Joined: Sat Sep 11, 2010 8:43 am

Theory / Design Question for You Experts

Post by kevmatic »

I'm just starting out and I've been tasked with creating a web page that when a "customer" logs in to a password protected section, he/she is shown a list of approximately 10-20 items that they can order. Here's the kicker, each customer will have a different discount rate. I'm planning on having just a basic page that someone logs onto, this page will check a MySQL database which will contain name,password & discount multiplier. It will then multiply the discount rate by the items purchase price and display the product & adjusted price. The customer will then be able to set each product's quantity and the form will be emailed to me for processing. My question is, how does the web page know who has logged in to apply the correct discount? Is there some sort of function for this? Thanks for any suggestion or correction if my logic is flawed, like I said I'm just starting out & trying to learn this stuff.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Theory / Design Question for You Experts

Post by John Cartwright »

After they have logged in, you generally want to use sessions to persist some data (i.e., user id, username, permission levels, and in your case discount multiplier).

See PHP Sessions
kevmatic
Forum Newbie
Posts: 7
Joined: Sat Sep 11, 2010 8:43 am

Re: Theory / Design Question for You Experts

Post by kevmatic »

Is the "Session" initiated by the log in? For example do I have it say create the session=variable_email_Address when they submit the log-in and then when it gets to the password protected page, have the session variable (email_Address) somehow get the multiplier from the database to render the web page?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Theory / Design Question for You Experts

Post by John Cartwright »

When they log in, you are already performing a look-up on the database to verify that the user exists and is authorized. At that point you want to save all the information you need about the user that your web application needs. So in your case, I would stick the multiplier in a session, and simply reference when you want to perform calculations.

Semi pseudo e.g.,

Code: Select all

// Whenever we use sessions, this generally has to be the first line (specifically before any output/session calls).
session_start();

// However you check if user exists, and return the database row containing his information
$user = validate_user($username, $password); 

if ($user) { // Check if we have a valid user however you handle this
 
    // Now that we have our users information, lets save it for later
    $_SESSION['id'] = $user['id'];
    $_SESSION['username'] = $user['username'];
    $_SESSION['multiplier'] = $user['multiplier'];
    
    //etc
}
In that example, you will have 3 session elements that will be persisted over page requests, so it is no longer necessary to perform a database look-up. You only need to reference $_SESSION['multiplier'], etc. (assuming you started the session).
kevmatic
Forum Newbie
Posts: 7
Joined: Sat Sep 11, 2010 8:43 am

Re: Theory / Design Question for You Experts

Post by kevmatic »

thanks for your patience and expertise, that was a huge help. Now it's off to the tutorials to see the syntax. Thanks for all of your help
kevmatic
Forum Newbie
Posts: 7
Joined: Sat Sep 11, 2010 8:43 am

Re: Theory / Design Question for You Experts

Post by kevmatic »

Also, thanks for the example you provided, it's making sense. Once again thanks for your advice and patience
Post Reply