Redirect if login successful not working

Hello. I have this login page, where I am getting the ID and Password from the index.html.
Now it is setup, that when I input the id and password in the index.html, the values is send to login.html, that is calling the function login and checks for valid login.
If the login is valid then:

echo "<h1>Welcome: </h1><h4>".$_SESSION['session_member_first_name']."</h2><a href = 'welcome_member.php'> Click here </a>";

What I wanted to do, is to include this code, so that it will automatically redirect to the welcome_member.php, but it doesn’t work:

header('Location: welcome_member.php');

How can I do this?

This is the full page:

<div class="row">
<div class="center-div">
	require ('./database_initialisation.php');
	$id = $_POST['inputID'];
	$pass = $_POST['inputPassword'];
	$member = $_POST['appUser'];

	if ($member == 'Member' )
			$member_query->logIn($id, $pass);

			if(isset($_SESSION['session_member_first_name']) )
				{header('Location: welcome_member.php');
				//echo "<h1>Welcome: </h1><h4>".$_SESSION['session_member_first_name']."</h2><a href = 'welcome_member.php'> Click here </a>";
			else {echo "Credential do not match out database records";}	

			elseif ($member == 'Employer')
				$admin_query->logIn($id, $pass);
				if(isset($_SESSION['session_employer_first_name']) )
				{header('Location: welcome_employer.php');
				//echo "<h1>Welcome: </h1><h2>".$_SESSION['session_employer_first_name']."</h2><a href = 'welcome_employer.php'> Click here </a>";
			else {echo "Credential do not match our database records";}	

	{ echo "Could not log In";} ?>

Those PHP headers can only be included on the page IF nothing else has been outputted to the page yet.

<div class="row">
<div class="center-div">

This makes it so the header code will not work. If you checked your error logs you would surely see something about content being outputted before headers.

Yes, the error log file does say that. So how can I get around this and make the welcome page load?
Can I just take the div’s out and leave the page as it is?

Put the headers before any content :slight_smile: . You should be detecting the sessions and whether they are valid before any content so…dunno. Just restructure to check for them before any content :slight_smile: . Perhaps a PHP guru can advise a better way to structure.

1 Like

I managed to do it. I just took out the div’s. My mouth is faster than my brain. hahaha

I don’t if I would just “take them out” but instead move them (or more likely move the header() call)

Does the resultant mark-up still validate?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.