Jump to content
rattlsnak

echo username

Recommended Posts

Question on how to echo username on page.I understand how to do it if the session id is set from using rowaccount from the column 'username' in the table, like this: $user="".$_SESSION['users']; and then <echo $user>, 'user' being the value that was set from the session id, but how do you do it, if the session id was set using another column, like ID, or password, or such?I tried this: echo $_SESSION['username']; but it doesnt work. Thanks

Share this post


Link to post
Share on other sites

In order to allow this feature, you require (of course) a Register script, a Log-in script, and a user must have Logged-in already.When the User logs-in, set the value of their real name or their user name into the session array ie: $_SESSION['username']; by issuing the following (or similar):

	if (empty($errors)) { // If everything's OK.		/* Retrieve the user_id and first_name for 		that email/password combination. */		$query = "SELECT user_id, first_name, level FROM users WHERE email='$e' AND password=SHA1('$p')";				$result = @mysql_query ($query); // Run the query.		$row = mysql_fetch_array ($result, MYSQL_NUM); // Return a record, if applicable.		if ($row) { // A record was pulled from the database.							// Set the session data & redirect.			session_name ('YourVisitID');			session_start(); 			$_SESSION['user_id'] = $row[0];			$_SESSION['first_name'] = $row[1];			$_SESSION['level'] = $row[2];			$_SESSION['login_time'] = time();			// Redirect the user to the loggedin.php page.			// Start defining the URL.			$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);			// Check for a trailing slash.			if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {				$url = substr ($url, 0, -1); // Chop off the slash.			}			// Add the page.			$url .= '/loggedin.php';						header("Location: $url");			exit(); // Quit the script.						} else { // No record matched the query.			$errors[] = 'The email address and password entered do not match those on file.'; // Public message.			$errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message.		}			} // End of if (empty($errors)) IF.

Of course, the session_start function must have been called on the page before the use of the session array.

Share this post


Link to post
Share on other sites

ok, I follow all of that until I get here..// Set the session data & redirect. session_name ('YourVisitID'); session_start(); $_SESSION['user_id'] = $row[0]; $_SESSION['first_name'] = $row[1]; $_SESSION['level'] = $row[2]; $_SESSION['login_time'] = time();I know how to do everything above and below this code. But I have been setting the session id like this:if ($row){$_SESSION['user'] = $row['username'];}Which is obviously setting the session based on the username, but isn't the whole row captured? (This has been working fine on the sites Ive built where only one thing was required for an echo statement, like a username or a user level.)I thought since the whole row is captured, that i could simply echo $_SESSION['username'], or $_SESSION['emailaddress'] on the same page.So, by naming the session in a session_name function and all the $_SESSION's listed below, you are creating a record inside the session_name?I guess I am confused because i thought you could only hvae one session running per page, and it looks like this way,, you have 4?

Share this post


Link to post
Share on other sites

ok, after looking at this some more, I understand about the number of sessions. One session is running. I get that. but I still thought the $row assigned to the fetch_array would pull in the entire row, and you could simply pick what field you wanted out of it, but by your example, and now mine, as i tried it and it works. I think what i was missing was that $_SESSION['xx'] is actually storing a variable whereas I was thinking that it was setting the actual session id , if that makes sense. OK, I'm sure that was php 101 that I misunderstood!It took your example to ring the bell though! THANKS!One last question on your example though, is the session_name function absolutely necessary? If I follow that, whenever you run 'session_start();' at the top of the page, your just running a session named 'YourVisitID', correct?Is there then something that needs to run on each page other than 'session_start()'? I have modified my script as I mentioned, and everything works ok except when I add the session_name('xxx') part.

Share this post


Link to post
Share on other sites

session_start is required to enable session data and to provide a semi_persistent data supply from page to page. If you fail to perform the session_start on subsequent pages, you will lose the data in the session array, so use it on all pages while the user is logged-in. session data is lost if the Browser is closed, too.session_name is optional. I used it there as an example only to modify the session name from the php default (php_sess_id) or something generic like that. It needs to be called before session_start, if I recall correctly.An example of session_id is something like this: 9f53f1dc066fc1f785784304b3d2f45f and that data is randomly generated by php and is a hashed value which makes no sense to anyone.

Share this post


Link to post
Share on other sites

Yes I understand the session_start(), it was the session_name() that I didnt understand. When I use the above code without the session_name it works fine, but when i added the _name portion it wont work which is what was puzzling me.As always, Thanks for your help!

Share this post


Link to post
Share on other sites

I confess I'm a little confused about session_name() myself. What is the purpose? When would I need to access it, and when would I want to change it? The examples I find keep assuming that I know why.

Share this post


Link to post
Share on other sites

Sure, but why? Why is it in the script you posted? At first glance, it seems to defeat the whole purpose of sessions, so I must be missing something.

Share this post


Link to post
Share on other sites

Some people like to see a Session name they recognize when they check the values of the Session ID's I guess.Using the FF3 Web Developer's Tool, this w3schools Forum sets 8 different cookies for various purposes and each of them use a different name.ie: forum_read, ipb-myass-div, ipb_stronghold, member_id, pass_hash, etc. Each cookie is named, presumably, to advise the system and/or User the purpose of each cookie.I use it in that module as an identifier of the particular script of a small package I am coding right now. I'll likely remove the Session_name () once the package is developed.I was concerned about testing multiple modules on localhost, and having the sessions interfering with one another, so I've given different session names to each module for testing purposes. Seems to work okay.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...