session variable help for newbie?

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!

Moderator: General Moderators

Post Reply
dennis73
Forum Newbie
Posts: 10
Joined: Wed Oct 17, 2007 2:51 am

session variable help for newbie?

Post by dennis73 »

Hi

I am developing an app to allow parents to access their children's academic reports online. I previously did this in asp.net and used the pupil id no. as the session variable to limit all the subsequent recordsets to show data only for that pupil. Can anyone advise on how I can do this in PHP? The pupil id is seffectively the password, and I want the to carry to the next pages to use in the MySQL queries on those pages. Can I do this or is there a better method? I want to keep security high as I don't want parents to see the wrong reports :oops: !

Thanks all.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Sure, just put it in your session data. ;)

Code: Select all

//must be called at the top of every page you want to use session information on
session_start();

//store student's id
$_SESSION['student_id'] = $student_id;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dennis73
Forum Newbie
Posts: 10
Joined: Wed Oct 17, 2007 2:51 am

Post by dennis73 »

Great, thanks for that. however when i try to call the variable in a MySql query ("SELECT pupilID, UPN, forename, surname FROM pupils WHERE UPN = $_session['UPN']";) I get a bad syntax message for near ['UPN']. UPN is the pupilid number i am using in this case and i have set the variable as you suggested.

Thanks
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You have to concatenate it. ;)

Code: Select all

mysql_query("SELECT pupilID, UPN, forename, surname FROM pupils WHERE UPN = '" . $_SESSION['UPN'] . "'");
However, PHP is case sensitive. Meaning that $_session['upn'] is not the same as $_SESSION['UPN']. $_SESSION must always be capitalized.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dennis73
Forum Newbie
Posts: 10
Joined: Wed Oct 17, 2007 2:51 am

Post by dennis73 »

Great, thanks for your prompt and accurate help! :D
Post Reply