Skip to main content
Participating Frequently
October 16, 2012
Answered

Help with session variable please - CS5.5 PHP

Hi all,

I am needing a little assistance with using a session variable and hope that someone may point me in the right direction.

I have created a PHP page that uses the Dreamweaver 'User Authentication' feature, and the basics of this works fine, directing a user to the correct pages depending on whether they are or are not a valid user. I would like however to personalise the 'valid user' page with the persons Username as entered in the User Authentication table....a seemingly simple task using a session variable, but one that I just don't seem to be able to get working!

The code generated for the UA on page 1 is as follows:

<?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['txtfirst_name'])) {

  $loginUsername=$_POST['txtfirst_name'];

  $password=$_POST['txtsurname'];

  $MM_fldUserAuthorization = "";

  $MM_redirectLoginSuccess = "member_update.php";

  $MM_redirectLoginFailed = "login.php";

  $MM_redirecttoReferrer = false;

  mysql_select_db($database_panto, $panto);

 

  $LoginRS__query=sprintf("SELECT firstname, surname FROM web_access WHERE firstname=%s AND surname=%s",

    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

  

  $LoginRS = mysql_query($LoginRS__query, $panto) or die(mysql_error());

  $loginFoundUser = mysql_num_rows($LoginRS);

  if ($loginFoundUser) {

     $loginStrGroup = "";

   

    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;         

    if (isset($_SESSION['PrevUrl']) && false) {

      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];   

    }

    header("Location: " . $MM_redirectLoginSuccess );

  }

  else {

    header("Location: ". $MM_redirectLoginFailed );

  }

}

?>

Firstly, the text highlighted in red above appears to be setting the session variable that I require. Is This correct?

If so, what is the code that I need to place in page 2 to use that session variable? or

Do I need to do something else on page 1 to properly assign the session variable?

Would really appreciate your expertise

Mark

This topic is closed to new replies. Start a new post to keep the conversation going.
David_Powers
David_PowersCorrect answer
Inspiring
October 16, 2012

It looks as though you've set the columns incorrectly in the User Authentication server behavior. This is the SQL query that checks the user's credentials:

  $LoginRS__query=sprintf("SELECT firstname, surname FROM web_access WHERE firstname=%s AND surname=%s",

    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

You're looking for firstname and surname, whereas you should be looking for the user's login name and password.

$_SESSION['MM_Username'] is a session variable that stores the user's login name. To use it in a subsequent page, all that's needed is for the page to begin with session_start(). You can then echo the value to display it.

If you want to display the person's real name, you need to create a recordset in the second page using $_SESSION['MM_Username'] to find the firstname and surname. Alternatively, you can edit the existing code like this (I've copied only part of it):

  $LoginRS__query=sprintf("SELECT firstname, surname FROM web_access WHERE firstname=%s AND password=%s",

    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

  $LoginRS = mysql_query($LoginRS__query, $panto) or die(mysql_error());

  $loginFoundUser = mysql_num_rows($LoginRS);

  if ($loginFoundUser) {

     $loginStrGroup = "";

    $row = mysql_fetch_assoc($LoginRS);

     $_SESSION['full_name'] = $row['firstname'] . ' ' . $row['surname'];

    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;

You can then use $_SESSION['full_name'] in a subsequent page that begins with session_start().

3 replies

David_Powers
David_PowersCorrect answer
Inspiring
October 16, 2012

It looks as though you've set the columns incorrectly in the User Authentication server behavior. This is the SQL query that checks the user's credentials:

  $LoginRS__query=sprintf("SELECT firstname, surname FROM web_access WHERE firstname=%s AND surname=%s",

    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

You're looking for firstname and surname, whereas you should be looking for the user's login name and password.

$_SESSION['MM_Username'] is a session variable that stores the user's login name. To use it in a subsequent page, all that's needed is for the page to begin with session_start(). You can then echo the value to display it.

If you want to display the person's real name, you need to create a recordset in the second page using $_SESSION['MM_Username'] to find the firstname and surname. Alternatively, you can edit the existing code like this (I've copied only part of it):

  $LoginRS__query=sprintf("SELECT firstname, surname FROM web_access WHERE firstname=%s AND password=%s",

    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

  $LoginRS = mysql_query($LoginRS__query, $panto) or die(mysql_error());

  $loginFoundUser = mysql_num_rows($LoginRS);

  if ($loginFoundUser) {

     $loginStrGroup = "";

    $row = mysql_fetch_assoc($LoginRS);

     $_SESSION['full_name'] = $row['firstname'] . ' ' . $row['surname'];

    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;

You can then use $_SESSION['full_name'] in a subsequent page that begins with session_start().

Translate
Participating Frequently
October 16, 2012

Thanks very much David....the use of Echo was the bit I had missed and my application is now working.

I can understand why you felt my code to be incorrect, however I was actually striving to look for first name and surname, and it is all working fine now. Cheers for your valuable assistance

Mark

Translate
sudarshan.t
Inspiring
October 16, 2012

I have moved this thread to Developing server-side applications in Dreamweaver forum for better responses.

Translate