PDA

View Full Version : Session Variables in PhP using DWCS3


Ravingmonkey
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

RM

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

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

RM

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

Ravingmonkey
10-03-2008, 06:18 PM
<?php require_once('../Connections/WhykeUFC.php'); ?>
<?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";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
?>
<?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['textfield'])) {
$loginUsername=$_POST['textfield'];
$password=$_POST['textfield2'];
$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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<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">
<tr>
<td width="17%"><label>
<input type="text" name="textfield" id="textfield" />
uname</label></td>
<td width="75%"></td>
<td width="4%">&nbsp;</td>
<td width="4%">&nbsp;</td>
</tr>
<tr>
<td><label>
<input type="text" name="textfield2" id="textfield2" />
password
</label></td>
<td><label>
<input type="submit" name="button" id="button" value="login" />
</label></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
<p>&nbsp;</p>
<p>&nbsp; </p>
</body>
</html>

qxface
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?

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