SauloA Posted February 12, 2007 Share Posted February 12, 2007 I've created a login in page and I want the login form to appear when a user isn't logged in and hide the login form and displaying the username when the user is logged in. I think I'm close to solving the problem but I'm probably missing some code. Here's my code. I'd appreciate the help. <?php require_once('../Connections/connBlog.php'); ?> <?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['username'])) { $loginUsername=$_POST['username']; $password=$_POST['password']; $MM_fldUserAuthorization = "al_levelid"; $MM_redirectLoginSuccess = "home.php"; $MM_redirectLoginFailed = "failed.php"; $MM_redirecttoReferrer = true; mysql_select_db($database_connBlog, $connBlog); $LoginRS__query=sprintf("SELECT user_id, user_sname, user_pass, al_levelid FROM user_tbl WHERE binary user_sname='%s' AND binary user_pass='%s'", get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); $LoginRS = mysql_query($LoginRS__query, $connBlog) or die(mysql_error()); $LoginArray= mysql_fetch_array($LoginRS); //Creates the array $loginFoundUser = mysql_num_rows($LoginRS); if ($loginFoundUser) { $loginStrGroup = mysql_result($LoginRS,0,'al_levelid'); //declare three session variables and assign them $_SESSION['MM_Username'] = $loginUsername; $_SESSION['MM_UserID'] = $LoginArray['user_id']; $_SESSION['MM_UserLevel'] = $LoginArray['al_levelid']; $_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=iso-8859-1" /> <title>Blog Login</title> <link href="mystyles.css" rel="stylesheet" type="text/css" /> </head> <body> <p><a href="home.php">Reviews</a> | <a href="users.php">Users</a> | <a href="shops.php">Shops</a> | <a href="customers.php">Customers</a> | <a href="subscriptions.php">Subscriptions</a> | <a href="invoices.php">Invoices</a> | <a href="logout.php">Logout</a> </p> <?php if ($_SESSION['MM_Username'] == 0) { ?> <form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="frmLogin" id="frmLogin"> <table> <caption> Login to the site </caption> <tr> <th align="right" scope="row">Username:</th> <td><input name="username" type="text" id="username" size="10" maxlength="10" /></td> </tr> <tr> <th align="right" scope="row">Password:</th> <td><input name="password" type="password" id="password" size="10" maxlength="10" /></td> </tr> <tr> <th align="right" scope="row"> </th> <td><input type="submit" name="Submit" value="Login" /></td> </tr> </table> </form> <?php } else { ?> <p>Welcome <?php echo $_SESSION['MM_Username']; ?></p> <?php } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted February 12, 2007 Share Posted February 12, 2007 Well whats actually happening at the moment? Also do you think you could modify your post and put [ code] [ /code] tags around it so we get syntax highlighting; makes it a lot easier to read. One little problem i can spot, however, is this part: if (!isset($_SESSION)) { session_start(); } You should put session_start() before any attempt to do anything at all with sessions. As far as i can tell, the way you've set it up at the moment will probably prevent the sessions from being set when the person trys to log in. so just change it to session_start(); on its own. No need for the if statement. Quote Link to comment Share on other sites More sharing options...
SauloA Posted February 12, 2007 Author Share Posted February 12, 2007 The problem isn't in the: if (!isset($_SESSION)) { session_start(); } The problem is most likely in this region near the bottom: <?php if ($_SESSION['MM_Username'] == 0) { ?> <form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="frmLogin" id="frmLogin"> <table> <caption> Login to the site </caption> <tr> <th align="right" scope="row">Username:</th> <td><input name="username" type="text" id="username" size="10" maxlength="10" /></td> </tr> <tr> <th align="right" scope="row">Password:</th> <td><input name="password" type="password" id="password" size="10" maxlength="10" /></td> </tr> <tr> <th align="right" scope="row"> </th> <td><input type="submit" name="Submit" value="Login" /></td> </tr> </table> </form> <?php } else { ?> <p>Welcome <?php echo $_SESSION['MM_Username']; ?></p> <?php } ?> Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 12, 2007 Share Posted February 12, 2007 The problem isn't in the: if (!isset($_SESSION)) { session_start(); } Could you explain where you got to this conclusion? If I remember correctly $_SESSION is a predefined superglobal variable, meaning it's always set. If you're so confident that it's not wrong, please humor us by replacing it with just session_start(). As well, what output are you getting? We have no idea what's wrong if you don't tell us what output you get. Quote Link to comment Share on other sites More sharing options...
SauloA Posted February 12, 2007 Author Share Posted February 12, 2007 I change: if (!isset($_SESSION)) { session_start(); } into: if (!isset($_SESSION)) { session_start() } and I get an error on my page. When I change it back, I no longer have the error. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 12, 2007 Share Posted February 12, 2007 You just removed the semi-colon, and PHP requires the semi-colon. Change: if(!isset($_SESSION)) { session_start(); } into: session_start(); Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 12, 2007 Share Posted February 12, 2007 To make sure that the session actually has a value in it, try this: print_r($_SESSION); The value you could be storing in the variable could be white space, so it is actually printing out something, but since it is possibly a space, then that would be what your seeing. Quote Link to comment Share on other sites More sharing options...
SauloA Posted February 12, 2007 Author Share Posted February 12, 2007 Well, I changed: if(!isset($_SESSION)) { session_start(); } into: session_start(); and I get no errors and the page works fine. But, that wasn't the issue I was trying to solve. I want the login form to appear when a user isn't logged in. If a user is logged in then the form should be hidden and "Welcome username" should appear. Quote Link to comment Share on other sites More sharing options...
SauloA Posted February 12, 2007 Author Share Posted February 12, 2007 I'm essentially trying to check is there is anything in my session variable. If my session variable is empty then it should display the login form. If there is something in the session variable then the login form should no longer display and "Welcome 'username' " should appear. My " if " statement begins here: <?php if ($_SESSION['MM_Username'] == 0) { ?> <form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="frmLogin" id="frmLogin"> <table> <caption> Login to the site </caption> <tr> <th align="right" scope="row">Username:</th> <td><input name="username" type="text" id="username" size="10" maxlength="10" /></td> </tr> <tr> <th align="right" scope="row">Password:</th> <td><input name="password" type="password" id="password" size="10" maxlength="10" /></td> </tr> <tr> <th align="right" scope="row"> </th> <td><input type="submit" name="Submit" value="Login" /></td> </tr> </table> </form> <?php } else { ?> <p>Welcome <?php echo $_SESSION['MM_Username']; ?></p> <?php } ?> but I don't know how to do what I explained above. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 if(isset($_SESSION['MM_Username']) && $_SESSION['MM_Username']){ print $_SESSION['MM_Username']; }else{ //Login form. } Quote Link to comment Share on other sites More sharing options...
SauloA Posted February 12, 2007 Author Share Posted February 12, 2007 That code worked perfectly jesirose. So far most or all of the problems I've posted on PHP Freaks have been solved by you. Thanks a bunch. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.