Page 1 of 1

Using session variables to display username

Posted: Thu Apr 08, 2010 3:01 pm
by HopyMSU
Hi all,

Kind of a PHP newbie and trying to display a username (which is an email address) after login, but having problems getting the PHP to actually spit anything out.

I have a login index page that directs to a page with this PHP code:

Code: Select all

       //DB variables taken out of this example

       // Connect to server and select databse.
	mysql_connect("$host", "$username", "$password")or die("cannot connect");
	mysql_select_db("$db_name")or die("cannot select DB");

	// username and password sent from form
	$myusername=$_POST['email'];
	$mypassword=$_POST['password'];

	// To protect MySQL injection (more detail about MySQL injection)
	$myusername = stripslashes($myusername);
	$mypassword = stripslashes($mypassword);
	$myusername = mysql_real_escape_string($myusername);
	$mypassword = mysql_real_escape_string($mypassword);

	$sql="SELECT * FROM $tbl_name WHERE email='$myusername' and password='$mypassword'";
	$result=mysql_query($sql);

	// Mysql_num_row is counting table row
	$count=mysql_num_rows($result);
	// If result matched $myusername and $mypassword, table row must be 1 row

	if($count==1){
	// Register $myusername, $mypassword and redirect to file "/members/tools.php"
	$_SESSION['email'] = $myusername;
	$_SESSION['password'] = $mypassword;
	header( "Location: /members/tools.php" );
	}
		else 
		{
		echo "Wrong Username or Password";
		exit();
		}
?>
When it directs to /members/tools.php, I want it to display: "Login Successful! Welcome [username/email]". Here's the code I have right now that isn't working:

Code: Select all

	
        <?
	// Check if session is not registered , redirect back to main page.
	// Put this code in first line of web page.
	session_start();
	if(!session_is_registered(myusername)){
	header("location:/members/");

	}
	?>
<html>
	<head>
	</head>
	<body>
	<p>Login Successful! Welcome <?php echo $_SESSION['email']; ?></p>
	<p>Select an option below to get started...</p>
		<ul>
			<li><a href="/members/contactlist.php">Team Member Contact List</a></li>
			<li><a href="/members/traininglog.php">Training Log</a></li>
			<li><a href="/members/logout.php">Logout of Member Tools</a></li>
		</ul>
	</body>
</html>
This displays a blank for the $_SESSION['email']. I've tried a few variations, including the $myusername variable and assigning that value to $_SESSION['email'] but to no avail. Any help would be appreciated! Thanks in advance!

Andy

Re: Using session variables to display username

Posted: Thu Apr 08, 2010 3:44 pm
by AbraCadaver
First, there is no session_start() in your first page. Second, don't use session_is_registered(), plus myusername is not a valid session var anyway. Use:

Code: Select all

if(!isset($_SESSION['email'))

Re: Using session variables to display username

Posted: Thu Apr 08, 2010 3:49 pm
by HopyMSU
Shawn,

That solved it - thank you so much!

Re: Using session variables to display username

Posted: Thu Apr 08, 2010 4:37 pm
by HopyMSU
Related question -- what kind of query/session variable do I need to use in order to get another piece of info from the database? For example, if instead of addressing the login as "username/email" I want "first name" ("fname" in the MySQL database).