Forums

PHP

This topic is locked

session variable

Posted 12 Feb 2005 13:49:23
1
has voted
12 Feb 2005 13:49:23 walter napoleon posted:
1) I have a login page which writes username and password into a database. After login the server behavior swich to a login_ok.php or a login_fail.php. Thusfar every works fine. But in the login_ok I want to use the session variable MM_username created by the login page. In the datababindings panel I created a session_variable MM_username and dragt it into the page. This won't work. What do I do wrong?
2) Can I use the session variable MM_UserGroup to display the access level of the user?


This my login page I ommitted the php tags:
php require_once('Connections/conn_Dmenu.php');

mysql_select_db($database_conn_Dmenu, $conn_Dmenu);
$query_rstUsers = "SELECT * FROM tblUsers";
$rstUsers = mysql_query($query_rstUsers, $conn_Dmenu) or die(mysql_error());
$row_rstUsers = mysql_fetch_assoc($rstUsers);
$totalRows_rstUsers = mysql_num_rows($rstUsers);


// *** Validate request to login to this site.
session_start();

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

if (isset($_POST['Username'])) {
$loginUsername=$_POST['Username'];
$password=$_POST['UserPw'];
$MM_fldUserAuthorization = "UserStatus";
$MM_redirectLoginSuccess = "login_ok.php";
$MM_redirectLoginFailed = "login_fail.php";
$MM_redirecttoReferrer = false;
mysql_select_db($database_conn_Dmenu, $conn_Dmenu);

$LoginRS__query=sprintf("SELECT UserName, UserPw, UserStatus FROM tblUsers WHERE UserName='%s' AND UserPw='%s'",
get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));

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

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

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

//register the session variables
session_register("MM_Username";
session_register("MM_UserGroup";

if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
*************************************************************
This is my login_ok page:

You are loged in as : php echo $_SESSION['MM_username']; </p>
<p>Your status is : </p>
<p><a href="index.php">Back to homepage</a> </p>

Replies

Replied 13 Feb 2005 21:41:02
13 Feb 2005 21:41:02 Iain Stewart replied:
I do something similar.

I create a log in page, which logs them onto a page, to push them away from the page use a restrict acces to page, so that if they haven't logged on they get pushed to the log on page.

If successfull log on they get the page you want the form to send them to.
Once there I use a recordset which is filtered by session varible to filter the users table to list just there info which I then print to the page in the normal way, works a treat for me.

hope that makes sense.

Iain

head hurts, 'mental note to self, stop banging head !'
Replied 13 Feb 2005 22:22:08
13 Feb 2005 22:22:08 walter napoleon replied:
That's my point: how do I activate the session variable MM_Username (this is the default name for the session variable) after the user succesfully logged in? I thougt this happens automatically!
When I print the session variable in the login_ok page there is nothing to see!
I also tried to define the session variable = MM_Username in the login page but this works not also. <img src=../images/dmxzone/forum/icon_smile_sad.gif border=0 align=middle>
Replied 15 Feb 2005 23:50:03
15 Feb 2005 23:50:03 Iain Stewart replied:
Once they login and go to the login_ok page the varible should be started.

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

which you have declared.

so on the login_ok page created a recordset and the recordset is filtered by session varible MM_Username
spelling must be exact with upper and lower case.

The just drag the require details to the page from the recordset.

Iain

head hurts, 'mental note to self, stop banging head !'
Replied 18 Feb 2005 23:52:55
18 Feb 2005 23:52:55 walter napoleon replied:
I've created a recordset on the log_ok page with the filter = MM_Username - still it does not work. ??
Replied 19 Feb 2005 00:13:02
19 Feb 2005 00:13:02 walter napoleon replied:
It works half after I manually inserted the following code:

session_start();"

and dragged the session variables from the databindings panel:
echo $_SESSION['MM_Username']; ?&gt;"
echo $_SESSION['MM_UserAuthorization']; ?&gt;

It works for the username but not for the UserAuthorisation.
Why do I have to add the code manually. Is this a bug in DW2004?



Replied 19 Feb 2005 01:36:07
19 Feb 2005 01:36:07 Chris Charlton replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>It works half after I manually inserted the following code:
<pre id=code><font face=courier size=2 id=code>session_start();</font id=code></pre id=code>

and dragged the session variables from the databindings panel:
<pre id=code><font face=courier size=2 id=code>echo $_SESSION['MM_Username'];
echo $_SESSION['MM_UserAuthorization'];</font id=code></pre id=code>

It works for the username but not for the UserAuthorisation.
Why do I have to add the code manually. Is this a bug in DW2004?<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Try <i>MM_UserGroup</i>, since that's DW's default SESSION var for user authorisation (usergroup).

It's not a bug in DW (not adding "session_start();" by default), in most cases dragging in a SESSION var should add/prompt the user for rapid development sake. Reguardless, now you know "session_start();" <i>has</i> to be declared on any page that uses SESSION vars, now you'll know to keep an eye out for it. <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle> Live and learn.

Hope that helps.

~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>
Replied 19 Feb 2005 13:23:43
19 Feb 2005 13:23:43 walter napoleon replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
It's not a bug in DW (not adding "session_start();" by default), in most cases dragging in a SESSION var should add/prompt the user for rapid development sake. <hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

I tried several pages and DW never added/prompted for a 'session_start' when dragging the SESSION var. Seems I cannot rely 100% on DW' code and have to learn myself some php/mysql basics. <img src=../images/dmxzone/forum/icon_smile_blush.gif border=0 align=middle>
Replied 19 Feb 2005 22:50:54
19 Feb 2005 22:50:54 Chris Charlton replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote><BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>It's not a bug in DW (not adding "session_start();" by default), in most cases dragging in a SESSION var should add/prompt the user for rapid development sake.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

I tried several pages and DW never added/prompted for a 'session_start' when dragging the SESSION var. Seems I cannot rely 100% on DW' code and have to learn myself some php/mysql basics. <img src=../images/dmxzone/forum/icon_smile_blush.gif border=0 align=middle><hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Are you applying the <i>Server-Behavior</i> <b>Restrict Access to Page</b>? Because when you do it'll add the "session_start();" line near the top. I've forgotten to add this required <i>Server-Behavior</i> when rushing to get a page done. <img src=../images/dmxzone/forum/icon_smile_tongue.gif border=0 align=middle> <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>
Replied 23 Feb 2005 16:31:27
23 Feb 2005 16:31:27 walter napoleon replied:
mmm.. a lot of code with this server behavior but the session_start is in it.
I think I better add it manually.

Thanks very much

Reply to this topic