2

I am pretty new to this and 'enjoying' some of the frustrations and long hours associated with learning something new. Anyway! I want to get my php script to do the following:

A) See if the form at the bottom of the page (in the footer of each of the pages of the website) has been triggered using a hidden input. B) Open the table in the database and do some stuff to it. C) Depending on the outcome of this, stop processing, forget everything and go to one of two new php pages; either 'thanks for joining' or 'already a member'.

A and B are working splendidly and C is refusing to work no matter what i do. I have tried using header("Location: http://www.xxx.co.uk/xxx.php"); in just about every part of the page and with all the various subtle changes - single quote, double quote, space after Location, etc. that i have seen used on other answers.

If i issue an exit after the header() command then it does this and i just get a totally white page and nothing, if i don't exit then the page that i was just on comes back unchanged.

I have read the other posts on this subject and see that there is a complication with header to do with an error about header already having been called, but i don't understand the answer and I am not seeing an error, it just will not redirect.

I have tried moving all the php to the top of the page before even the opening doctype declaration and still no dice.

Could it be something to do with the structure of my pages which is as follows:

Each php starts with an include_once for a header.php and ends with an include_once for a footer.php. Inside the header I start with an include_once for a php fuctions file and then some html and then check to see if my footer form was used.

Can somebody explain the limitation with the header() command in simple terms so that i can see whether i am going to be able to use it or whether i will need to redesign my whole strategy.

Here is some code:

One of the pages:

<?
    include_once ("includes/header.php");
?>
HTML distinct to page
<?
    include_once ("includes/footer.php");
?>

The Header file:

<? include_once("includes/functions.php"); ?>
  In here is just the logon script for the database and a global variable for the logon...
<?
    if (isset($_POST['submitfoot'])) {

Code to send me an email to tell me what is happening...    

/* log on to database */
openDB();
/*tidy fields and check for existing record */
$tidyemail = trim($_POST['footemail']);
$tidyname = trim($_POST['footname']);
$sql_email = "SELECT id FROM subscribers WHERE email = '".$tidyemail."'";
$check_res = mysqli_query($mysqli, $sql_email) or die(mysqli_error($mysqli));

 if (mysqli_num_rows($check_res) < 1) {

 /* free result add the new record */
 mysqli_free_result($check_res);
$add_sql = "INSERT INTO subscribers (name,email,date,sub) VALUES('".$tidyname."','".$tidyemail."',CURDATE(),1)";
$add_res = mysqli_query($mysqli, $add_sql) or die(mysqli_error($mysqli));

/* redirect to thanks page */
header("Location: http://www.xxx.co.uk/thanks.php");

} else {

/* redirect to already subscribed page */
header("Location: http://www.xxx.co.uk/subalready.php");

  }
}

?>

Then there is the rest of the HTML including js script headers, page header, page body with common menu system for all pages etc.

I realise that the code here on the forum is a mess, but it is much neater in the actual files, so apologies for any difficulty.

Thank you in advance for any guidance that will stop me from going insane with this problem.

EDIT

Thank you showdev - i now have enabled errors as you recommended: I get this:

Warning: Cannot modify header information - headers already sent by (output started at /homepages/9/d302209163/htdocs/includes/header.php:9) in /homepages/9/d302209163/htdocs/includes/header.php on line 59

So this is the problem that previous questions have faced i beleive. Can anybody, gently, tell me how to sort this out please.

Thanks...

Donquick
  • 73
  • 9
  • 1
    To help with [error reporting](http://php.net/manual/en/function.error-reporting.php), you might try including `error_reporting(E_ALL); ini_set('display_errors', 1);` – showdev Jan 30 '15 at 00:37
  • in general a header() call should be the very first output sent to the browser - in other words no other character (whitespaces, newlines, special characters, etc) may precede it, otherwise it wont behave as intended. – Chris du Preez Jan 30 '15 at 00:51
  • What does `openDB();` do? – Darren Jan 30 '15 at 00:51
  • 1
    Do you have any code in `header.php` (around line 9) preceding the `header()` statement like this: `...?>(space)...?>` or `...?>(newline)...?>` – Chris du Preez Jan 30 '15 at 00:59
  • You may have already seen it, but this is a [pretty good explanation of the issue](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php#8028987). – showdev Jan 30 '15 at 01:04
  • @showdev line 9 is `openDB();` which leads me to believe an error is being caused within that function somewhere, outputting something. – Darren Jan 30 '15 at 01:04
  • Hi All - and thank you for the suggestions so far. I have succeded thanks to being able to switch on error checking and then more moving code around. The problem was solved by incorporation the call to my functions php within my main php area. So instead of havign two ... ?> sets i have just one with all the instruction in there. I have also had to leave the php at the very top of the header.php otherwise the error persists. – Donquick Jan 30 '15 at 01:13
  • Chris - Can i ask you about your statement regarding the very first output please. This is what I understood from former posts, but i can't get my head round it. I presume it doesn't mean the first bit of code, otherwise it would be extremely limited in use, so what constitutes output. The follow on question is to ask why there is no commoand in php without this limitation. Surely, 'stop doing what you are doing and go to a requested URL without question' is entirely fundamental to a language designed for webpage coding. Would you agree? – Donquick Jan 30 '15 at 01:20

1 Answers1

0

have you already tried instead of using:

header("Location: http://www.xxx.co.uk/thanks.php");

and

 header("Location: http://www.xxx.co.uk/subalready.php");

try changing it to:

header("Location:thanks.php");

and

 header("Location:subalready.php");

I just assumed that they are both contained in your WWW folder you don't need to have to add the url anymore.

Ravelkin
  • 3
  • 5
  • This doesn't solve the issue. The script doesn't reach there because of errors in the document so it will **never** redirect. – Darren Jan 30 '15 at 01:05
  • Frogive me I don't know the etiquet regarding answers and just comments. I have solved the problem by combining my two previous php sections into one and leaving this at the very top of header.php/ Thanks. – Donquick Jan 30 '15 at 01:16