0

Here is my code, not too sure why it doesn't work but it cannot be processed. I can process phpinfo() correctly.

<?php

include("tools.php");
$username = $_POST["uname"];
$email = $_POST["email"];
$pasword = $_POST["pword"];

if(isset($username) and isset($email) and isset($password)){
    if(add_user_database($username, $email, $password) == TRUE){
        echo "You've been added!!!";
        header("location:login.php");
    }else{
        echo "<script>alert('Error has occurd please contact " . 
                            "support or try again later');</script>";
        header("location:register.php");
    }
}else{
  echo "<script>alert('Please fill in all forms');</script>";
  header("location:register.php");
}
?>
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
Nathan
  • 62
  • 1
  • 6
  • 2
    Try removing the echo statements before each `header()` call. It is a bad practice to show any message when you want to have a redirect page. Also, depending on your php error settings, it may throw an error. – Ibrahim May 14 '17 at 15:19
  • 1
    Are you getting any error? If not, add these lines at the very top of your PHP script, `ini_set('display_errors', 1); error_reporting(E_ALL);` and see if it yields any error or not. – Rajdeep Paul May 14 '17 at 15:20
  • 1
    It's not a bad practice it is wrong. One can't send client data before a redirect call with header command. – Jorge Campos May 14 '17 at 15:20
  • I'm guessing it's probably this: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php. If there anything interesting in the error log? or are you familar with [how to get errors to print to the output](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)? – HPierce May 14 '17 at 15:21
  • What are you trying to achieve? Echo, "You've been added" and then the redirect? – Robert May 14 '17 at 15:22
  • 1
    You do see that you have `$pasword` with one s, right? – junkfoodjunkie May 14 '17 at 15:56

2 Answers2

0

From the php docs,

"Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file."

You shouldn't need the echo things there, if you really wanted those messages, you could set them as $_SESSION('statusMessage'); and then on the redirect page check if it is set, echo out something to show it, and set them to undefined.

Also, please please please make sure that input gets sanitised in add_user_database()!!

EDIT:

Helpful hints

In the check login script:

if(add_user_database()){
  $_SESSION['addUserStatus'] = "Success, you have been added, woo!";
  header("Location: someOtherPage.php");
}else{
  $_SESSION['addUserStatus'] = "Error! Please contact support for assistance!");
  header("Location: someOtherPage.php");
}

In the some other page:

if(isset($_SESSION['addUserStatus']){
  echo "<script>showLoginMessage('" . $_SESSION['addUserStatus'] . "')</script>";
  $_SESSION['addUserStatus'] = undefined;
}
Jason Swan
  • 109
  • 5
0

Header already sent error look at http://php.net/manual/en/function.header.php

Remember that header() must be called before any actual output is sent

user10089632
  • 5,216
  • 1
  • 26
  • 34