andy_b_1502 Posted October 9, 2011 Share Posted October 9, 2011 Could somebody help to pint me in the right direction where to look for the correct syntax for my problem. The user logs in and is sent to the members area, their username is echoed out to display that they are logged in, my problem is that it is displaying all the users in my database (currently 2 usernames). I would just like to have the current users username displayed. <?php $con = mysql_connect("server","username", "password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT username FROM users" ); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>";; echo "</tr>";} echo "</table>"; ?> What sholud i research to perform this task properly? thank you. Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/ Share on other sites More sharing options...
awjudd Posted October 9, 2011 Share Posted October 9, 2011 You need to add a WHERE clause to your query restricting it to only selecting the one row that you want (depending on their logged in information). ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277452 Share on other sites More sharing options...
andy_b_1502 Posted October 9, 2011 Author Share Posted October 9, 2011 Thanks, something on the lines of this: $result = mysql_query("SELECT username FROM users WHERE username='$username'"); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>";; echo "</tr>";} echo "</table>"; ?> i would like it to take their username value from when they click log-in and it display in the memebr's area? Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277466 Share on other sites More sharing options...
awjudd Posted October 9, 2011 Share Posted October 9, 2011 If you already have the username in a variable (i.e. $username) then why are you querying the database for it? ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277468 Share on other sites More sharing options...
andy_b_1502 Posted October 9, 2011 Author Share Posted October 9, 2011 To display the username of the user. the log in script stores the username in a variable i want it to be displayed on the memebers area of the site. should i use session like this: <?php $con = mysql_connect("removalspacecom.ipagemysql.com","removalspace", "123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("removalspacelogin", $con); $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; ?> this is the log-in script that takes the $username: <?php /* "Warning: Cannot modify header information - headers already sent by " To avoid the header error , give value zero to $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; */ $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; ob_start(); if (isset($_POST['submitted']) && !empty($_POST['myusername']) && !empty($_POST['mypassword'])) { //check for submit button click $username = $_POST['myusername']; $password = $_POST['mypassword']; // NOTE - quick check $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; // proceed with rest of code $self = $_SERVER['PHP_SELF']; $referer = $_SERVER['HTTP_REFERER']; #if either form field is empty return to the log-in page if( (!$username ) or (!$password ) ){ header( "Location:$referer" ); exit(); } #connect to mysql $conn = @mysql_connect( "server", "username", "password" ) or die( "Could not connect" ); #select database $rs = @mysql_select_db( "database", $conn ) or die ( "Could not select db" ); #create query $sql="select * from users where username = '". $username."' and upassword = '".$password."'"; #execute query $rs = mysql_query( $sql, $conn ) or die( mysql_error() ); if($rs){ $_SESSION['username'] = $username;} #get number of rows that match username and password $num = mysql_numrows( $rs ); #if there is a match the log-in is done if( $num != 0 ){ header( "Location:members_area.php" ); } else { header( "Location:$referer" ); exit(); }} else { ?> Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277474 Share on other sites More sharing options...
awjudd Posted October 9, 2011 Share Posted October 9, 2011 Then yes, use the session variable. You shouldn't hit the database with queries if not necessary ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277476 Share on other sites More sharing options...
andy_b_1502 Posted October 9, 2011 Author Share Posted October 9, 2011 Thanks juddster. I wonder if you or anyone could help me with my original problem to displaying the username, currently with the session it displays "Hello:" only so somethings going wrong somewhere it's just knowing where to look? Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277557 Share on other sites More sharing options...
awjudd Posted October 10, 2011 Share Posted October 10, 2011 $_SESSION['username'] = $username; You are overwriting your previous value with an empty string. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277629 Share on other sites More sharing options...
andy_b_1502 Posted October 10, 2011 Author Share Posted October 10, 2011 i am? Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277780 Share on other sites More sharing options...
awjudd Posted October 10, 2011 Share Posted October 10, 2011 Yes, that is an assignment and if it is on a page that doesn't have $username defined then it will clear the value. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277781 Share on other sites More sharing options...
andy_b_1502 Posted October 10, 2011 Author Share Posted October 10, 2011 Okay, is there a way of taking the username from the login script i posted earlier and using that to display it on the next page (with the session script)? Sorry i should have asked that question in the first instance, that is what i need isnt it. Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277792 Share on other sites More sharing options...
awjudd Posted October 10, 2011 Share Posted October 10, 2011 If in your login script you have $_SESSION['username']=$username; then anywhere else in the application (assuming you called session_start()) you can access it through $_SESSION['username']. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1277798 Share on other sites More sharing options...
andy_b_1502 Posted October 11, 2011 Author Share Posted October 11, 2011 Thanks, i called session start () at line 1, so it puts it before anything to the browser, <?php session_start(); ?> then i take it it should display the username where ever i want it to using: <?php $con = mysql_connect("removalspacecom.ipagemysql.com","removalspace","123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("removalspacelogin", $con); $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; mysql_close($con); ?> but im getting header error? what do you think i could to do to stop this error from occuring? specific error code is: Warning: session_start() [function.session-start]: open(/home/users/web/b109/ipg.removalspacecom/cgi-bin/tmp/sess_1719827ba3cdce3c8642d3ff0bd404fc, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2 Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278147 Share on other sites More sharing options...
garteth Posted October 11, 2011 Share Posted October 11, 2011 try this <h1>Welcome <?php echo $_SESSION['SESS_CUSTOMER_NAME'];?></h1> obviously where 'SESS_CUSTOMER_NAME' is the name of the session that you have created. Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278181 Share on other sites More sharing options...
Bazzaah Posted October 11, 2011 Share Posted October 11, 2011 I use this on my site. YOu'd need to adapt it for your own of course. <?php session_start(); // check session variable if (isset($_SESSION['first_name'])) { echo 'Welcome'; echo ", {$_SESSION['first_name']}!"; just change the $_SESSION[] from first_name to whatever you use on your db. btw, I use the isset is to ensure that someone is logged on. The useful code as far as your query goes is {$_SESSION['first_name]}. hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278218 Share on other sites More sharing options...
andy_b_1502 Posted October 12, 2011 Author Share Posted October 12, 2011 Thanks guys! Does that code need to go on line 1, before <!DOCTYPE html PUBLIC.... or where i want the user's username to be displayed? Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278536 Share on other sites More sharing options...
andy_b_1502 Posted October 12, 2011 Author Share Posted October 12, 2011 Im having a little trouble getting this. Ive put the session_start (); at the top of the html page at line 1 and i get headers already sent error. I also placed this code where i would like the username to be displayed: <?php session_start (); $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; ?> But i get the same error message, i wonder whats happening here? Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278676 Share on other sites More sharing options...
Bazzaah Posted October 12, 2011 Share Posted October 12, 2011 Is the problem with way you call the $_SESSION? should it not be something like? echo '<div class="medium">Welcome'; echo ", {$_SESSION['first_name']}!"; Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278714 Share on other sites More sharing options...
andy_b_1502 Posted October 12, 2011 Author Share Posted October 12, 2011 The field name in my table "users" is called "username" That is the name of the $_session created when they log-in. $_session['username'] If i call session like: echo '<div class="medium">Welcome'; echo ", {$_SESSION['first_name']}!"; Is doesn't give me any header errors, however it doesn't display the users username either. It reads: Welcome, ! Where it should read: Welcome, (username)! Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278799 Share on other sites More sharing options...
Bazzaah Posted October 13, 2011 Share Posted October 13, 2011 well that's progress of sorts! Perhaps you could try using isset function to see whether the $_SESSION[] is set? http://php.net/manual/en/function.isset.php I use it like this; if (isset($_SESSION['first_name'])) { echo '<div class="medium">Welcome'; echo ", {$_SESSION['first_name']}!"; echo '</div>'; } else { include ('includes/header.html'); echo '<h1><p>You are not logged in.</p></h1><br>'; echo '<h2><p>If you want to access the members\' area, please register or log in.</p></h2>'; } ?> Maybe there's something wrong with your log-in script if $_SESSION[] is not being set? Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278894 Share on other sites More sharing options...
andy_b_1502 Posted October 13, 2011 Author Share Posted October 13, 2011 Possibly? thanks anyway i think i'll just leave it for now until the future. Quote Link to comment https://forums.phpfreaks.com/topic/248744-display-login-name/#findComment-1278978 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.