designedfree4u Posted March 19, 2008 Share Posted March 19, 2008 I Created a login area and members page, which works. What i want is for the username to be displayed. From what i gather session would be the awnser but its not working for me heres what i got. login page <?php session_start(); $_SESSION['username'] = $_POST['username']; $user_area_location = 'account.php'; // Location of the user area // Connect to MySQL database: $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $error = array(); if($_GET['action']) { switch($_GET['action']) { case 'logoff': unset($_SESSION['loggedIn']); array_push($error, 'You were logged off.'); break; } } if(!$error) { if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); } if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); } } if(!$error){ $result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\''); if($row = @mysql_fetch_row($result)) { $_SESSION['loggedIn'] = true; header('Location: '.$user_area_location); die('<a href="'.$user_area_location.'">Go to your user account</a>'); }else{ array_push($error, 'The credentials you provided were not correct'); } } ?> <!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>Login</title> </head> <body> <table width="284" height="162" border="0" cellpadding="0" cellspacing="2"> <form method="post" action="login.php"> <?php if(isset($error) && $error) { ?> <tr> <td colspan="2"> <ul> <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?> </ul></td> </tr> <?php } ?> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Login!" /></td> </tr> <tr> <td colspan="2">Not a User? <a href="register.php">Register Here</a></td> </tr> </form> </table> </body> </html> Members area <?php session_start(); session_start(); $_SESSION['username'] = $_POST['username']; if(!isset($_SESSION['loggedIn'])) { header('Location: login.php'); die('<a href="login.php">Login first!</a>'); } ?> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <p> </p> <table width="791" height="515" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="205" bgcolor="#FFFFFF"> <? echo $session_username ?> </td> <td width="198" bgcolor="#FFFFFF"><img src="japimages/addfiles.jpg" width="150" height="106"></td> <td width="236" bgcolor="#FFFFFF"><img src="japimages/deletefiles.jpg" width="150" height="106"></td> <td width="152" height="135" colspan="3" bgcolor="#FFFFFF"> </td> </tr> <tr> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"><img src="japimages/adduser.jpg" width="150" height="99"></td> <td bgcolor="#FFFFFF"><img src="japimages/deleteuser.jpg" width="150" height="99"></td> <td height="99" colspan="3" bgcolor="#FFFFFF"> </td> </tr> <tr> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> </td> <td height="150" colspan="3" bgcolor="#FFFFFF"> </td> </tr> <tr> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> <p><a href="login.php?action=logoff">Log off</a> </p></td> <td colspan="3" bgcolor="#FFFFFF"> <div align="center"> <p> </p> </div></td> </tr> </table> <p> </p> </body> </html> Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/ Share on other sites More sharing options...
lemmin Posted March 19, 2008 Share Posted March 19, 2008 Try setting a session_save_path() before session_start() and make sure that your server has write access to that folder. Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-495990 Share on other sites More sharing options...
cooldude832 Posted March 19, 2008 Share Posted March 19, 2008 first off don't suppress mysql errors they are a good thing. secondly take the data in $row in your login function area and attach it to sessions i.e $row = mysql_fetch_assoc($result); $_SESSION['Username'] = $row['Username']; and then you can recall it for the life of the session Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-495991 Share on other sites More sharing options...
BlueSkyIS Posted March 19, 2008 Share Posted March 19, 2008 yes, you appear to set $_SESSION['username'] = $_POST['username']; at the top of each page. if there is no POST['username'], there will be no $_SESSION['username']. Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-495994 Share on other sites More sharing options...
designedfree4u Posted March 19, 2008 Author Share Posted March 19, 2008 i placed the code like this but still no succsess login.php <?php session_start(); $row = mysql_fetch_assoc($result); $_SESSION['username'] = $row['username']; $user_area_location = 'account.php'; // Location of the user area // Connect to MySQL database: $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $error = array(); if($_GET['action']) { switch($_GET['action']) { case 'logoff': unset($_SESSION['loggedIn']); array_push($error, 'You were logged off.'); break; } } if(!$error) { if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); } if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); } } if(!$error){ $result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\''); if($row = @mysql_fetch_row($result)) { $_SESSION['loggedIn'] = true; header('Location: '.$user_area_location); die('<a href="'.$user_area_location.'">Go to your user account</a>'); }else{ array_push($error, 'The credentials you provided were not correct'); } } ?> <!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>Login</title> </head> <body> <table width="284" height="162" border="0" cellpadding="0" cellspacing="2"> <form method="post" action="login.php"> <?php if(isset($error) && $error) { ?> <tr> <td colspan="2"> <ul> <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?> </ul></td> </tr> <?php } ?> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Login!" /></td> </tr> <tr> <td colspan="2">Not a User? <a href="register.php">Register Here</a></td> </tr> </form> </table> </body> </html> And then account.php (is this the right way to display the username in the members area?) = <? echo $session_username ?> <?php session_start(); $row = mysql_fetch_assoc($result); $_SESSION['username'] = $row['username']; $user_area_location = 'account.php'; // Location of the user area // Connect to MySQL database: $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $error = array(); if($_GET['action']) { switch($_GET['action']) { case 'logoff': unset($_SESSION['loggedIn']); array_push($error, 'You were logged off.'); break; } } if(!$error) { if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); } if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); } } if(!$error){ $result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\''); if($row = @mysql_fetch_row($result)) { $_SESSION['loggedIn'] = true; header('Location: '.$user_area_location); die('<a href="'.$user_area_location.'">Go to your user account</a>'); }else{ array_push($error, 'The credentials you provided were not correct'); } } ?> <!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>Login</title> </head> <body> <table width="284" height="162" border="0" cellpadding="0" cellspacing="2"> <form method="post" action="login.php"> <?php if(isset($error) && $error) { ?> <tr> <td colspan="2"> <ul> <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?> </ul></td> </tr> <?php } ?> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Login!" /></td> </tr> <tr> <td colspan="2">Not a User? <a href="register.php">Register Here</a></td> </tr> </form> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-495999 Share on other sites More sharing options...
cooldude832 Posted March 19, 2008 Share Posted March 19, 2008 I didn't see the change I suggested Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-496006 Share on other sites More sharing options...
lemmin Posted March 19, 2008 Share Posted March 19, 2008 I didn't see the change I suggested either! Are your sessions working at all? Make sure your web server has write access to the session folder. I think the default is just the root, but I'm not sure. I recommend setting a session folder. Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-496009 Share on other sites More sharing options...
designedfree4u Posted March 19, 2008 Author Share Posted March 19, 2008 sorry new to all this. I created a folder named session and gave it 777 Here is the changes you suggest right? login.php <?php session_start(); $row = mysql_fetch_assoc($result); $_SESSION['username'] = $row['username']; $user_area_location = 'account.php'; // Location of the user area // Connect to MySQL database: $username="jaybirdf_Admin"; $password="607101593"; $database="jaybirdf_RealEstate"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $error = array(); if($_GET['action']) { switch($_GET['action']) { case 'logoff': unset($_SESSION['loggedIn']); array_push($error, 'You were logged off.'); break; } } if(!$error) { if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); } if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); } } if(!$error){ $result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\''); if($row = @mysql_fetch_row($result)) { $_SESSION['loggedIn'] = true; header('Location: '.$user_area_location); die('<a href="'.$user_area_location.'">Go to your user account</a>'); }else{ array_push($error, 'The credentials you provided were not correct'); } } ?> <!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>Login</title> </head> <body> <table width="284" height="162" border="0" cellpadding="0" cellspacing="2"> <form method="post" action="login.php"> <?php if(isset($error) && $error) { ?> <tr> <td colspan="2"> <ul> <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?> </ul></td> </tr> <?php } ?> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Login!" /></td> </tr> <tr> <td colspan="2">Not a User? <a href="register.php">Register Here</a></td> </tr> </form> </table> </body> </html> Account.php <?php session_start(); $_SESSION['username'] = $_POST['username']; if(!isset($_SESSION['loggedIn'])) { header('Location: login.php'); die('<a href="login.php">Login first!</a>'); } ?> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <p> </p> <table width="791" height="515" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="205" bgcolor="#FFFFFF"> <? echo $session_username ?> </td> <td width="198" bgcolor="#FFFFFF"><img src="japimages/addfiles.jpg" width="150" height="106"></td> <td width="236" bgcolor="#FFFFFF"><img src="japimages/deletefiles.jpg" width="150" height="106"></td> <td width="152" height="135" colspan="3" bgcolor="#FFFFFF"> </td> </tr> <tr> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"><img src="japimages/adduser.jpg" width="150" height="99"></td> <td bgcolor="#FFFFFF"><img src="japimages/deleteuser.jpg" width="150" height="99"></td> <td height="99" colspan="3" bgcolor="#FFFFFF"> </td> </tr> <tr> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> </td> <td height="150" colspan="3" bgcolor="#FFFFFF"> </td> </tr> <tr> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"> <p><a href="login.php?action=logoff">Log off</a> </p></td> <td colspan="3" bgcolor="#FFFFFF"> <div align="center"> <p> </p> </div></td> </tr> </table> <p> </p> </body> </html> Still no worky Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-496021 Share on other sites More sharing options...
lemmin Posted March 19, 2008 Share Posted March 19, 2008 You need to use the session_save_path() function to set the path before you use session_start(), as I stated before. You might find some useful information under the documentation: http://us3.php.net/manual/en/ref.session.php Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-496029 Share on other sites More sharing options...
designedfree4u Posted March 19, 2008 Author Share Posted March 19, 2008 Guess i got alot to learn before i try this. Thanks for your help and link. Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-496040 Share on other sites More sharing options...
designedfree4u Posted March 19, 2008 Author Share Posted March 19, 2008 ok how about through cookie right now this works but i want it to display the user that is logged on Login.php session_start(); $username = 'jonny4'; setcookie('username', $username); Account.php echo $_COOKIE['username']; Full code for login.php <?php session_start(); $username = 'jonny4'; setcookie('username', $username); $user_area_location = 'account.php'; // Location of the user area // Connect to MySQL database: $username="jaybirdf_Admin"; $password="607101593"; $database="jaybirdf_RealEstate"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $error = array(); if($_GET['action']) { switch($_GET['action']) { case 'logoff': unset($_SESSION['loggedIn']); array_push($error, 'You were logged off.'); break; } } if(!$error) { if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); } if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); } } if(!$error){ $result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\''); if($row = @mysql_fetch_row($result)) { $_SESSION['loggedIn'] = true; header('Location: '.$user_area_location); die('<a href="'.$user_area_location.'">Go to your user account</a>'); }else{ array_push($error, 'The credentials you provided were not correct'); } } ?> <!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>Login</title> </head> <body> <table width="284" height="162" border="0" cellpadding="0" cellspacing="2"> <form method="post" action="login.php"> <?php if(isset($error) && $error) { ?> <tr> <td colspan="2"> <ul> <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?> </ul></td> </tr> <?php } ?> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Login!" /></td> </tr> <tr> <td colspan="2">Not a User? <a href="register.php">Register Here</a></td> </tr> </form> </table> </body> </html> Instead of jonny4 should it be $result ? Link to comment https://forums.phpfreaks.com/topic/96930-trying-to-display-username-through-session/#findComment-496065 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.