0

How can i echo the first and last name of the specific user that logs in? Here is my code for checkLogin.php:

$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];

$sql="SELECT * FROM myweekprofiles WHERE LastName='$LastName' and FirstName='$FirstName'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){

session_register("FirstName"); 
session_register("LastName"); 
header("location:loginSuccess.php");
}else {
  //This page will describe that the email/password is incorrect
}

Here is the code for inc_header_User.php:

if(!isset($_SESSION['FirstName'])) {
echo "Hello";
}else{
$FirstName = $_SESSION['FirstName'];
}

I try to echo $FirstName within the header, but i get an error that says:

Undefined variable: FirstName in C:\wamp\www\MyWeek\includes\inc_header_User.php on line 55

7
  • could you please point which line is line #55? Commented Jul 1, 2013 at 20:04
  • 1
    First off. You'll likely get a response to 'NOT USE MYSQL_* FUNCTIONS'. They are a bit outdated. mysqli is a better solution, or possibly PDO. As for this question, do you have session_start(); in your page code before trying to call $_SESSION['Firstname']? Commented Jul 1, 2013 at 20:05
  • line #55 says <?echo $FirstName;?> Commented Jul 1, 2013 at 20:06
  • 1
    Also, as a heads up, you will want to do some research on 'SQL Injection' and how to combat it before beginning with any kind of login system. en.wikipedia.org/wiki/SQL_injection Commented Jul 1, 2013 at 20:08
  • 2
    You also need to call session_start(); before adding anything to the session variable, and you may need to call session_write_close(); at the end of the script. Commented Jul 1, 2013 at 20:11

1 Answer 1

2

Two things that could have gone wrong.

First, This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0. from the documentation. So delete these lines from checkLogin.php:

session_register("FirstName");

session_register("LastName");

And write this instead (note the added session_start() ):

session_start();
$_SESSION['FirstName'] = $FirstName;
$_SESSION['LastName'] = $LastName;

Second (might be derived from the first, might not be), the logic in inc_header_User.php could be wrong for your purposes, try changing it to:

session_start();
if(!isset($_SESSION['FirstName'])) {
$FirstName = '';
echo "Hello";
}else{
$FirstName = $_SESSION['FirstName'];
}

Other notes: Is it an error, a notice or a warning? It doesn't look fatal actually, echoing a non-set variable will not likely kill your whole app.

You are prone to MySQL injection, change the first lines to this:

$FirstName = mysql_real_escape_string($_POST['FirstName']);
$LastName = mysql_real_escape_string($_POST['LastName']);

As already suggested, try using MySQLi or PDO instead of mysql_* functions for new code.

Sign up to request clarification or add additional context in comments.

15 Comments

Is this better: if(!isset($_SESSION['FirstName'])) { echo "Hello"; }else{ $_SESSION['FirstName'] = $FirstName; } ?>
I edited the answer since I found another logic mistake that is causing the actual error. If the session variable is not set, you keep it unset, which causes the problem. And I don't think that new code you wrote is better...
You might add as well, that the session should be started ;)
I added the code you included: if(!isset($_SESSION['FirstName'])) { $FirstName = ''; echo "Hello"; }else{ $FirstName = $_SESSION['FirstName']; } but i still dont see the firstname appering
Now it looks like a perfect answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.