Header() not working

I have a form…


I have this at the top of the page…

<?php
session_start();
include 'db/conn.php';

if(!empty($_POST)) 
{
	//regex expressions
	//Alphabetic and range: /^[a-zA-Z]{2,15}$/
	//length: /^[a-zA-Z]{2}$/
	 
	 
  function checkFirstName($input) {
	  
	  $pattern = '/[a-zA-Z]{2,15}/';
	  if(preg_match($pattern, $input)) {
	      //set a SESSION variable
		   $_SESSION['First_Name'] = $input;
		   return true;
	  
	  } else {
		  //refresh the page usinng HTML
	      header('index.php?error=1');	  
	  }
  
  }

  checkFirstName($_POST['First_Name']);

} else {
?>
{show the page}

But I just get

(blank page)
when the form is submitted.
Did the header() not work?

Chances are you are getting a php error of some sort, probably a “content already sent”. Remember that header can only be used when no output has been provided yet.

No. The header syntax is actually wrong. It needs to be

header('Location: index.php?error=1');
5 Likes

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