PHP Session Error

Hi, everyone. I’m having a problem storing session data. I have three pages: a login page, a registration page, and a frame that’s meant to be in the corner of all of the pages. The registration page creates mysql username, password, first name, last name and primary key variables each time someone registers. The login page takes a username and password provided by the user, checks them against the mysql data, and stores the corresponding mysql first and last name variables in a session. The problem I am having is that these variables do not seem to be passed on to the corner frame.

Registration code:
[php]<?php

if($_POST["firstname"]!=null)

    {

		if($_POST["password"]==$_POST["password2"])

			{

				$databaseConnection = mysql_connect ("localhost", "root");
			
				mysql_select_db("users", $databaseConnection);
			
				mysql_query("INSERT INTO usernamesandpasswords (Username, Password, FirstName, LastName)
			
				VALUES ('$_REQUEST[username]', '$_REQUEST[password]', '$_REQUEST[firstname]', '$_REQUEST[lastname]')");
				
				echo 'Thank you for your registration! <a href= login.php >Please login here.</a>';

			}

		else

			{?>

Registration:

First name:

Last name:

Email (this will be your username):

Your passswords did not match.

Password:

Confirm Password:

<?php } } else {?>

Registration:

First name:

Last name:

Email (this will be your username):

Password:

Confirm Password:

<?php }?> [/php]

Login code:
[php]<?php session_start();

if ($_REQUEST['username']!=null)

	{

		$databaseConnection = mysql_connect ("localhost", "root");
			
		mysql_select_db("users", $databaseConnection);

		$sql = "SELECT * FROM usernamesandpasswords WHERE Username='$_REQUEST[username]'";
		
		$result = mysql_query($sql);
		
		$row = mysql_fetch_array($result);
		
		if ($row['Password'] = $_REQUEST['password'])
		
			{

				$_SESSION[username] = $_REQUEST[username];
				
				$_SESSION[name] = $row[FirstName]." ".$row[LastName];
				
				if ($_SESSION[name] == " ")
				
				{?>
Your username or password was incorrect. Please reload this page by clicking here. <?php } else { echo "Hi, ".$_SESSION[name]."!"; } } else {?> Your username or password was incorrect. Please reload this page by clicking here. <?php } } else {?>

Username:

Password:

<?php } ?>[/php]

Frame code:
[php]<?php

if (!isset($_SESSION['name']))

    {

		$session = false;

    }

else

    {
  
		$session = true;

    }

?>

<?php
if ($session)

	{
		echo "Hello, ".$_SESSION['name'].$_SESSION['username']."!";
	}

else

	{
		
		echo "Hello, guest! <a href='login.php' target='main'>Login here</a> or <a href='registration.php' target='main'>sign up here</a>!";
	
	}

?>

[/php]

Thank you all in advance!

What exactly is passed to the corner frame?

The session variable name (containing the mysql variable FirstName, a space, and then the mysql variable LastName) is passed along with the username (which is an email address).

I thought these things weren’t showing up in the frame? I understand you would like them to do so…but I guess I should rephrase and say, what ACTUALLY shows in the frame? Anything?

Aha! My mistake! The variables are not recognized: that is, the isset statement is returning false when it should be true and “Hello, guest! Please sign up here or log in here!” is appearing although the user is signed in.

Okay, so for the time being we can rule out the Frame Code as the source of the problem. That leaves 2 other code snippets to worry about. Let’s consider the first, that is, the registration. When you create a new account, does it actually store in your DB? I don’t know if you’re using PHPMyAdmin or something like that, but can you actually check to see that the accounts are created? If you do that, then we can rule out the first code snippet as the problem, and move onto the login code.

I had a problem storing passwords but it was trivial and now appears to be fixed (the code above does not have this problem). I suppose, then, that it must be the login code.

Do you have session_start(); command there? If not, try it.

I do.

After login, but before you execute the corner code, try outputting the username and password…so echo it right after you retrieve it. That way we can determine if something is wrong with the query (that is, not returning what you want), or if there is something going wrong with the transfer to a new page. That would be a good next step.

The transfer to the next page is causing the problem: the name is output on the login page and it works.

Okay. I should have been clearer. What exactly did you try to output? Did you try calling upon the session variables? That’s important. I apologize for not being clearer. If so, then we’ve narrowed it down a bit.

First, I think we should clean up the corner code. You start with an evaluation, and then you create a variable that is used to do the exact same evaluation but negated. We could delete that first php element entirely with something like:
[php]

<?php if (isset($_SESSION['name'])) { echo "Hello, ".$_SESSION['name'].$_SESSION['username']."!"; } else { echo "Hello, guest! Login here or sign up here!"; } ?> [/php] Much clearer IMO. Anyways, let me know about the printing you tried.
Sponsor our Newsletter | Privacy Policy | Terms of Service