dellrat Posted May 16, 2008 Share Posted May 16, 2008 Hi guys, Am new here and this is my first post. I have 2 php file login and menu. In login I have queried my dbase and if the user logs in, menu.php will be displayed. However, I am struggling to display session variables which were set originally in login.php in menu.php. I would like to display the username in menu.php. Something like "Welcome (User)". Would appreciate help on this as I am rather in PHP. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/105928-need-some-help/ Share on other sites More sharing options...
conker87 Posted May 16, 2008 Share Posted May 16, 2008 Make sure you have session_start(); at the top of both pages. Replace <?php $_SESSION[username]; ?> with <?php echo $_SESSION[username]; ?> Also, there's nothing in the login.php Link to comment https://forums.phpfreaks.com/topic/105928-need-some-help/#findComment-542832 Share on other sites More sharing options...
dellrat Posted May 16, 2008 Author Share Posted May 16, 2008 I had it at the start of the files. Sorry it seems the login file was not uploaded properly. I have copied it below login.php <?php session_start(); $_SESSION['db_is_logged_in'] = true; include 'config.php'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); if (isset($_POST['offline_folder'])){ $_SESSION['offline_folder'] = $_POST['offline_folder']; } $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { include 'opendb.php'; $username = $_POST['txtUserId']; $password = $_POST['txtPassword']; // check if the user id and password combination exist in database $sql = "SELECT Username, Password FROM test_user_login WHERE Username = '$username' AND Password = ('$password')"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { // the user id and password match, // set the session $_SESSION['db_is_logged_in'] = true; $row = mysql_fetch_row($result); $user_id = $row[2]; $_SESSION['user_id'] = $user_id; // retrieve first name from logged in user $sql = "SELECT Surname, Given_Names FROM test_user_login WHERE Username = '$user_id'"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); $row = mysql_fetch_row($result); $_SESSION['Given_Names'] = $row[1]; // after login we move to the main page header('Location: menu.php'); exit; } else { $errorMessage = 'Sorry, wrong user id / password'; print $errorMessage; } include 'closedb.php'; } ?> <!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>UCW Adelaide Client Information System</title> <style type="text/css"> <!-- .style1 { font-family: Arial, Helvetica, sans-serif; font-size: 55px; font-weight: bold; color: #AD2E25; } .style2 { font-family: Arial, Helvetica, sans-serif; font-size: 12px; } .style4 { font-family: Arial, Helvetica, sans-serif; font-weight: bold; } --> </style> </head> <body> <table width="100%" border="0"> <tr> <td width="74%"><span class="style1">Client Information System</span></td> <td width="26%"><div align="right"><img src="UCW Logo" alt="UCW Logo" /></div></td> </tr> <tr> <td colspan="2" bgcolor="#990000"> </td> </tr> <tr height="300"> <td colspan="2"> <div> <form method="post" name="frmLogin" id="frmLogin"> <table width="240" border="0" align="center" cellpadding="2" cellspacing="2"> <tr> <td width="150" class="Stil5 style4">Username:</td> <td><input name="txtUserId" type="text" id="txtUserId"></td> </tr> <tr> <td width="150" class="Stil5 style4">Password:</td> <td><input name="txtPassword" type="password" id="txtPassword"></td> </tr> <tr> <td colspan="2"><div align="center"><br> <input type="submit" name="btnLogin" value="Login"> </div></td> </tr> </table> </form></td> <td width="25%"> </td> </tr> </table> </td> </tr> <tr> <td colspan="2"><hr color="#AD2E25"> <div align="center" class="style2">Tuesday, May 13, 2008 3:44 PM</div></td> </tr> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/105928-need-some-help/#findComment-543039 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.