Session Variable

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
thumbliner
Forum Newbie
Posts: 6
Joined: Mon Apr 25, 2011 1:21 pm

Session Variable

Post by thumbliner »

Need help declaring some session variable guys.

I have a login form where the member enters his
1. Pilot Callsign
2. Password

I want to declare that Pilot Callsign as the session variable on authentication.

Using that Pilot Callsign session variable, I will fetch data from the database relevant to his profile.

I already have the whole login page coded along with the restricted access pages (not coded by me).

Check this out

1. Page is coded like this and working PERFECTLY
---

Code: Select all

<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['pilot_callsign'])) {
  $loginUsername=$_POST['pilot_callsign'];
  $password=$_POST['password'];
  
  mysql_select_db($database_brn_system, $brn_system);
  	
  $LoginRS__query=sprintf("SELECT pilot_callsign, password, staff_level, firstname FROM pilots WHERE activated = 1 AND pilot_callsign=%s AND password=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $brn_system) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'staff_level');
    
	if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	     
?>
---

2. As you can see, there already is a session variable declared for Pilot Callsign
But on the next page "Restricted Access Page", when I try to call this same Session Variable, it doesn't work.

I tried doing this
<?php echo $_SESSION['MM_Username'] ?>

Moreover, I even tried to fetch data from the table like this -

SELECT * FROM pilots WHERE pilot_callsign=$_SESSION['MM_Username']

Doesn't work :-\
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Session Variable

Post by tr0gd0rr »

Do you call session_start() on the other pages? You must call session_start() on every page before accessing anything in $_SESSION.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Session Variable

Post by social_experiment »

Can you paste the code on the restricted access page
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
thumbliner
Forum Newbie
Posts: 6
Joined: Mon Apr 25, 2011 1:21 pm

Re: Session Variable

Post by thumbliner »

Chill out. Problem solved. Thanks for your time guys. :)
Post Reply