Hey everyone, I'm relatively new to PHP coding and I have a question I've been having trouble finding the answer to - it may be I'm not sure exactly what I'm needing or what it's called. I am trying to create a very rudimentary login in system where users will register, log in, and then other pages will check whether the user is logged in and if so, display their first name from the mySQL database. Here's what I have:

Login Page

<?php
session_start(); // Start a new session
require('dbconnection.php'); // Holds all of our database connection information

// Get the data passed from the form
$username = $_POST['user'];
$password = $_POST['pass'];

// Do some basic sanitizing
$username = stripslashes($username);
$password = stripslashes($password);

$password = md5($password);

$sql = "select * from User where Username = '$username' and Password = '$password'";
$result = mysql_query($sql) or die ( mysql_error() );

$count = 0;

$count = mysql_num_rows($result);

if ($count == 1) {
	 $_SESSION['loggedIn'] = "true";
	 header("Location: index.php"); // This is wherever you want to redirect the user to
} else {
	 $_SESSION['loggedIn'] = "false";
	 header("Location: http://www.msn.com"); // Wherever you want the user to go when they fail the login
}

?>

And on my index page I have the following snippets:

Start Session (At The Top of the Page)

<?php
session_start();
if ($_SESSION['loggedIn'] != "true") {
	 header("Location: login2.php");
}

?>

Echo User Logged In (In the Middle of the Page)

<div class="user"><? if ($_SESSION['loggedIn'] = "true") {echo '$FName';} else {echo 'Not Logged In';}?></div><br>

I think it's that the variable $FName is not being defined in this file. In saying that, how would I go about defining this variable given that it's being set by whoever is logged in (the Session?). Or does it need to be set on the login page? :-/

Alright, the issue was defining the variable; however, now I'm unable to get the value I want. I'll start another thread on that issue. For those that are interested, you can pass through the Username variable by adding the following to Line 8 on the Login Page:

$_SESSION['username'] = $_POST['user'];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.