View Full Version : Session Variables in PhP using DWCS3

09-29-2008, 07:36 PM
Hello all,

I have recently purchased a new licence of DWCS3 for my MAC and have now made the jump from ASP pages to PhP, I am having real problems getting my PhP pages to set and remember session variables. A job I could do easily in DW using ASP.

Here is what I have done, created a login screen, opened the session (as I have found on many websites) using a session variable MM_username. Now the login screen works moves me into the 'secure' area and prevents me from doing so if I enter bad credentials.

However I am trying to use the MM_username in the 'secure' area to permit actions in other areas of the site (together with a 'level' setting), however my PhP session is not remembering who the user is and keeps saying "Undefined index: MM_username".. I have searched Google and find nothing to help me progress this.

Am I doing something ridiculous?

Any help would be much appreciated


09-30-2008, 06:21 AM
make sure you have session_start(); at the top of each page where sessions are used.

10-01-2008, 06:26 PM
Thanks Lux, i have tried that! Does not seem to want to work for me at all.


10-01-2008, 06:40 PM
post your code and we can have a look

10-03-2008, 06:18 PM
<?php require_once('../Connections/WhykeUFC.php'); ?>
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
return $theValue;
// *** Validate request to login to this site.
if (!isset($_SESSION)) {

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

if (isset($_POST['textfield'])) {
$MM_fldUserAuthorization = "level";
$MM_redirectLoginSuccess = "in.php";
$MM_redirectLoginFailed = "login.php";
$MM_redirecttoReferrer = true;
mysql_select_db($database_WhykeUFC, $WhykeUFC);

$LoginRS__query=sprintf("SELECT uname, password, level FROM users WHERE uname=%s AND password=%s",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

$LoginRS = mysql_query($LoginRS__query, $WhykeUFC) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {

$loginStrGroup = mysql_result($LoginRS,0,'level');

//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;

if (isset($_SESSION['PrevUrl']) && true) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
header("Location: " . $MM_redirectLoginSuccess );
else {
header("Location: ". $MM_redirectLoginFailed );
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<p>Create a session variable for use on the website;</p>
<form id="login" name="login" method="POST" action="<?php echo $loginFormAction; ?>">
<table width="100%" border="0">
<td width="17%"><label>
<input type="text" name="textfield" id="textfield" />
<td width="75%"></td>
<td width="4%">&nbsp;</td>
<td width="4%">&nbsp;</td>
<input type="text" name="textfield2" id="textfield2" />
<input type="submit" name="button" id="button" value="login" />
<p>&nbsp; </p>

02-26-2009, 06:42 PM
I am having the same problem. My code is almost identical, so I'm guessing that you used Server Behavior>User Authentication>Login User to generate the code.

Did you figure out what was going wrong?

02-27-2009, 12:30 AM
try moving the seesion start code to the top so its the first code on the page